Multiple Connections to a Server [Archive] - C Board

PDA

View Full Version : Multiple Connections to a Server


niqo
03-31-2008, 03:27 AM
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.. :D

Salem
03-31-2008, 12:14 PM
Yes, that's pretty much it.
You just need to keep listening to the accept port to gather additional connections.

niqo
03-31-2008, 06:57 PM
Hmm. I got this simple code. I used multithreading.


#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? :confused:
Thanks again.. ;)

Salem
04-01-2008, 10:14 AM
> 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?

niqo
04-01-2008, 12:04 PM
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.. ;)

Salem
04-01-2008, 02:56 PM
I could, but it ought to be pretty obvious if you actually run the code.

niqo
04-01-2008, 04:24 PM
Arg. Kinda new here.. Hehe Can't find the bug. :(

Salem
04-02-2008, 09:02 AM
What does your memset / strcpy combination do?

niqo
04-02-2008, 03:45 PM
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.

Salem
04-03-2008, 09:36 AM
Does strcpy copy from temp to buffer, or from buffer to temp?