CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Threaded View

  1. #1
    Join Date
    May 2009
    Posts
    12

    Question 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured