Tal Darom
Tal Darom

Reputation: 1409

detecting incoming socket connection

I'm writing a server application and having problems with selecting on the socket. The server side select always returns 0, even when the client tries to connect. when I comment the select and go straight to the accept line everything works (but my application is blocked)

server code (in C):

int sockfd = socket(AF_INET, SOCK_STREAM, 0);
memset((char *) &serv_addr, 0, sizeof (serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(System_->commParams.port);
bind(sockfd, (struct sockaddr *) &serv_addr, sizeof (serv_addr));
fd_set fds;
timeval tv;
FD_ZERO(&fds);
FD_SET(sockfd, &fds);
int retval = 0;
tv.tv_sec = 1;
tv.tv_usec = 0;
listen(sockfd, 5);
while (retval == 0)
{
   retval = select(1, &fds, NULL, NULL, &tv);
   tv.tv_sec = 1;
   tv.tv_usec = 0;
   FD_ZERO(&fds);
   FD_SET(sockfd, &fds);        
}
unsigned int clilen = sizeof (cli_addr);
int newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);

client code ((in python):

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((RemoteIP, Port))

Upvotes: 0

Views: 304

Answers (2)

wildplasser
wildplasser

Reputation: 44250

 retval = select(1, &fds, NULL, NULL, &tv);

1 is a bit low. Try:

 retval = select(sockfd+1, &fds, NULL, NULL, &tv);

Upvotes: 0

cnicutar
cnicutar

Reputation: 182639

retval = select(1, &fds, NULL, NULL, &tv);

The first argument to select should be sockfd + 1.

Upvotes: 5

Related Questions