Reputation: 1
i've the following code which runs just fine under windows env while in linux using the same code it doesn't (besides few changes of libs) .the select function does not respond to new connections.
The relevant code is as follows:
struct SocketState
{
int id; // Socket handle
int recv; // Receiving?
int send; // Sending?
int sendSubType; // Sending sub-type
char buffer[128];
int len;
int authenticate;
char userName[10];
};
struct SocketState sockets[MAX_SOCKETS]={0};
int socketsCount = 0;
int main()
{
int listenSocket = socket(PF_INET, SOCK_STREAM, 0);
if (-1 == listenSocket)
{
return 0 ;
}
struct sockaddr_in serverService;
serverService.sin_family = AF_INET;
serverService.sin_addr.s_addr = htonl(INADDR_ANY);
serverService.sin_port = htons(TIME_PORT);
if (-1 == bind(listenSocket, (struct sockaddr*)&serverService, sizeof(serverService)))
{
perror("Couldn't bind socket");
return -1;
}
if (-1 == listen(listenSocket, 10))
{
perror("Couldn't listen to port");
}
addSocket(listenSocket, LISTEN);
while (true)
{
fd_set waitRecv;
FD_ZERO(&waitRecv);
for (int i = 0; i < MAX_SOCKETS; i++)
{
if ((sockets[i].recv == LISTEN) || (sockets[i].recv == RECEIVE))
FD_SET(sockets[i].id, &waitRecv);
}
fd_set waitSend;
FD_ZERO(&waitSend);
for (int i = 0; i < MAX_SOCKETS; i++)
{
if (sockets[i].send == SEND)
FD_SET(sockets[i].id, &waitSend);
}
int nfd;
nfd = select(0, &waitRecv, &waitSend, NULL, NULL);
if (nfd == -1)
{
return 0 ;
}
}
}
Upvotes: 0
Views: 1619
Reputation: 98378
You are passing 0
as the first argument to select
. That is wrong. Probably in Windows this parameter is not used, but in linux it have to be set correctly.
It has to be set to the number of the higher fd
plus 1.
Upvotes: 2