CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2009
    Posts
    3

    Cannot accept more clients using sockets

    Hello, I'm trying a very simple chat application with accepts some clients with ID (not yet implemented) and it works perfectly but for only 1 client.

    Any ideas on how to accept more clients and then send information between them?
    I want to assign an ID to every client so then it would be easy to select one.

    Here goes my code so far using (Dev-C++):

    #include <stdio.h>
    #include <iostream>

    #ifdef _WIN32 // WINDOWS
    #include <winsock2.h> // LibrerĂ*a de sockets
    #include <cstdlib>
    #else // LINUX
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <arpa/inet.h>
    #include <netdb.h>
    #endif

    using namespace std;

    WSADATA wsadata; //Declaramos WSADATA

    struct hostent *host;

    SOCKADDR_IN conexloc;

    SOCKET locsock;

    char Buffer[1024];

    int WSAStarted()
    {
    int wasa = WSAStartup(MAKEWORD(2,0),&wsadata);

    if (wasa != 0)
    {
    printf("Error initializing WSAStartup\n");
    WSACleanup();
    return 1; // Return 1 if something failed
    }
    return 0;
    }

    int DefineSocket()
    {
    // using Socket Stream(TCP)
    locsock = socket(AF_INET/* IP V4 */, SOCK_STREAM, 0);

    if (locsock == INVALID_SOCKET) // if something went wrong…
    {
    printf("Error defining socket\n");
    WSACleanup(); //Limpiamos WSADATA
    return 1; // Return 1 if something failed
    }
    return 0;
    }

    int estructsocket()
    {
    conexloc.sin_family = AF_INET;

    /*
    Define IPv4
    */
    conexloc.sin_addr.s_addr = INADDR_ANY;

    /*
    Define local IP
    */
    conexloc.sin_port = htons(9999);

    // if something failed when binding...
    if (bind(locsock, (sockaddr*)&conexloc, sizeof(conexloc)) == SOCKET_ERROR)
    {
    printf("Error defining socket\n"); //Mostramos un mensaje
    WSACleanup(); //Limpiamos WSADATA
    return 1; // Return 1 if something failed
    }
    else
    {
    if (listen(locsock, 5) == SOCKET_ERROR) // if something failed when listening...
    {
    printf("Error when listening\n");
    WSACleanup(); //clean WSADATA
    return 1; // Return 1 if something failed
    }
    else
    {
    printf("Listening on port 5555 for incoming connections...\n\n");
    return 0; // Everything went okay so we'll return '0'
    }
    }
    }

    void connetion()
    {
    int conm;
    conm=sizeof(struct sockaddr);
    locsock=accept(locsock,(sockaddr*)&conexloc,&conm);
    printf("Stablishing connection... OK");

    while (conm!=0)
    { //while we're connected...
    conm=recv(locsock,Buffer,sizeof(Buffer),0); //we'll receive the data

    if (conm>0)
    { //if the client is still connected...
    printf("Received data:%s",Buffer); //print the received data
    }
    }
    }

    void sockets()
    {
    if((WSAStarted()) == 0)
    { // WSAStarted correctly…
    if((defininingsocket()) == 0)
    { // socket defined correctly…
    if((estructsocket()) == 0)
    {// estructsocket initialized correctly…
    connection(); // Start the connection() procedure
    }
    }
    }
    }

    int main(int argc, char *argv[])
    {
    sockets();
    }

  2. #2
    Join Date
    Aug 2009
    Posts
    10

    Re: Cannot accept more clients using sockets

    you are using a unique loop for receiving data from the clients... look at the code, accept() is called once...
    first, you will need to create an array of SOCKETS to store client connections...
    an acceptor thread must be coded for accepting the clients (it can be done on the main thread)
    but after that, for receiving data, you will need another threads (one-per-client, just one for all clients, which can be a little laggy, or a thread pool, read about Io Completion Ports too)

    it just depends on what you will be doing next

  3. #3
    Join Date
    Aug 2009
    Posts
    3

    Re: Cannot accept more clients using sockets

    Hello, before posting this doubt I knew that the way to do it is by storing the clients in an array, but.. How can I store the clients in that array? I've never worked before with sockets so I don't know exacty what should be stored.. the IP?? if the answer is yes.. How can I capture that IP? if not.. what do I have to do? with a sample modification of my code or pseudo-code it'll be great for me to understand.

    The server itself will be laggy so I think I won't use just 1 thread for all clients. How can I make it with 1 thread for each client?

    Thanks for your answer. You've given me some ideas but unfortunately I don't know how to implement them.

  4. #4
    Join Date
    Aug 2009
    Posts
    10

    Re: Cannot accept more clients using sockets

    this will help you:
    http://www.codeproject.com/KB/IP/winsockintro03.aspx

    but, keep in mind: a lot of clients connected will create a lot of threads, which can be a little problem. once you have understood how the whole system works, try doing it over an IOCP server (no, it isnt so simple to do :P)

  5. #5
    Join Date
    Aug 2009
    Posts
    3

    Re: Cannot accept more clients using sockets

    Thank you very much dude, I'll take a look at that site to see what can I learn about sockets.
    I think I'll re-use a code that I've found around google. As you say... it's not simple thing to do hehe. So I think I can study it without trying to code something that is already done.

    Anyways... if someone wants to make a contribution helping me to code it I would appreciate it very much.

    Regards.

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