today I am fighting with a game code. The game runs in DirectX9 and will be an online multiplayer (1v1 game). Thus I create 2 applications actually: 1 to act as a server and 1 to act as a client. However, while the games compile, the server does not want to display anything. Here's my code (not all of it, for clarity, but I can show more if asked)
SERVER:
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;
//settipg up DX3D window
//setting up initial state of game
//here we establish communication between server and client
WORD winsock_version = 0x202;
WSADATA winsock_data;
int address_family = AF_INET;
int type = SOCK_DGRAM;
int protocol = IPPROTO_UDP;
SOCKET sock = socket(address_family, type, protocol);
SOCKADDR_IN local_address;
local_address.sin_family = AF_INET;
local_address.sin_port = htons(PORT);
local_address.sin_addr.s_addr = INADDR_ANY;
int8 buffer_in[SOCKET_BUFFER_SIZE];
int8 buffer_out[SOCKET_BUFFER_SIZE];
// enter the main loop:
MSG msg;
while (TRUE)
{
int flags = 0;
SOCKADDR_IN from;
SOCKADDR* to = (SOCKADDR*)&from;
//prepare state packet to send to a client
int32 bytes_written = 0;
memcpy(&buffer_out[bytes_written], &bat1.pos_x, sizeof(bat1.pos_x));
//i read the rest of gamestate and prepare a data packet
// send to client
int to_length = sizeof(from);
// get state packet from a client
flags = 0;
int from_size = sizeof(from);
int bytes_received = recvfrom(sock, buffer_in, SOCKET_BUFFER_SIZE, flags, (SOCKADDR*)&from, &from_size);
//get data from packet
int read_index = 0;
memcpy(&bat2.pos_x, &buffer_in[read_index], sizeof(bat2.pos_x));
read_index += sizeof(bat2.pos_x);
//likewise, i get the rest of player 2 game state here
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (msg.message == WM_QUIT)
break;
//reading input
//game state update
ball1.move();
//etc.
}
render_frame();
}
// clean up DirectX and COM
cleanD3D();
return msg.wParam;
}
The client code is quite similar, it sends the gamestate of player2 and tries to receive gamestate of player 1, so the structure of the code is the same, I only changed bat1 to bat2 etc.. Only, before the main loop there is a following piece of code setting up the ip of the server (hardcoded for now):
SOCKADDR_IN server_address;
server_address.sin_family = AF_INET;
server_address.sin_port = htons(PORT);
server_address.sin_addr.S_un.S_un_b.s_b1 = IP1;
server_address.sin_addr.S_un.S_un_b.s_b2 = IP2;
server_address.sin_addr.S_un.S_un_b.s_b3 = IP3;
server_address.sin_addr.S_un.S_un_b.s_b4 = IP4;
Now I have following problems:
While the client compiles neatly and game runs when launched, server only shows white window and "thinking" cursor. Through testing I pinpointed a line that is causing the unwanted bahaviour to this one:
int bytes_received = recvfrom(sock, buffer_in, SOCKET_BUFFER_SIZE, flags, (SOCKADDR*)&from, &from_size);
Now, how do I fix it? At this point I don't care as much about synchronization of gamestates as much (since that I can deal with later and the issue is not as relevant as areas for both players are separate), but I would like the game obviously to send some information and read each other gamestates.
- My second big question is: how do I avoid hardcoding server IP? I would like the client to be able to find server on its own. However, I seem to find no simple solution to this.