Asp.net Socket Client (Remote sending and receiving data) favorites
/***************************************
* 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
}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/shaily/archive/2009/05/31/4230005.aspx