C # Socket Study Notes 1,

Source: Internet
Author: User

C # Socket Study Notes 1,

First, let's take a look at the following points:

1. How do processes communicate with each other in the network? 2. What is Socket? 3. Basic socket operations 3.1, socket () function 3.2, bind () function 3.3, listen (), connect () function 3.4, accept () function 3.5, read (), write () function and so on 3.6, close () function 4. Three handshakes of TCP in socket establish connection details 5. Four handshakes of TCP in socket release connection details 6. An example: currently, socket communication uses TCP and UDP protocols, compared with UDP, TCP is a secure and stable protocol. This article only involves TCP socket communication. This section describes the three-way handshake of TCP/IP and the basic process of socket communication based on the handshake. Next we will introduce the three most notorious handshakes that are very familiar with the graduation interview: 1. the client sends the syn packet to the server and sets the serial number to x. 2. The server receives the request message sent by the client, then sends the syn packet to the client, and sends the confirmation serial number x + 1. The parallel transmission serial number is y. 3. After receiving the confirmation message sent by the server, the client sends the confirmation signal y + 1 and sets the sending sequence number to z. So far, the client has established a connection with the server. On this basis, the socket connection process: server listening: the server socket does not locate the specific client socket, but is waiting for the listener to monitor the network status in real time. Client request: the client clientSocket sends a connection request to the server's serverSocket. Therefore, clientSocket must know the address and port number of serverSocket and scan to send a connection request. Connection Confirmation: when the server socket listens to or receives a connection request from the client socket, the server responds to the request from the client. We recommend that you send a new socket to the client, once the client confirms the connection, the connection is established. Note: In the connection validation phase, even if the server socket is still listening After establishing a connection with a client socket, it can still receive connection requests from other clients, this is also the reason for one-to-Multiple Generation. Brief description of the connection process: the following code is analyzed: SERVER: TcpServer. cs copy code 1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 using System. net; 7 using System. net. sockets; 8 9 namespace TcpServer10 {11 class Program12 {13 static void Main (string [] args) 14 {15 // determine the port number 16 int port = 6000; 17 18 // set the connection IP19 string host = "127.0.0.1"; 20 21 // convert the IP address string to an IP address Instance 22 IPAddress ip = IPAddress. parse (host); 23 24 // indicates the network endpoint as the IP address and port number 25 IPEndPoint ipe = new IPEndPoint (ip, port ); 26 27 // set the Socket 28 // addressFamily parameter to specify the addressing scheme used by the Socket class 29 // The socketType parameter to specify the Socket class type 30 // protocolType parameter to specify the protocol used by the Socket. 31 Socket socket = new Socket (AddressFamily. interNetwork, SocketType. stream, ProtocolType. tcp); 32 33 // The socket is associated with the local endpoint. 34 socket. bind (ipe); 35 while (true) 36 {37 // start listening for port 38 socket. listen (0); 39 40 Console. writeLine ("the service is enabled. Please wait ..... "+ DateTime. now. toString (); 41 42 // create a new Socket for the new connection. Objective: the client will establish a connection 43 Socket serverSocket = socket. accept (); 44 Console. writeLine ("Connection established ...... "+ DateTime. now. toString (); 45 46 String recStr = string. empty; 47 // define the buffer for receiving client data 48 byte [] recbyte = new byte [1024]; 49 50 // return the number of received bytes 51 int bytes = serverSocket. receive (recbyte, recbyte. length, 0); 52 53 // capture the received bytes. Huawei string54 // parameter 1: byte array parameter 2: Start index parameter 3: total length 55 recStr + = Encoding. ASCII. getString (recbyte, 0, bytes); 56 57 Console. writeLine ("server received client information:" + recStr + "" + DateTime. now. toString () + "\ n"); 58 59 60 // The service end sends a message to the client 61 string s TrSend = "Hello Client! "; 62 byte [] sendByte = new byte [1024]; 63 // convert the sent string to byte [] 64 sendByte = Encoding. ASCII. getBytes (strSend); 65 66 // the server sends data 67 serverSocket. send (sendByte, sendByte. length, 0); 68 69 serverSocket. close (); 70} 71} 72} copy the code of the Client: copy the Code 1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 using System. threading. tasks; 6 using System. net; 7 using System. net. sockets; 8 9 namespace TcpClient10 {11 class Program12 {13 static int port = 6000; // listening port number 14 static string host = "127.0.0.1 "; // connect to the server IP15 static IPAddress ip = IPAddress. parse (host); // convert the IP address to ip instance 16 static IPEndPoint ipe = new IPEndPoint (IP, port ); // indicates the network endpoint as the IP address and port number 17 static Socket clientSocket = new Socket (AddressFamily. interNetwork, SocketType. stream, ProtocolType. tcp); // create the client Socket18 19 static voi D Main (string [] args) 20 {21 22 clientSocket. Connect (ipe); // The client starts to Connect to the Server 23 24 25 string sendStr = "Hello, Server! "; // Send a message to the server 26 byte [] sendBytes = Encoding. ASCII. getBytes (sendStr); 27 clientSocket. send (sendBytes); 28 29 30 string revStr = ""; // receives 31 bytes from the server [] revBytes = new byte [4096]; 32 int bytes = clientSocket. receive (revBytes, revBytes. length, 0); 33 34 revStr + = Encoding. ASCII. getString (revBytes, 0, bytes); 35 Console. writeLine ("response from the server: {0}", revStr); 36 37 clientSocket. close (); 38} 39} 40}

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.