Jump to content

  • Log In with Google      Sign In   
  • Create Account


My Server/Client lagging crazy


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Johannes1991   Members   -  Reputation: 433

Like
0Likes
Like

Posted 18 January 2012 - 09:53 AM

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[i], 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[i], &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[i]->Get_Position();
  Package.Direction = GameData.ObjectList[i]->Get_Direction();
  for(int c = 0; c < NumberOfClients; c++)
  {
   if(c == i)
	continue;
   send(ClientSockets[c], (char *) &Package, BufferSize, NULL);
  }
}
}

EnJOJ Gaming

Sponsor:

#2 Johannes1991   Members   -  Reputation: 433

Like
0Likes
Like

Posted 18 January 2012 - 10:16 AM

Solved it!
I was sending to many packages! per second
EnJOJ Gaming




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS