Server code:
#include <iostream>
#include <winsock2.h>
#include <ws2tcpip.h>#
include <string>
using namespace std;
#pragma comment (lib, "Ws2_32.lib")
#define IP_ADDRESS "192.168.56.1"
#define DEFAULT_PORT "3504"
#define DEFAULT_BUFLEN 2024
const char option_value = 1;
//Function Prototypes
DWORD WINAPI ProcessClient(LPVOID Parameter);
int main();
WSADATA wsaData;
struct addrinfo hints;
struct addrinfo *server = NULL;
SOCKET server_socket = INVALID_SOCKET;
SOCKET client_socket= INVALID_SOCKET;
int num_clients = 0;
string msg = "";
char tempmsg[DEFAULT_BUFLEN] = "";
DWORD WINAPI ProcessClient(LPVOID Parameter)
{
//Session
cout << "Client #" << num_clients << " Accepted" << endl;
num_clients++;
while (1)
{
memset(tempmsg, 0, DEFAULT_BUFLEN);
if (client_socket != 0)
{
int iResult = recv(client_socket, tempmsg, DEFAULT_BUFLEN, 0);
if (iResult > 0)
{
if (strcmp("", tempmsg))
msg = tempmsg;
cout << msg.c_str() << endl << endl;
}
}
}
return 0;
}
int main()
{
//Initialize Winsock
cout << "Intializing Winsock..." << endl;
WSAStartup(MAKEWORD(2, 2), &wsaData);
//Setup hints
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
//Setup Server
cout << "Setting up server..." << endl;
getaddrinfo(static_cast<LPCTSTR>(IP_ADDRESS), DEFAULT_PORT, &hints, &server);
//Create a listening socket for connecting to server
cout << "Creating server socket..." << endl;
server_socket = socket(server->ai_family, server->ai_socktype, server->ai_protocol);
//Setup socket options
setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &option_value, sizeof(int)); //Make it possible to re-bind to a port that was used within the last 2 minutes
setsockopt(server_socket, IPPROTO_TCP, TCP_NODELAY, &option_value, sizeof(int)); //Used for interactive programs
//Assign an address to the server socket.
cout << "Binding socket..." << endl;
bind(server_socket, server->ai_addr, (int)server->ai_addrlen);
//Listen for incoming connections.
cout << "Listening..." << endl;
listen(server_socket, SOMAXCONN);
while (1)
{
while (client_socket = accept(server_socket, NULL, NULL))
{
DWORD threadID;
CreateThread(NULL, 0, ProcessClient, (LPVOID)client_socket, 0, &threadID);
}
}
//Close listening socket
closesocket(server_socket);
//Close client socket
closesocket(client_socket);
//Clean up Winsock
WSACleanup();
cout << "Program has ended successfully" << endl;
system("pause");
return 0;
}
The client code isnt necessary, as the problem lies in here.In attempt two to try and get it working, I tried making the client_socket an array. Although it sort of works, it works in a very bad way. For example, if I have client 1 and client 2 log on afterwards. The server can receive a message from client 1 but cant from client two until it has received a message from client 1, and vice versa. As though it gets stuck in recv() in the middle of a for loop in the thread. I have no idea on how to fix any of this and any help will be appreciated. Thanks in advance.