Home > Software engineering >  TCP asynchronous communication program, how to determine whether the connect link success
TCP asynchronous communication program, how to determine whether the connect link success

Time:11-23

The client connect to the server of the return value is 10035, so I write like this:

 res=connect (sockCli, (SOCKADDR *) & amp; AddrSer, sizeof (SOCKADDR)); 
If (res==10035 | | res==0) {
MessageBox (NULL, "the client connect to the server success", "warning", 0). }
The else
Failed to connect to the server MessageBox (NULL, "client", "notice", 0).
break;


But the result is the client the pop-up message box twice, the first is the client connect to the server success, the second is the client connect to the server failure, why is this so?

CodePudding user response:

The connect
The Windows Sockets connect function establishes a connection to a specifed socket.

Int the connect (
The SOCKET s,
Const struct sockaddr FAR * name,
Int namelen
);

The Parameters
S
[in] A descriptor identifying an unconnected socket.
The name
[in] The name of The socket to connect to.
Namelen
[in] The length of The name parameter.
Few
The connect function is 2 create a connection to The specified destination. If The socket, s, is unbound, unique values are assigned to The local association by The system, and The socket is marked as bound.

For the connection - oriented sockets (For example, the type SOCK_STREAM), an active connection is initiated to the foreign host using the name (an address in the name space of the socket; For a detailed description, both please see bind and SOCKADDR). When the socket call of successfully, the socket is ready to send/receive data. If the address member of the structure specified by the name parameter is all zeroes, connect will return the error WSAEADDRNOTAVAIL. Any attempt to re - connect an active connection will fail with the error code WSAEISCONN.

For connection-oriented, nonblocking sockets, it is often not possible to complete the connection immediately. In such a case, this function returns the error WSAEWOULDBLOCK. However, the operation proceeds. When the success or failure outcome becomes known, it may be reported in one of several ways depending on how the client registers for notification. If the client uses the select function, success is reported in the writefds set and failure is reported in the exceptfds set. If the client uses the functions WSAAsyncSelect or WSAEventSelect, the notification is announced with FD_CONNECT and the error code associated with the FD_CONNECT indicates either success or a specific reason for failure

For a connectionless socket (for example, type SOCK_DGRAM), the operation performed by connect is merely to establish a default destination address that will be used on subsequent send/WSASend and recv/WSARecv calls. Any datagrams received from an address other than the destination address specified will be discarded. If the address member of the structure specifed by name is all zeroes, the socket will be "dis-connected." Then, the default remote address will be indeterminate, so send/WSASend and recv/WSARecv calls will return the error code WSAENOTCONN. However, sendto/WSASendTo and recvfrom/WSARecvFrom can still be used. The default destination can be changed by simply calling connect again, even if the socket is already connected. Any datagrams queued for receipt are discarded if name is different from the previous connect.

For connectionless sockets, name can indicate any valid address, o a broadcast address, or, to connect to a broadcast address, a socket must use the setsockopt to enable the SO_BROADCAST option. Otherwise, the connect will fail with the error code WSAEACCES.

When a connection between sockets is broken, the sockets should be discarded and recreated. When a problem develops on a connected socket, the application must discard and recreate the men sockets in order to return to a stable point.

Windows CE: Windows CE does not support the WSAEINTR error value.

For IrSocket implementation:

The Af_irda. H file must be explicitly included.
If WSAENETDOWN is returned, the an existing IrDA connection was detected at the media access level.
If WSAEADDRINUSE is returned, the active connections to a device with a company's address exist.
If WSAEINPROGRESS is returned, IAS name resolution failed because another IAS query is in progress. Retry the operation at one second intervals.
If WSAEISCONN is returned, the socket is already connected or an exclusive/multiplexed mode change failed.
IrSockets implements the connect function with the addresses of the form sockaddr_irda. Typically, a client application will create a socket with the socket function, scan the immediate vicinity for IrDA devices have the IRLMP_ENUMDEVICES socket option, choose a device from the returned list, form an address, and call the connect. There is no difference in blocking and non - blocking semantics.

The Return Values
If no error occurs, connect returns zero. Otherwise, it returns SOCKET_ERROR, and a specific error code can be retrieved by calling WSAGetLastError.

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related