Category : utility
Matters to be attended to program Windows/Linux Socket
1. connection CLOSE
- When the connection is closed, specific function is called for informing the connection CLOSE.
- Check the return value of recv() function if it is zero. If it is zero, the connection is closed by peer.
2. connection RESET
If the connection is crashed the socket unexpectedly, ezTCP could reset that connection after about 1 minute by Keep-Alive Timer. When ECONNRESET(Connection reset by peer) error is occurred, refer to above line.
3. Broken PIPE
A process tried to read or write to a pipe where the other end of the pipe no longer exists. The user must handle this situation.
4. BSD recv() or Windows Receive() function
Note that the parameter len is the maximum length of the buffer. You shouldn't confuse this with the number of bytes actually read into the buffer. The length of received data is the return value of function.
5. What is the stream socket?
There are two types of Internet sockets(Actually, there are more). One is "Stream Sockets"; the other is "Datagram Sockets" which are sometimes called "connectionless sockets". Stream sockets are implemented on TCP. Datagram sockets are implemented on UDP.
Anyway stream socket provides a connection-oriented, sequenced, and unduplicated flow of data. Stream socket of course transmits data on a reliable basis, in order, and with out-of-band capabilities. In other words, the data is sent the flow of data regardless of data block size in stream socket contrary to datagram socket sends block of data. Misunderstanding about this difference could bring a potential bug of program.
For example, although the 512-bytes data is sent at a time from send() function to recv() function, it does not ensure to receiving that data at one time.
In most case, it can be received at a time. But, actually, it is subject to network environment or Kernel structure. Similarly, nevertheless the 256-bytes data is sent twice, it can be received at a time.
When you develop the application program related ezTCP, pay attention to handling stream socket.
6. Reuse listening address
When a TCP connection is closed, the connection may remain in a timeout state for a period of time after the connection is closed(typically known as the TIME_WAIT state or 2MSL state).
You can confirm this "netstat -a"(in Windows) or "netstat -t"(in Linux) command, and the reason is below:
* Remember that TCP guarantees all data transmitted will be delivered, if at all possible.
When you close a socket, the server goes into a TIME_WAIT state, just to be really sure that all the data has gone through. When a socket is closed, both sides agree by sending messages to each other that they will send no more data. This, it seemed to me was good enough, and after the handshaking is done, the socket should be closed. The problem is two-fold. First, there is no way to be sure that the last ack was communicated successfully. Second, there may be duplicated data left on the net that must be dealt with if they are delivered.
* MSL(Max Segment Lifetime) : specified as 2 minutes(RFC793, 1122)
Enabling SO_REUSEADDR socket option enables reuse of existing socket address even though a previous connection is in a timeout state.