Why this code cant work?

closed account (ETCk4iN6)
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iostream>
#include <stdio.h>
#include <conio.h>


using namespace std;

#define DEFAULT_BUFLEN 2
#define DEFAULT_PORT "27017"
#define RPSS_NUMOFUSERS 0x03
#define RPSS_STARTGAME 0x04
#define VK_LEFT 0x25
#define VK_UP 0x26
#define VK_RIGHT 0x27
#define VK_DOWN 0x28
WPARAM wParam;

struct reqset
{
int playerX;
int playerY;
};

void ProcessData(reqset &rs, WPARAM wParam)//just a function to process the player's coordinates
{
// switch(rs.playerX)
//{
// case VK_LEFT : rs.playerX += 1;
// break;
// case VK_RIGHT : rs.playerY -= 1;
// break;
//}
//
// switch(rs.playerY)
//{
// case VK_UP : rs.playerY -= 1;
// break;
// case VK_DOWN : rs.playerY += 1;
// break;
//}

if(wParam == VK_LEFT)
{
rs.playerX -= 1;
}
else if(wParam == VK_RIGHT)
{
rs.playerX += 1;
}
else if(wParam == VK_UP)
{
rs.playerY += 1;
}
else if(wParam == VK_DOWN)
{
rs.playerY -= 1;
}
}




int __cdecl main(void)
{
WSADATA wsaData;
SOCKET ListenSocket = INVALID_SOCKET;
//ClientSocket = INVALID_SOCKET;
SOCKET s[2];//create 2 sockets for 2 players
sockaddr you[2];//2 socket address for 2 players
int addr_size = sizeof (sockaddr);

reqset RS;//create an instance RS of type "reqset"
struct addrinfo *result = NULL,
hints;

int iResult, iSendResult;

cout << "Start of Server Application\n";

// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0)
{
cout << "WSAStartup failed: " << iResult << endl;
return 1;
}


ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;

// 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)
{

cout << "socket failed: " << WSAGetLastError() << endl;
freeaddrinfo(result);
WSACleanup();
return 1;
}



// Setup the TCP listening socket
iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR)
{
cout << "bind failed: " << WSAGetLastError() << endl;
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 1;
}

freeaddrinfo(result);

iResult = listen(ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR)
{
cout << "listen failed: " << WSAGetLastError() << endl;
closesocket(ListenSocket);
WSACleanup();
return 1;
}


int num_players = 0;

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);

if (s[num_players] == INVALID_SOCKET)
{
cout << "accept failed: " << WSAGetLastError() << endl;
closesocket(ListenSocket);
WSACleanup();
return 0;
}

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
Last edited on
???
For what part?
[code][/code] tags, please.
Code without the tags is really ugly.

I assume your referring to the part that has been commented out; is it giving you an error or is it simply not doing what your asking?
Topic archived. No new replies allowed.