C#socket Communication

Source: Internet
Author: User

About C#socket communication, which is divided into synchronous and asynchronous communication, this paper briefly introduces the synchronous communication.

The two ends of the communication are client and server, respectively:

(1) Cient:

1: Create a socket pair image;

2: Connect () using the socket to the image of the endpoint as a parameter, to the server to make a connection request;

3: If the connection is successful, send a message to the server using the Send () method of the socket for the image;

4: Accept the message sent by the server using the receive () method of the socket image;

5: Remember to close the socket after the communication is over;

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks;        Using system.net.sockets;using System.net;namespace client{class Program {static Socket clientsocket;            static void Main (string[] args) {string IP = "127.0.0.1";            int port = 8885;  IPAddress IP = ipaddress.parse (IP); Converts an IP address string into a ipaddress instance clientsocket = new Socket (addressfamily.internetwork,sockettype.stream,protocoltype.tc p);//Use the specified address cluster protocol, socket type, and communication protocol IPEndPoint EndPoint = new IPEndPoint (IP, port);  Initializes the IPEndPoint instance Clientsocket.connect (endPoint) with the specified IP and port number;            Establish a connection with the remote host Console.WriteLine ("Start sending message");  byte[] message = Encoding.ASCII.GetBytes ("Connect the Server");            A byte array is actually sent when communicating, so the sending message is converted to byte clientsocket.send (message);            Console.WriteLine ("Send message:" + Encoding.ASCII.GetString (message));            Byte[] receive = new byte[1024]; int LEngth = Clientsocket.receive (Receive);            The length of the received byte array is Console.WriteLine ("Receive message:" + Encoding.ASCII.GetString (receive));  Clientsocket.close (); Close Connection}}}

The client returns the result:

(2) Server:

1: Create a socket pair image;

2: Bind the endpoint with the bind () method of the socket to the image;

3: Use the socket to the image of the Listen () method to start monitoring;

4: Accept the connection to the client, and use the socket to create a new socket for the image to communicate with the client such as the request;

5: Receives (receive) and sends (send) messages with the new socket object.

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using system.net.sockets;using system.net;using System.threading;namespace server{class Program {static SOC        Ket Receivesocket;            static void Main (string[] args) {int port = 8885;  IPAddress IP = ipaddress.any; Guest activity for listening on all network client interfaces Receivesocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, protocoltype.tcp); /using the specified address cluster protocol, socket type, and communication protocol
Receivesocket.setsocketoption (socketoptionlevel.socket,socketoptionname.reuseaddress,true); About socket settings IPEndPoint endPoint = new IPEndPoint (ip,port); Receivesocket.bind (New IPEndPoint (IP, port)); Bind IP address and port number Receivesocket.listen (10); Set up to 10 queued connection requests Console.WriteLine ("Establish connection"); Socket socket = receivesocket.accept (); Byte[] receive = new byte[1024]; Socket. Receive (receive); Console.WriteLine ("Received message:" + Encoding.ASCII.GetString (receive)); byte[] Send = Encoding.ASCII.GetBytes ("Success receive the Message,send the back of the message"); Socket. Send (send); Console.WriteLine ("Send Message:" +encoding.ascii.getstring (send)); } }}

The server returns the result:

 

C#socket Communication

Related Article

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.