Asp.net Socket Client (remote data sending and receiving)

Source: Internet
Author: User

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
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.