There are two other points to note about transplantation
problem:
1. The getsockopt implementation from Berkeley returns 0, and the error waiting for processing is returned in the variable errno; But Solaris will have getsockopt return -1 and errno set as the error to be handled; We have to deal with both cases;
2. Portability issues in platforms with different socket implementations when dealing with non-blocking connect. First, it is possible that the connection has already been established and the other party’s data has already arrived before the select call. In this case, when the connection succeeds, the socket will be both readable and writable. This is the same as if the connection failed. At this point we also have to read the error value through getsockopt
The simple code is as follows, for reference only:
BOOL ConnectServer(int robot_num)
{
char errnum[10] = {0};
int err = -1;
int error = -1;
int errlen = sizeof(error);
int no1 = robot_num;
fd_set wset;
struct timeval tm;
tm.tv_sec = 5;
tm.tv_usec = 0;
FD_ZERO(& wset);
FD_SET(sHost[no1],& wset);
// set server address
servaddr. sin_family =AF_INET;
servAddr. Sin_addr. S_addr = inet_addr (192.168.1.104, “”); //& IP [no1-1][0]
servaddr.sin_port = htons(6665); //atoi(& The port [no1-1] [0])
// connect server
retVal = connect (sHost [no], (LPSOCKADDR) & amp; servAddr, sizeof(servAddr));
err = WSAGetLastError();
if(SOCKET_ERROR == retVal)
{
{
if(WSAEWOULDBLOCK == err || WSAEINVAL == err) // there is no more non-blocking operation
{
if(select(0,NULL,&); wset,NULL,& tm) < = 0){// Error timeout or connection failure; Return 0, representing timeout
printf(“error code is %s\n”,strerror(errno));
closesocket(sHost[no1]);
}
if( ! FD_ISSET(sHost[no1],& Wset)){// If the value in the set, non-zero;
printf(“error code is %s\n”,strerror(errno));
closesocket(sHost[no1]);
}
getsockopt(sHost[no1],SOL_SOCKET,SO_ERROR,(char*)& error,& errlen);
if (error ! = 0)
{
printf(“Connect failed! \n”);
bConnecting = FALSE;
}else{
printf(“Connect successfully! \n”);
bConnecting = TRUE;
}
}else if (WSAEISCONN == err)// connection complete
{
printf(” connection complete \n”);
}else if (WSAENOTCONN == err)// connection not complete
{
printf(“socket is not connected \n”);
} else// for other reasons, the connection fails
{
printf (” connect err = % d \ n “, err); // trying an already in progress operation on a non-blocking socket
printf(” other reason, connection failed \n”);
}
}
if (retVal == 0)// successfully Connect
{
printf(“Connect successfully! \n”);
bConnecting = TRUE;
}
return bConnecting;
}
Read More:
- What should be paid attention to in socket programming — bind socket error: address already in use
- Errno in Linux Programming
- [Windows] Socket Server Failed to bind, error 10048
- pthread_ Introduction and application of join function
- Socket Error 104 bug
- Python TCP socket programming: send returns broken pipe error?
- socket error 10035
- mysql problem solving: mysqladmin: connect to server at’localhost’ failed
- A solution to the error of asynchronous socket error 10053 in C + + Builder
- Python appears socket.gaierror : [errno 11004] getaddrinfo failed error
- The mongoDB service failed to start (exception: connect failed)
- node.js Server MySQL database connection timeout (error: connect etimeout)
- RTMP_Connect0, failed to connect socket. 110 (Connection timed out)
- filezilla Failed to create listen socket on port 21 for IPv4 solution
- Socket error 10053
- MongoNetworkError: failed to connect to server [localhost:27017]
- NBU recovery report error socket read failed: errno = 119-System call timed out
- Linux getsockopt SO_ERROR values (errno.h)
- Deployment project websocket failed: error during websocket Handshake: unexpected response code: 400
- Attribute