1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| void ConnexionTCP::RunServer()
{
// create a listening socket
SOCKET listening = createSocket();
if (listening == INVALID_SOCKET)
{
cout << " could not create the socket, Err #" << WSAGetLastError() << endl;
}
else
{
cout << "Server starts at the IP Address: " << m_ipAddress << " and on the port: " << m_port << endl;
}
while (SOCKET client = waitForConnection(listening))
{
_beginthread(&(ConnexionTCP::Thread), 0, &client);
}
}
void ConnexionTCP::Thread(void * data)
{
SOCKET* client = (SOCKET*)data;
SOCKET Newconnection = *client;
char buf[MAX_BUFFER_SIZE];
while (true)
{
int byteReceived = 0;
do
{
ZeroMemory(buf, MAX_BUFFER_SIZE);
byteReceived = recv(Newconnection, buf, MAX_BUFFER_SIZE, 0);
if (byteReceived > 0)
{
send(Newconnection, buf, byteReceived + 1, 0);
cout << string(buf, 0, byteReceived) << endl;
}
} while (byteReceived > 0);
}
closesocket(Newconnection);
} |
Partager