你好,我有一个客户端套接字应用程序,它同时连接,在 linux 中有超过 5000 个服务器使用 tcpip,但是当我打开套接字连接时,几乎所有连接,检索错误 Operation now in progress。
这是我的客户端套接字连接代码:我怎样才能同时做好数千个套接字连接???对不起我的英语不好。这是我的代码:
struct sockaddr_in echoserver;
struct sockaddr_in sa_loc;
char aport[64];
int optval = 1;
int sock;
memset(&echoserver, 0, sizeof (echoserver));
echoserver.sin_family = AF_INET;
echoserver.sin_addr.s_addr = inet_addr(server.c_str());
echoserver.sin_port = htons(0);
SOCKET = socket(AF_INET, SOCK_STREAM, 0);
if (SOCKET == -1)
{
iLastError = errno;
strLastError = "Create socket Error : "+string(strerror(errno));
connected = false;
return connected;
}
struct timeval timeouts, timeoutr;
memset(&timeouts, 0, sizeof(timeouts)); // zero timeout struct before use
timeouts.tv_sec = SendTimeOut/1000;
timeouts.tv_usec = 0;
memset(&timeoutr, 0, sizeof(timeoutr)); // zero timeout struct before use
timeoutr.tv_sec = ReceiveTimeOut/1000;
timeoutr.tv_usec = 0;
int sockopt = setsockopt(SOCKET, SOL_SOCKET, SO_SNDTIMEO, &timeouts, sizeof(timeouts));
if (sockopt == -1)
{
printf("%s%s","Set socket Option error : ",strerror(errno));
iLastError = errno;
strLastError = "setsockopt Error : "+string(strerror(errno));
}
sockopt = setsockopt(SOCKET, SOL_SOCKET, SO_RCVTIMEO, &timeoutr, sizeof(timeoutr)); //
if (sockopt == -1)
{
printf("%s%s","Set socket Option error : ",strerror(errno));
iLastError = errno;
strLastError = "setsockopt Error : "+string(strerror(errno));
}
memset(&sa_loc, 0, sizeof(struct sockaddr_in));
sa_loc.sin_family = AF_INET;
sa_loc.sin_port = htons(0); //8000
sa_loc.sin_addr.s_addr = inet_addr(SourceIP.c_str());
int bindid = bind(SOCKET, (struct sockaddr *)&sa_loc, sizeof(sa_loc));
if (bindid !=0)
{
iLastError = errno;
strLastError = "Bind Error : "+string(strerror(errno));
}
int conn = connect(SOCKET, (struct sockaddr *) &echoserver, sizeof(echoserver));
if (conn == -1)
{
strLastError = "Connect Error : "+string(strerror(errno));
connected = false;
}
else {
connected = true;
}
return connected;
wpp