Preparation of TCP-based applications

This seems to be a very simple topic, just like "you can do is a personal site", as you might think "individual will be able to use TCP socket write network program." But a few basic principles, following the introduction of practice, you may not understand.

TCP is a stream protocol, simply, TCP does not check the semantics of the data, the boundary will not check data, the application layer is generally used protocol packet, so there will be so-called "stick pack" "demolition package "problem. To this end, it had a certain usage and patterns.

Any application, all message protocol design must first be.  Although some people cover your ears and cried, "I do not need the message protocol," but he still needs to packet protocol design. There are several ways to design messages protocol:

1. The length of the data packets explicitly stated.
2. delimiters.
3. The sender will transmit the connection is closed.

A third is the specific use of the socket.

Telegram Method 1: the length of the message data explicitly stated

This method is more generally used, because of the high compatibility performance will introduce a method of time you know. Typically a binary integer fixed on top of a few bytes of data storage, display of the following data length. However, this is relatively close to the underlying hardware packet protocol design application layer is generally not done, the front end of a fixed number of bytes of data stored in ASCII number, the front end of the string fill '0', or after the number string with newline '\ n', which is a method of mix and 2.

Telegram Method 2: Using delimiters

The method described previously mentioned a time, using the delimiter to separate the packets, and then in a general language has split () function, simple to use, but the use of the separator has a disadvantage that the data to be escaped, avoid message data with delimiters, it is not good. there is also a drawback of this method is to traverse each byte, look for separators, performance is not good. this section describes how to 1, because we know exactly digital is behind the string with line breaks, so no escape, there will be no escaping the loss of performance, while generally very short string of numbers, you can also ignore traverse performance loss.

Telegram Method 3: the sender will transmit the connection is closed

This is a way of using HTTP 1.0, HTTP 1.0 is closed in response to the connection After transmission (of course, after sending the request can not close the connection, it can be imagined, necessarily using HTTP 1.0 or Method 2 Method 1, you can go learn Learn). this method is not used, because the very narrow application scenarios, the function difference.

TCP is difficult to commonly used applications are understood idiom:

1. The loop must be used to send data

To the original socket, it is a function of the transmission data write:

ssize_t write(int fd, const void *buf, size_t count);

However, only a portion of the write data may be transmitted ahead of you request, i.e., write the return value (the number of bytes that have been sent) may be less than the parameter count. Therefore, you should call the write cycle, and check the return value. Please carefully look APUE (Advanced Programming in the UNIX) relevant content.

2. The loop must be used to receive data

Reading data interface functions:

ssize_t read(int fd, void *buf, size_t count);

I often see some people, because the data is not completely received sent by the sender, and the sender complained several times to call the write method. This is a mistake of complaining, based on the erroneous understanding of the TCP.  * Regardless of whether they call how many times write, you can not just call once read! even if you receive buffer is set to 1GB is not OK! *

First, the sender call write, copy the data to the transmission buffer of the sender, and the sender network subsystem period (the fragment) section transmits data in the buffer. The recipient network subsystem to the data fragments in order assembling the receive buffer, once into the receive buffer, there is no argument fragment the recipient calls the read method may read all or part of data in the buffer after return, if only partially, and this portion of the data segment no contact - remember this!

3. Standard IO interface is called only once fgets / fputs

The standard IO gets / puts up to provide a packet-based interface, which checks the data in the buffer delimiter '\ n', so as to separate the packets, so called only once when you can read the other side gets a call when data puts sent, do not be surprised standard IO package to help you read and write cycle.

4. At the end of the string is always plus '\ 0'

If you want a certain period of the byte array as a C string to deal with, then you must manually in the string should be '\ 0' local plus '\ 0'. For example, if you think ptr [0-5 ] (6 data bytes, the last byte should be a value '\ 0') is a string, then, prior to the processing, should be performed ptr [5] = '\ 0'; note, thousands million will not execute ptr [strlen (ptr)] = '\ 0'! way to guarantee whether they're unintentionally or maliciously does not contain '\ 0', you can be treated safely. in addition, without having to perform in front of a similar reception memset (ptr, 0, bUFLEN) statement, this will waste a little bit performance, modify only one byte is better than six or more modified bytes faster.

See also: TCP packets do not read the complete problem analysis (stick package, unpacking)

Related posts:

  1. Linux kernel programming - fsync, write
  2. The use of jemalloc compilation error
  3. Be careful int multiplication overflow!
  4. iOS data correctly received HTTP chunked method
  5. C ++ member function as an argument pthread_create
Posted by  ideawu at 2009-10-12 15:15:52

Guess you like

Origin www.cnblogs.com/leijiangtao/p/12078561.html