stratoforce
May 16th, 2009, 10:45 PM
Hi, im having a problem, and its driving me mad xD
Can anyone help me pls? :D
(ANSI C + winsock 2)
Im having a problem with a server im coding.
I have isolated the problem in this small program, i know it has no practical use and don't to any error checking, but
its just a small program that has the same problem my real program is having.
(For easy reading + simplicity)
I need this code to work, because i do something similar when the max number of simultaneous
downloads is reached (That number is chosen by the server admin).
What i do is accept the connection and put the socket descriptor in a dinamic queue.
The code:
The problem with this code is that it only accepts 2 simultaneous connections, is this supossed to be normal behavior?
Why does it accept all the connections i want when i pass the socket as a parameter to a thread and start it?
int setUp(SOCKET * aSocket, struct sockaddr_in * server, int port);
int main(void)
{
SOCKET listenSocket;
struct sockaddr_in server;
WORD wVersionRequested;
WSADATA wsaData;
int wsaerr;
SOCKET socketsArray[20];
int socketNumber = 0;
wVersionRequested = MAKEWORD(2,2);
wsaerr = WSAStartup(wVersionRequested, &wsaData);
//Set up the socket for listening
setUp(&listenSocket, &server, 80);
while(1)
{
socketsArray[socketNumber] = accept(listenSocket, NULL,NULL);
socketNumber++;
printf("Accepted connections until now: %d\n", socketNumber);
}
}
int setUp(SOCKET * aSocket, struct sockaddr_in * server, int port)
{
int yes = 1;
*aSocket = socket(AF_INET, SOCK_STREAM, 0);
setsockopt(*aSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes));
server->sin_family = AF_INET;
server->sin_addr.s_addr = INADDR_ANY;
server->sin_port = htons(port);
bind(*aSocket, (struct sockaddr*)server, sizeof(*server));
listen(*aSocket, 5);
return 0;
}
Can anyone help me pls? :D
(ANSI C + winsock 2)
Im having a problem with a server im coding.
I have isolated the problem in this small program, i know it has no practical use and don't to any error checking, but
its just a small program that has the same problem my real program is having.
(For easy reading + simplicity)
I need this code to work, because i do something similar when the max number of simultaneous
downloads is reached (That number is chosen by the server admin).
What i do is accept the connection and put the socket descriptor in a dinamic queue.
The code:
The problem with this code is that it only accepts 2 simultaneous connections, is this supossed to be normal behavior?
Why does it accept all the connections i want when i pass the socket as a parameter to a thread and start it?
int setUp(SOCKET * aSocket, struct sockaddr_in * server, int port);
int main(void)
{
SOCKET listenSocket;
struct sockaddr_in server;
WORD wVersionRequested;
WSADATA wsaData;
int wsaerr;
SOCKET socketsArray[20];
int socketNumber = 0;
wVersionRequested = MAKEWORD(2,2);
wsaerr = WSAStartup(wVersionRequested, &wsaData);
//Set up the socket for listening
setUp(&listenSocket, &server, 80);
while(1)
{
socketsArray[socketNumber] = accept(listenSocket, NULL,NULL);
socketNumber++;
printf("Accepted connections until now: %d\n", socketNumber);
}
}
int setUp(SOCKET * aSocket, struct sockaddr_in * server, int port)
{
int yes = 1;
*aSocket = socket(AF_INET, SOCK_STREAM, 0);
setsockopt(*aSocket, SOL_SOCKET, SO_REUSEADDR, (char *)&yes, sizeof(yes));
server->sin_family = AF_INET;
server->sin_addr.s_addr = INADDR_ANY;
server->sin_port = htons(port);
bind(*aSocket, (struct sockaddr*)server, sizeof(*server));
listen(*aSocket, 5);
return 0;
}