有两个套接字服务器,一个是主服务器,它不总是打开,一个是备份服务器。
我的程序将尝试使用非块连接连接主服务器(以便可以应用超时值),如果失败,它将使用阻塞连接连接备份服务器。
然而,第二个连接函数将在大多数时间内返回“无效参数”错误代码:
#define SERVER_URL "example.com"
#define SERVER_PORT_PRIMARY "1234"
#define SERVER_PORT_BACKUP "5678"
struct addrinfo *result = NULL;
struct addrinfo hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
if (getaddrinfo(SERVER_URL , SERVER_PORT_PRIMARY , &hints, &result) != 0) {
WSACleanup();
return;
}
SOCKET socketClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (socketClient == SOCKET_ERROR){
WSACleanup();
return;
}
//set the socket in non-blocking
unsigned long iMode = 1;
iResult = ioctlsocket(socketClient, FIONBIO, &iMode);
if (iResult != NO_ERROR){
closesocket(socketClient);
WSACleanup();
return;
}
if (connect(socketClient, result->ai_addr, (int)result->ai_addrlen) == SOCKET_ERROR){
if (WSAGetLastError() != WSAEWOULDBLOCK){
closesocket(socketClient);
WSACleanup();
return;
}
}
//switch it back to blocking socket
iMode = 0;
iResult = ioctlsocket(socketClient, FIONBIO, &iMode);
if (iResult != NO_ERROR){
closesocket(socketClient);
WSACleanup();
return;
}
fd_set Write, Err;
FD_ZERO(&Write);
FD_ZERO(&Err);
FD_SET(socketClient, &Write);
FD_SET(socketClient, &Err);
TIMEVAL Timeout;
Timeout.tv_sec = 10;
Timeout.tv_usec = 0;
select(0, NULL, &Write, &Err, &Timeout);
if (FD_ISSET(socketClient, &Write) == false){
//unable to connect to primary server within 10s, try to connect backup server
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
if (getaddrinfo(SERVER_URL , SERVER_PORT_BACKUP, &hints, &result) != 0) {
closesocket(socketClient);
WSACleanup();
return;
}
iResult = connect(socketClient, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR){
int a = WSAGetLastError(); ///<----Problem here, a == WSAEINVAL (Invalid argument)
closesocket(socketClient);
WSACleanup();
return;
}
}
正如我对上面的代码所做的评论,第二个“connect”调用将在大多数时间返回SOCKET_ERROR,WSAGetLastError()返回WSAEINVAL(无效参数)。
如果我删除非阻塞代码,它将连接没有错误。那我的代码出了什么问题?
答案 0 :(得分:1)
当connect()
仍然忙于连接时,您必须将套接字置于非阻塞模式。仅当select()
返回connect()
时才调用WSAEWOULDBLOCK
,您还需要检查select()
的返回值。您还泄漏了getaddrinfo()
返回的内存。
尝试更像这样的东西:
int connectTo(SOCKET s, const char *host, const char *port)
{
struct addrinfo *result = NULL;
struct addrinfo hints;
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
int ret = getaddrinfo(host, port, &hints, &result);
if (ret != 0) {
return ret;
}
if (connect(socketClient, result->ai_addr, result->ai_addrlen) != SOCKET_ERROR) {
freeaddrinfo(result);
return 0;
}
ret = WSAGetLastError();
freeaddrinfo(result);
if (ret != WSAEWOULDBLOCK) {
return ret;
}
fd_set Write, Err;
FD_ZERO(&Write);
FD_ZERO(&Err);
FD_SET(s, &Write);
FD_SET(s, &Err);
TIMEVAL Timeout;
Timeout.tv_sec = 10;
Timeout.tv_usec = 0;
ret = select(0, NULL, &Write, &Err, &Timeout);
if (ret == SOCKET_ERROR) {
return WSAGetLastError();
}
if (ret == 0) {
return WSAETIMEDOUT;
}
if (FD_ISSET(s, &Err)) {
u_long err;
if (getsockopt(s, SOL_SOCKET, SO_ERROR, (char*)&err, sizeof(err)) == SOCKET_ERROR) {
return WSAGetLastError();
}
return (int) err;
}
return 0;
}
然后你可以这样做:
#define SERVER_URL "example.com"
#define SERVER_PORT_PRIMARY "1234"
#define SERVER_PORT_BACKUP "5678"
SOCKET socketClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (socketClient == SOCKET_ERROR) {
WSACleanup();
return;
}
//set the socket in non-blocking
u_long iMode = 1;
iResult = ioctlsocket(socketClient, FIONBIO, &iMode);
if (iResult == SOCKET_ERROR) {
closesocket(socketClient);
WSACleanup();
return;
}
if (connectTo(socketClient, SERVER_URL, SERVER_PORT_PRIMARY) != 0) {
if (connectTo(socketClient, SERVER_URL, SERVER_PORT_BACKUP) != 0) {
closesocket(socketClient);
WSACleanup();
return;
}
}
//switch it back to blocking socket
iMode = 0;
iResult = ioctlsocket(socketClient, FIONBIO, &iMode);
if (iResult == SOCKET_ERROR) {
closesocket(socketClient);
WSACleanup();
return;
}
// communicate with the server as needed ...