C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 03-31-2008, 03:27 AM   #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..
niqo is offline   Reply With Quote
Old 03-31-2008, 12:14 PM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,706
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.

Salem is offline   Reply With Quote
Old 03-31-2008, 06:57 PM   #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..
niqo is offline   Reply With Quote
Old 04-01-2008, 10:14 AM   #4
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,706
> 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.

Salem is offline   Reply With Quote
Old 04-01-2008, 12:04 PM   #5
Registered User
 
Join Date: Oct 2007
Posts: 9
Quote:
Shouldn't you be passing the remote socket.
Uhm, I guess I'm passing the remote socket.
Quote:
And some other bugs in rcv().
Can you specify the bugs so I may fix it?
Quote:
A for loop in the server perhaps?
Okay I'll try that. Thanks..
niqo is offline   Reply With Quote
Old 04-01-2008, 02:56 PM   #6
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,706
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.

Salem is offline   Reply With Quote
Old 04-01-2008, 04:24 PM   #7
Registered User
 
Join Date: Oct 2007
Posts: 9
Arg. Kinda new here.. Hehe Can't find the bug.
niqo is offline   Reply With Quote
Old 04-02-2008, 09:02 AM   #8
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,706
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.

Salem is offline   Reply With Quote
Old 04-02-2008, 03:45 PM   #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.
niqo is offline   Reply With Quote
Old 04-03-2008, 09:36 AM   #10
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,706
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.

Salem is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple clients to "internet" server Zarniwoop C Programming 2 10-11-2008 11:04 PM
MISO Soup: Multiple Clients and one Server doubleanti Networking/Device Communication 2 07-24-2007 02:29 AM
multiple connections via sockets cope Networking/Device Communication 7 06-07-2007 10:35 AM
socket question Unregistered C Programming 3 07-19-2002 01:54 PM
Multiple Client Connections (TNT) Windows Programming 1 04-06-2002 11:04 PM


All times are GMT -6. The time now is 11:43 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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