Network transmission is a complex process. Although Winsock has hidden this complexity, some of the underlying features will affect the transmission, simple hopes to send a handle, often change with the network environment, will be violated! Some procedures have no problems in the LAN, but it is often an error in the Internet, which is also the reason. Therefore, it is necessary to consider the uncertainty of network communication.
Some people expect every time to send, the other party produces a DataArrive event, which is too ideal, but network data will not only bond, but also the possible separation.
After calling the SendData method, the data is to enter the send buffer. If the system has the opportunity to really send it, if the last time is sent, the previous time has not been sent, then the data will be sent one time.
Is the receiving end to receive it once? In TCP control, the data packet is processed, that is, according to the network channel condition, it automatically seals the data into a size suitable packet to send, so that if the amount of data exceeds the package size, it will be numbered. In multiple packages. Although the order arrival, the receiver will be reorganized according to the number of the data packet, and if there is no interrupt number after each new package arrives, all DataArrive events will be generated. Provided to the program, can be seen once, it is possible to generate multiple DataArrive events.
Each IP package will be relatively large when the network is open, and the network is crowded, and the local area network is generally greater than the Internet. Sending string data, the separator should be added to each independent data, and the receiving end is re-separated by the separator.
This method is small for small data error, and the general IP packet in the Internet is less than 512 bytes, often around 4k, is very common, which is completely enough for a normal string command.
If you want to design it, it is recommended to consider the possibility of data separated. This separation is sometimes annoying. Maybe a string command is divided into two packs, or even one Chinese character will be Divide in two packs (when I met this phenomenon, I didn't understand it)!
Advanced network communication programs should be abandoned String transmission, only processing the Byte array transmission, so you don't have to worry about the above problems.