Socket platforms communicate with each other. NET

Source: Internet
Author: User

The two platforms communicate with each other, and the other party sends the data. We receive the data, process the data, and send the result to the other party. The other party performs corresponding operations.

= IPEndPoint (address, = [] arrMsg = [* arrLen = clientReqSocket. Receive (arrMsg );

Extract actual valid data:

            [] arrNew =  , arrNew, , arrLen);

After the arrNew is connected to the received data for business processing, the result arrResult is returned to the other party, and the connection is closed:

       socket.Close();

Explain the above Code: first, socket. accept (); this method, the service will stay on this method all the time, wait for the client connection, once a client connection comes up, the following code will continue to be executed. If you do not have a client to connect to the service, the program will stay in this place, and the phenomenon of "false death" will occur. The preceding sample code can only process connection requests from one client. After the request is processed, the connection is closed. The server should be a program that never stops. It can always listen to client requests and process and feedback them.

Therefore, the preceding problem is described. First, the thread is used. After the service is enabled, the server processes the listening status. When a client connection request comes up, a new thread is created for the client connection, to process the request operations of the client. When multiple clients are connected, the Service creates a new thread for each client. In this way, the service can continue to listen to connection requests from other clients and respond to the connected clients. Second, to enable the Service Team to listen to client connection requests forever, the server must continue to call the socket after processing a client connection request. accept () to listen for connection requests. Here we use the while (true) {} infinite loop.

         isListener =                   =[] arrMsg =  [ *  arrLen =[] arrNew =  , arrNew, = 

As there is only one client in this demand, this time there is no multi-threaded approach. It is only on the main thread that a new thread is created to listen to and process client request operations. In this way, the main thread can continue to execute its tasks and the program will not be in a "suspended" state.

Of course, the server must be able to identify and judge the data sent from the client to extract valid data. Therefore, during the communication between the two parties, the data format is agreed. Only data that complies with the format can be processed, parsed, and responded. The Interface Protocol definition is shown in the following table (the XML structure definition of the Data Protocol is omitted ):

The definition of the communication data packet structure is shown in the following table:
Baotou mark Total Length Message Type XML Stream Length XML format data Package tail mark
4B 4B 1B 4B   2B

 

The definitions of each item in the data packet are described as follows:
Serial number Project name Value Description
1 Baotou 0xE1 0x2B 0xD3 0x78 Take a fixed value
2 Total Length   4 bytes (including the length of the end of the packet header)
3 Message Type 0x21 or 0x22 Used to differentiate the data packet type
4 XML Stream Length   Length of data in XML format (4 bytes)
5 XML format data   Is the XML message passed
6 Package end 0xFF 0xFF Take a fixed value

 

The following table describes the data packet protocol types:
Message number Description XML format
0x21 Indicates that the client sends data to the server. XMLInfoSed
0x22 Indicates that the server sends data to the client. XMLInfoReq

After the communication protocol is defined, the Socket communication needs to follow this Protocol. Both the server and the client only receive the packets that follow this protocol and send the packets in this format.

         isListener =                   =[] arrMsg =  [ *   arrLen = (arrLen !=  (arrMsg[] == )=                                  Buffer.BlockCopy(arrMsg, , arrNew, , arrLen -  msg = Encoding.GetEncoding(= ;  (arrMsg[] == )[] arrNew =  [                                arrNew[] = ] = ] = ] =                                 arrNew[] = [] xmlArr =  , xmlArr,  msg = Encoding.GetEncoding( hexLength = String.Format(                                arrNew[] = Convert.ToByte(hexLength.Substring(, ), ] = Convert.ToByte(hexLength.Substring(, ), ] = Convert.ToByte(hexLength.Substring(, ), ] = Convert.ToByte(hexLength.Substring(, ),                                  dataLen = arrNew.Length + xmlArr.Length +  dataHexLen = String.Format(                                arrNew[] = Convert.ToByte(dataHexLen.Substring(, ), ] = Convert.ToByte(dataHexLen.Substring(, ), ] = Convert.ToByte(dataHexLen.Substring(, ), ] = Convert.ToByte(dataHexLen.Substring(, ), [] newArr =  , newArr, - ] = - ] =                                 Socket toclientSocket = ==  IPEndPoint(address,  [] {  }); [] {  });                                 clientReqSocket.Close();                                 = 

Here the service becomes a machine similar to a transfer station. The client sends data to this service. This service receives data and parses the data. After the data is parsed, it is handed over to the background system, after the background system processes the business logic of the data, it sends the result data to the service, and then the Service sends the final result data to the client. The client and the background system can directly communicate with each other without the intermediate link of the service. However, due to business requirements, the intermediate link of the service must be established.

The preceding Code indicates that the home page service must first determine whether the received data is the data sent by the client or the data that the background system needs to send to the client. According to the definition of the communication data packet structure, the message type occupies 1 byte. There are 4 bytes in front of the packet header and 8 bytes in total, so the message type is at the position of 9th bytes. Connect to the received data arrMsg [8] 9th bytes for determination. If arrMsg [8] = 0x22 indicates the data sent by the client, the data sent by the client, this service extracts XML data, parses XML data, and sends the data required by the background service to the background.

Buffer. BlockCopy (arrMsg, 13, arrNew, 0, arrLen-15); Removes the first 13 bytes and the last two bytes to extract the XML data.

If arrMsg [8] = 0x22 indicates the data that the background system sends to the client. To receive the data sent from the background system, encapsulate the data first, assemble the data into an agreed XML structure (the data received by this service is XML data), and add the XML data to the end of the packet header, re-encapsulate the data structure that can be recognized by the client. Re-encapsulate the data and send it to the client, and notify the background System of the sending result status.

Here, I would like to share with you how to interact with each other when there is a definition of the Communication Interface format during network 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.