Copy codeThe Code is as follows: /***************************************
* Object Name: SocketObj
* Function Description: Remote Sending and receiving
* Trial example:
* Using EC; // The name of the referenced space.
* String url = "218.75.111.74"; // The URL can also be in this form (http://www.baidu.com /)
* Int port = 8000; // port
* String SendStr = "domainname \ n"; // organize the string to be sent
* SendStr + = "check \ n ";
* SendStr + = "entityname: domains \ n ";
* SendStr + = "domainname:" + this. TextBox1.Text + "\ n ";
* SendStr + = ". \ n ";
* EBSocketObj o = new SocketObj (); // create a new object
* O. Connection (url, port); // open the remote port
* O. Send (SendStr); // Send data
* Response. Write (o. Recev (); // receives data
* O. Dispose (); // destroy the object
**************************************** ******/
Using System;
Using System. IO;
Using System. Net;
Using System. Net. Sockets;
Using System. Text;
Namespace EC
{
/// <Summary>
/// Remote Socket sending and receiving
/// </Summary>
Public class SocketObj
{
Private NetworkStream ns;
Private bool _ alreadyDispose = false;
# Region construction and interpretation
Public EBSocketObj ()
{
//
// TODO: add the constructor logic here
//
}
Public EBSocketObj (string url, int port)
{
Connection (url, port );
}
~ EBSocketObj ()
{
Dispose ();
}
Protected virtual void Dispose (bool isDisposing)
{
If (_ alreadyDispose) return;
If (isDisposing)
{
If (ns! = Null)
{
Try
{
Ns. Close ();
}
Catch (Exception E ){}
Ns. Dispose ();
}
}
_ AlreadyDispose = true;
}
# Endregion
# Region IDisposable Member
Public void Dispose ()
{
Dispose (true );
GC. SuppressFinalize (this );
}
# Endregion
# Region open port
/// <Summary>
/// Open the port
/// </Summary>
/// <Param name = "url"> URL or: IP address </param>
/// <Param name = "port"> </param>
/// <Returns> </returns>
Public virtual void Connection (string url, int port)
{
If (url = null | url = "") return;
If (port <0) return;
If (port. ToString () = string. Empty) port = 80;
TcpClient tcp = null;
Try
{
Tcp = new TcpClient (url, port );
}
Catch (Exception E)
{
Throw new Exception ("Can't connection:" + url );
}
This. ns = tcp. GetStream ();
}
# Endregion
# Region send Socket
/// <Summary>
/// Send Socket
/// </Summary>
/// <Param name = "ns"> </param>
/// <Param name = "message"> </param>
/// <Returns> </returns>
Public virtual bool Send (string message)
{
If (ns = null) return false;
If (message = null | message = "") return false;
Byte [] buf = Encoding. ASCII. GetBytes (message );
Try
{
Ns. Write (buf, 0, buf. Length );
}
Catch (Exception E)
{
Throw new Exception ("Send Date Fail! ");
}
Return true;
}
# Endregion
# Region receiving information
/// <Summary>
/// Collect information
/// </Summary>
/// <Param name = "ns"> </param>
/// <Returns> </returns>
Public string Recev ()
{
If (ns = null) return null;
Byte [] buf = new byte [1, 4096];
Int length = 0;
Try
{
Length = ns. Read (buf, 0, buf. Length );
}
Catch (Exception E)
{
Throw new Exception ("Receive data fail! ");
}
Return Encoding. ASCII. GetString (buf, 0, length );
}
# Endregion
}
}