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
|
WSADATA WSAData;
SOCKET server, client;
SOCKADDR_IN serverAddr, clientAddr;
WSAStartup(MAKEWORD(2, 0), &WSAData);
server = socket(AF_INET, SOCK_STREAM,0);
serverAddr.sin_addr.s_addr = htons(INADDR_ANY);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(13673);
bind(server, (SOCKADDR *)&serverAddr, sizeof(serverAddr));
listen(server, SOMAXCONN);
cout << "Listening for incoming connections..." << endl;
char buffer[1024];
int clientAddrSize = sizeof(clientAddr);
if ((client = accept(server, (SOCKADDR *)&clientAddr, &clientAddrSize)) != INVALID_SOCKET)
{
cout << "Client connected!" << endl;
recv(client, buffer, sizeof(buffer), 0);
cout << "Client says: " << buffer << endl;
memset(buffer, 0, sizeof(buffer));
closesocket(client);
cout << "Client disconnected." << endl;
}
getchar();
|