Why address/port in use though socket was closed ??

2.7 Please explain the TIME_WAIT state.

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 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 “wandering duplicates” left on the net that must be dealt with if they are delivered.

Andrew Gierth ( andrewg@microlise.co.uk) helped to explain the closing sequence in the following usenet posting:

Assume that a connection is in ESTABLISHED state, and the client is about to do an orderly release. The client’s sequence no. is Sc, and the server’s is Ss. The pipe is empty in both directions.

   Client                                                   Server
   ======                                                   ======
   ESTABLISHED                                              ESTABLISHED
   (client closes)
   ESTABLISHED                                              ESTABLISHED
                <CTL=FIN+ACK><SEQ=Sc><ACK=Ss> ------->>
   FIN_WAIT_1
                <<-------- <CTL=ACK><SEQ=Ss><ACK=Sc+1>
   FIN_WAIT_2                                               CLOSE_WAIT
                <<-------- <CTL=FIN+ACK><SEQ=Ss><ACK=Sc+1>  (server closes)
                                                            LAST_ACK
                <CTL=ACK>,<SEQ=Sc+1><ACK=Ss+1> ------->>
   TIME_WAIT                                                CLOSED
   (2*msl elapses...)
   CLOSED

Can UDP use Connect() ?

A client application uses the CONNECT command to establish a connection between a local socket and a remote socket.

The command supports both blocking and nonblocking sockets. When the socket is in blocking mode, the function does not return until a connection with the remote peer is established or until an error is received. When the socket is in nonblocking mode, the function returns immediately with either the 36 EINPROGRESS return code or an error.

The CONNECT command performs differently depending on the socket type:

Stream (TCP) sockets
If the application has not already issued an explicit bind, the CONNECT command completes the bind of the socket. The API then attempts to establish a connection to the remote socket that is specified by the name parameter. You can call the CONNECT command only once. Issuing additional CONNECT commands results in a 56 EISCONN error.
Datagram (UDP) sockets
The CONNECT command enables an application to associate a socket with the socket name of a peer. The socket then is considered to be a connected UDP socket. You can call the CONNECT command multiple times with different peer names to change the socket association.
Rules:

  • Using the CONNECT command on a UDP socket does not change the UDP protocol from a connectionless to a connection-based protocol. The UDP socket remains connectionless. The primary benefit of using connected UDP sockets is to limit communication with a single remote application.
  • When a UDP socket becomes a connected UDP socket, it can no longer use the SENDTO and RECVFROM commands. Connected UDP sockets use the socket commands READ, WRITE, SEND, or RECV to communicate with the remote peer, instead of using the SENTO and RECVFROM commands.
Tips:

  • For nonblocking sockets, use the SELECT command to determine when a connection has been established. Test for the ability to write to the socket.
  • A connected UDP socket can revert back to an unconnected UDP socket by calling CONNECT with 0 or AF_UNSPEC specified in the domain field of the name parameter.

Format

Read syntax diagramSkip visual syntax diagram
>>-SOCKET--(--"CONNECT"--,--socketid--,--name--)---------------><

Parameters

socketid
The descriptor of the local socket.
name
Identifies the remote socket.

The format for the name parameter depends on the socket type:

AF_INET sockets (IPv4)
name = “domain portid ipaddress
AF_INET6 sockets (IPv6)
name = “domain portid flowinfo ipaddress scopeid

where

  • The domain value is the decimal number 2 for AF_INET and the decimal number 19 for AF_INET6.
  • The portid value is the port number.
  • The ipaddress value is the IP address of the remote host. It must be an IPv4 address for AF_INET and an IPv6 address for AF_INET6.
  • The flowinfo value must be 0.
  • The scopeid value identifies the interfaces that are applicable for the scope of the address that is specified in the ipaddress field. For a link-local IP address, the scopeid field can specify a link index, which identifies a set of interfaces. For all other scopes, the scopeid field must be set to 0. Setting the scopeid field to 0 indicates that any address type and scope can be specified.

==================================================

https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.3.0/com.ibm.zos.v2r3.hala001/rexx_connect_r.htm