// Resolve the server address and port
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if ( iResult != 0 )
{
cout << "getaddrinfo failed: " << iResult << endl;
WSACleanup();
return 1;
}
// Create a SOCKET for connecting to server
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET)
{
while (num_players < 2)//if there are less than 2 players
{//keep on looping until 2 clients/players connect to the server
s[num_players] = accept(ListenSocket, NULL, NULL);
else
{
cout << "Player " << num_players+ 1 << " has joined!\n";
num_players++;
}
}
cout << "Starting Game!\n";//Start game since there are 2 clients/players
// No longer need server socket
closesocket(ListenSocket);
// Receive until the peer shuts down the connection
while (true) //while loop to keep looping to receive data from the 2 players/clients
{
iResult = recv(s[0], (char *)&RS, sizeof(reqset), 0 );//receive data from client #1
//data received is stored inside RS and is of the struct format "reqset"
if (iResult > 0)
{ //print out the data received by client's socket #1
cout << "Bytes received by s[0] " << iResult << endl;
cout << "s[0] Player X coordinate as received from client is " << RS.playerX << endl;
cout << "s[0] Player Y coordinate as received from client is " << RS.playerY << endl;
//Process the player's data which is stored inside the struct instance RS of type "reqset"
ProcessData(RS,wParam);
//then send the processed data over to client #2
iSendResult = send( s[1], (char *)&RS, sizeof(reqset), 0 );
if (iSendResult == SOCKET_ERROR)
{
cout << "send failed: " << WSAGetLastError() << endl;
closesocket(s[1]);
WSACleanup();
return 1;
}
cout << "Bytes sent " << iSendResult << endl;
//NOTE: You have just received data from client #1, process the data and then
//send it over to client #2
}
else if (iResult == 0)
cout << "Connection closing..." << endl;
else {
cout << "recv failed: " << WSAGetLastError() << endl;
closesocket(s[1]);
WSACleanup();
return 1;
}
int i2Result = 0;
i2Result = recv(s[1], (char *)&RS, sizeof(reqset), 0 );//receive data from client #2
//data received is stored inside RS and is of the struct format "reqset"
if (i2Result > 0)
{
//print out the data received by client's socket #2
cout << "Bytes received by s[1] " << i2Result << endl;
cout << "s[1] Player X coordinate as received from client is " << RS.playerX << endl;
cout << "s[1] Player Y coordinate as received from client is " << RS.playerY << endl;
//Process the player's data which is stored inside the struct instance RS of type "reqset"
ProcessData(RS,wParam);
//then send the processed data over to client #1
iSendResult = send( s[0], (char *)&RS, sizeof(reqset), 0 );
if (iSendResult == SOCKET_ERROR)
{
cout << "send failed: " << WSAGetLastError() << endl;
closesocket(s[0]);
WSACleanup();
return 1;
}
cout << "Bytes sent: " << iSendResult << endl;
//NOTE: You have just received data from client #1, process the data and then
//send it over to client #2
}
else if (i2Result == 0)
cout << "Connection closing...\n";
else {
cout << "recv failed: " << WSAGetLastError() << endl;
closesocket(s[0]);
WSACleanup();
return 1;
}
}//end of while loop
// cleanup
closesocket(s[0]);//close socket for client #1
closesocket(s[1]);//close socket for client #2
WSACleanup();
return 0;
}
Can anyone tell me why the codes are not working for that part?
It supposed to increase/decrease the X or Y when the specific direction keys are press but it is not working now.
Thanks alot. :)
I dont know which codes to use for that method, so someone please guide me along for the bold ones