Multiple Connections to a Server

This is a discussion on Multiple Connections to a Server within the Networking/Device Communication forums, part of the General Programming Boards category; Hi, I know a little about network programming. Though I wasn't sure if the codes of a simple server-client program ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    9

    Multiple Connections to a Server

    Hi, I know a little about network programming. Though I wasn't sure if the codes of a simple server-client program wouldl be the same as the codes of multiple clients connecting to a server. I'm thinking about creating a chat program wherein there is one server and several clients connecting to it. Any ideas where I can start?

    Thanks in advance..

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Yes, that's pretty much it.
    You just need to keep listening to the accept port to gather additional connections.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Hmm. I got this simple code. I used multithreading.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <winsock.h>
    
    DWORD WINAPI rcv(LPVOID lpParam)
    {
        SOCKET sock = (SOCKET) lpParam;
        char buffer[260];
        char temp[260];
        
        while (1)
        {
            int bytesRcv = recv(sock, buffer, sizeof(buffer), 0);
            
            if (bytesRcv == 0 || bytesRcv == SOCKET_ERROR) 
            {
                closesocket(sock);
                ExitThread(0);
            }
            else
            {
                memset(buffer, 0, 260);
                strcpy(temp, buffer);
                send(sock, temp, sizeof(temp), 0);
            }     
        }
    }
    
    int main(int argc, char *argv[])
    {
        WSADATA wsadata;
        if (WSAStartup(MAKEWORD(2,0), &wsadata) != 0) return 0;
        
        SOCKET hSocket, hRemoteSocket;
        hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (hSocket == INVALID_SOCKET) return 0;
        
        SOCKADDR_IN server, client;
        server.sin_family = AF_INET;
        server.sin_addr.s_addr = INADDR_ANY;
        server.sin_port = htons(4567);
        
        if (bind(hSocket, (SOCKADDR*)&server, sizeof(server)) != 0) return 0;
        if (listen(hSocket, SOMAXCONN) != 0) return 0;
        
        int len = sizeof(client);
        DWORD thread;
        
        while (1)
        {
            hRemoteSocket = accept(hSocket, (SOCKADDR*) &client, &len);
            if (hRemoteSocket == INVALID_SOCKET) return 0;
            
            CreateThread(NULL, 0, rcv, (LPVOID)hRemoteSocket, 0, &thread);
        }
        
        closesocket(hSocket);
        WSACleanup();
        	
        return 0;
    }
    My problem now is how to get the message sent by one client to all the clients connected to the server. Any ideas?
    Thanks again..

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    > CreateThread(NULL, 0, rcv, (LPVOID)hRemoteSocket, 0, &thread);
    Shouldn't you be passing the remote socket.

    And some other bugs in rcv().

    > My problem now is how to get the message sent by one client to all the clients connected to the server
    A for loop in the server perhaps?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Shouldn't you be passing the remote socket.
    Uhm, I guess I'm passing the remote socket.
    And some other bugs in rcv().
    Can you specify the bugs so I may fix it?
    A for loop in the server perhaps?
    Okay I'll try that. Thanks..

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    I could, but it ought to be pretty obvious if you actually run the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Arg. Kinda new here.. Hehe Can't find the bug.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    What does your memset / strcpy combination do?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    9
    Hmm. I just put that cuase cause last time when I ran the program (w/o the memset), if first input 'abcdefg' then input 'bed' the output became 'beddefg'.. That's why I put memset.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,854
    Does strcpy copy from temp to buffer, or from buffer to temp?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple clients to "internet" server
    By Zarniwoop in forum C Programming
    Replies: 2
    Last Post: 10-12-2008, 12:04 AM
  2. MISO Soup: Multiple Clients and one Server
    By doubleanti in forum Networking/Device Communication
    Replies: 2
    Last Post: 07-24-2007, 03:29 AM
  3. multiple connections via sockets
    By cope in forum Networking/Device Communication
    Replies: 7
    Last Post: 06-07-2007, 11:35 AM
  4. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 02:54 PM
  5. Multiple Client Connections
    By (TNT) in forum Windows Programming
    Replies: 1
    Last Post: 04-06-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21