Hey there,
I'm trying to create a server-client architecture application, which sends a struct called PLAYER which holds x,y,z as a float, the instance of PLAYER is Player.
Defined like so
struct PLAYER
{
float x, y, z;
};
PLAYER Player;
Additional declarations in the header file
#define SERVER_ADDRESS "127.0.0.1"
#define SERVER_PORT 17000
WSADATA Winsock;
SOCKET Socket;
sockaddr_in RemoteAddress;
int SizeInt;
These structs are available on both server and client applications, I then have this code to setup a socket on the
SERVER application:
void Application::Init_Winsock()
{
WSAStartup(MAKEWORD(2,2), &Winsock);
if(LOBYTE(Winsock.wVersion) !=2 || HIBYTE(Winsock.wVersion) !=2) //Check Version
{
WSACleanup(); //Cleanup and end if not version 2
return;
}
//Create Socket
Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
//Input Reciever Information
ZeroMemory(&RemoteAddress, sizeof(RemoteAddress)); //Clear Struct
RemoteAddress.sin_family = AF_INET; //Set the Family Address
RemoteAddress.sin_port = SERVER_PORT; //Set Port
bind(Socket, (sockaddr*)&RemoteAddress, sizeof(RemoteAddress));
//Start the recieve thread
CreateThread(NULL, 0, RecvThread, (void*)this, 0, NULL);
}
Then a static function to receive packets from clients
DWORD WINAPI Application::RecvThread(LPVOID knock)
{
Application *instance = static_cast<Application *>(knock);
while(true)
{
PLAYER Recv;
recvfrom(instance->Socket, (char*)&Recv, sizeof(PLAYER), 0, (sockaddr*)&instance->RemoteAddress, &instance->SizeInt);
instance->Player = Recv;
}
}
That struct is then used like this to move the object
D3DXMatrixTranslation(&matTrans, Player.x, Player.y, Player.z);
CLIENT Application:
There is a similar init_winsock method:
void Application::Init_Winsock()
{
WSAStartup(MAKEWORD(2,2), &Winsock);
if(LOBYTE(Winsock.wVersion) !=2 || HIBYTE(Winsock.wVersion) !=2) //Check version
{
WSACleanup(); //Cleanup and return false if version is wrong
return;
}
//Create Socket
Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
//Input Receiver Information
ZeroMemory(&RemoteAddress, sizeof(RemoteAddress)); //Clear Struct
RemoteAddress.sin_family = AF_INET; //Set the Address Family
RemoteAddress.sin_addr.s_addr = inet_addr(SERVER_ADDRESS); //Set IP Address
RemoteAddress.sin_port = SERVER_PORT; //Set Port
}
Then a function to send packets to the Server's Address
void Application::Send_Packet(PLAYER* data)
{
sendto(Socket, (char*)data, sizeof(PLAYER), 0, (sockaddr*)&RemoteAddress, sizeof(sockaddr));
}
The DirectX11 object has initialization and rendered to the "Player.x/y/z" values in both applications.
Although the object does not appear on the Servers screen when a client connects and starts moving around, modifying the "Player.x/y/z" values with WASD keys, I can't figure out why