![]() |
| | #1 |
| Registered User Join Date: Oct 2007
Posts: 9
| Multiple Connections to a Server Thanks in advance.. |
| niqo is offline | |
| | #2 |
| and the hat of Jobseeking 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. |
| Salem is offline | |
| | #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;
}
Thanks again.. |
| niqo is offline | |
| | #4 |
| and the hat of Jobseeking 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? |
| Salem is offline | |
| | #5 | |||
| Registered User Join Date: Oct 2007
Posts: 9
| Quote:
Quote:
Quote:
| |||
| niqo is offline | |
| | #6 |
| and the hat of Jobseeking 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. |
| Salem is offline | |
| | #7 |
| Registered User Join Date: Oct 2007
Posts: 9
| Arg. Kinda new here.. Hehe Can't find the bug. |
| niqo is offline | |
| | #8 |
| and the hat of Jobseeking Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,706
| What does your memset / strcpy combination do? |
| Salem is offline | |
| | #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 | |
| | #10 |
| and the hat of Jobseeking 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? |
| Salem is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |