Jump to content
  • Advertisement
Sign in to follow this  
Johannes1991

My Server/Client lagging crazy

This topic is 2524 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been playing around with winsock now from time to time and i decided to make a test server and client. I got the server up and going and the client works aswell. But there's a delay of 1 - 2 secs, just now i'm only sending the positions back and forth.

First when i used send() i set the len paramter with sizeof(package). But it made my client side unstable. But when i set a constant size it became very stable.

The code is in c++,

Server code,(process package is in the client too):

bool Server::Initialize_Server(UINT MaxClients)
{
this->MaxClients = MaxClients;
ClientAddresses = new sockaddr_in[MaxClients];
ClientSockets = new SOCKET[MaxClients];
AddressSize = sizeof(sockaddr_in);
NumberOfClients = 0;
if(WSAStartup(MAKEWORD(2,2), &Winsock) == WSAVERNOTSUPPORTED)
{
cout << "\n\nCouldn't Start Winsock\n\n";
WSACleanup();
return false;
}

HostSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(HostSocket == INVALID_SOCKET)
{
cout << "\n\nServer Socket Couldn't Be Created\n\n";
WSACleanup();
return false;
}
ZeroMemory(&HostAddress, sizeof(sockaddr_in));
HostAddress.sin_family = AF_INET;
HostAddress.sin_port = htons(PortNumber);
HostAddress.sin_addr.s_addr = INADDR_ANY;
if(bind(HostSocket, (sockaddr *) &HostAddress, sizeof(sockaddr_in)) != 0)
{
int i = WSAGetLastError();
cout << "Couldn't Bind Socket";
WSACleanup();
return false;
}
if(listen(HostSocket, MaxClients) != 0)
{
cout << "Couldn't Listen To Socket";
WSACleanup();
return false;
}
u_long iMode = 1;
ioctlsocket(HostSocket, FIONBIO, &iMode);
MultiPlayer_1();
return true;
}
void Server::Process_Packages()
{
int PackageSize, PointerPos;
char* tempPackage;
for(int i = 0; i < NumberOfClients; i++)
{
PointerPos = PackageSize = 0;
ZeroMemory(Buffer, BufferSize);
PackageSize = recv(ClientSockets, Buffer, BufferSize, NULL);
while(PointerPos < PackageSize)
{
char PackageID = *((char *) (Buffer + PointerPos));
switch(PackageID)
{
case PackageID_Player:
{
//tempPackage = Network_Stuff.Check_Complete_Package(sizeof(GroupUnitInfos_Package), &PackageSize, 512, &PointerPos, (char *&) *Buffer);
if(!Check_Package_Whole(&ClientSockets, &PointerPos, sizeof(Player_Package), &PackageSize, tempPackage))
{
Player_Package* temp = (Player_Package *) tempPackage;
Player* tempPlayer = (Player *) GameData.ObjectList[temp->Player_ID];
tempPlayer->Set_Position(temp->Position);
tempPlayer->Set_Direction(temp->Direction);
delete[] tempPackage;
}else
{
Player_Package* temp = (Player_Package *) (Buffer + PointerPos);
PointerPos += sizeof(Player_Package);
Player* tempPlayer = (Player *) GameData.ObjectList[temp->Player_ID];
tempPlayer->Set_Position(temp->Position);
tempPlayer->Set_Direction(temp->Direction);
}
}break;
default:
{

cout << "\n\nCouldn't Identify Package\nPackage Size: " << PackageSize * 0.001f << "kb\n\n";
PointerPos = PackageSize;
}
break;
}
}
}
}
bool Server::Check_Package_Whole(SOCKET* const Socket, int* PointerPos, const int SizeOfPackage, int* RevievedPackageSize, char *& tempBuffer)
{
if(*RevievedPackageSize - *PointerPos < sizeof(SizeOfPackage))
{
int PartSizeInBuffer = *RevievedPackageSize - *PointerPos;
tempBuffer = new char[sizeof(SizeOfPackage)];
memcpy(tempBuffer, (char *)(Buffer + *PointerPos), PartSizeInBuffer);
*RevievedPackageSize = recv(*Socket, Buffer, BufferSize, NULL);
memcpy((char *) (tempBuffer + PartSizeInBuffer), Buffer, sizeof(SizeOfPackage) - PartSizeInBuffer);
*PointerPos = sizeof(SizeOfPackage) - PartSizeInBuffer;
return false;
}
else
return true;
}
void Server::Update_Clients()
{
Player_Package Package;
for(int i = 0; i < NumberOfClients; i++)
{
Package.Player_ID = i;
Package.Position = GameData.ObjectList->Get_Position();
Package.Direction = GameData.ObjectList->Get_Direction();
for(int c = 0; c < NumberOfClients; c++)
{
if(c == i)
continue;
send(ClientSockets[c], (char *) &Package, BufferSize, NULL);
}
}
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!