|
-
May 16th, 2009, 10:45 PM
#1
Problem with sockets
Hi, im having a problem, and its driving me mad xD
Can anyone help me pls? 
(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;
}
Last edited by stratoforce; May 16th, 2009 at 11:04 PM.
-
May 17th, 2009, 04:45 PM
#2
Re: Problem with sockets
Sorry for the double post , but the scenario changed now, and maybe the ones who
already read the first post will not read it again if i edit.
I made a small client, to test my small server posted before.
It seems the code is fine.......
The thing is that my server is a web server.
And it should work with firefox. (wich doesn't)
Im think im not understanding how firefox manages its connections.
I heard that there were a limit of 2 connections per server in old browsers, but that changed now.
The limit is 6 by default on firefox.
(And it doesnt explain why my server works when i start new threads, the problem only shows when
i try to manually queue the requests)
Any ideas?
And sorry again, pls understand my need to double post.
-----------------------------NEW SCENARIO(again)--------------------------
Ok, i have been doing tests, and i realized it works as it should when i try to download different files
(or make different requests)
I was doing test with 2 files i had bookmarked in the server and everytime i tested my server i opened one those 2 files in many tabs.... bad choice, i know. 
It seems that firefox wont do a identical request twice, it seems that it waits for the first request to succed or fail before trying again.
That would explain why downloading the same file simultaneously works, but manually queing the requests doesn't. (since when i queue the requests, i accept
the connection and forget about it until the time to process the request comes, so the connection is not failing but its not succeding either)
Is this what really happens? Am i right? If anyone had the same situation and knows anything about it let me know pls.
Last edited by stratoforce; May 18th, 2009 at 01:50 AM.
Reason: New scenario again, but i dont want to make a triple post :P
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|