[Complete case] Development of TCP transmission client based on Socket

1 Program interface design

        TCP client is widely used in host computer development. In most cases, the host computer software is used as a TCP client to communicate with PLC or other servers. The main function of the TCP client is to connect to the server, send data, receive data, and disconnect. The difference between different occasions is that the content of sending and receiving data is different.

        This case is mainly to develop a TCP client software that has basic connection functions and can send strings in different formats such as ASCII and UTF8. It also supports sending hexadecimal strings, as well as files and JSON format data. According to the above functions, the design program interface is shown in the figure below:

2 Program code design

  • Receiving information display: First, write a method for receiving information display. Here, the received information is displayed through ListView.

  • Connect to the server: The connection to the server first instantiates the Socket object, and then implements it by calling the Connect method. After the connection is successful, a thread is started to receive the data returned by the server. The specific code is as follows:

  • Disconnect the server: Disconnect the server and directly call the related API methods of Socket.

  • Receiving data: For data reception and analysis, since the client software in this case supports a variety of different forms of data transmission, the reception must also support multiple forms, so for different forms, we created an enumeration type to facilitate subsequent follow-up Extension, the code is as follows:

  • Therefore, when the data is received and processed, we use the first byte as the type judgment. For example, if the first byte is 0, then the received data is considered to be an ASCII string, and it will be parsed in this form. The second byte from the beginning to the end is the valid data part, so the data reception and analysis methods are as follows:

  • Send ASCII string: For ASCII string, first convert the sent string into a byte array according to the ASCII encoding format, and then add a byte to the front of the byte array, the byte value is Message.ASCII, the code is as follows Shown:

  • Sending a file: Before sending a file, you must first select a file. Selecting a file mainly uses the OpenFileDialog component. The code is as follows:

  • After selecting the file, sending the file is to convert the selected file content into a byte array, and then add a byte to the front of the byte array, the byte value is Message.File, here will be sent twice, the first time is to send one The string tells the server the name of the file to be sent, using UTF8 encoding, the code is as follows:

  • Sending JSON data: JSON data mainly involves the conversion of entity objects and JSON files. Here, a JSONHelper method is written using the Newtonsoft.Json library, and then a collection of entity objects needs to be created, and then converted into a JSON string through the EntityToJSON method. Then convert the JSON string into a byte array. Similarly, you need to add a byte in front, and the byte value is Message.JSON. The code is as follows:

3 Program function debugging

Since the TCP server software has not been developed yet, here we use the network debugging assistant for functional debugging:

  • Establish connection and disconnection:

  • Various data types sending and receiving

 

Pay attention to the Xinge Education Service account thinger_as, backstage reply [lottery draw], the prizes are generous, first come first served.

 

Review previous content

 

[Siemens] Build Siemens PLC environment based on PLCSIM-Advanced

[Siemens] Siemens S7 communication protocol, those things you don’t know

[OPCUA] C# host computer realizes OPCUA communication case

[OPCUA] OPCUA+MQTT+Alibaba Cloud, what can you do

[WinCC] C#/.NET combined with WinCC to realize data communication

[WinCC] Teach you how to develop Wincc voice alarm plug-in based on C# [source code attached]

[Cloud APP] Based on C# to realize the mobile phone APP to access Siemens PLC [with source code]

 

Guess you like

Origin blog.csdn.net/xiketangAndy/article/details/108147881