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..