multiplayer server/client basic structure?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 2193 Views - Last Post: 15 December 2012 - 05:38 PM Rate Topic: -----

#1 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

multiplayer server/client basic structure?

Posted 14 December 2012 - 05:57 PM

Im wondering what the best way to set up my UDP server/client program is. Here is what I am currently trying to do.

-client sends key input to server
-Server receives key input. Checks for collisions and updates this client's position. 
-Server sends this client a list of all the client's information(position, HP, etc) including the client's own information
-client receives this list, updates his own information, and updates the list of connected players the client is keeping.



The problem im having with this, is getting sending the list of client's information from the server to the client. Im using SFML and only have a very basic understanding of networking.

My question is: Is my approach to this okay, and if it is how can i get the list( im using an std::map<std::string, Client> ) sent to the client though an sf::Packet. If my approach is not okay, what is a better approach?

Is This A Good Question/Topic? 0
  • +

Replies To: multiplayer server/client basic structure?

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: multiplayer server/client basic structure?

Posted 14 December 2012 - 06:25 PM

My immediate concern is that you are sending over an std::map structure between server and client? This isn't necessary. You should only be sending the essential data between client and server rather than all that with all the unnecessary fluff.

Here is how I would approach it. Note that you haven't described the game this is all based around so this will be vague.

Client - Should only send basic information in the packet like player position and their state like firing weapon or moving. Of course if and only if the player is moving or firing a weapon do we need to send updated orientation information. If that hasn't changed there isn't much if anything to send.

Server - This broadcasts the overall game state to all clients for local game state updating.

There is one key to this approach. If you have ever tested your broadband speed connections you will notice that download speed exceeds upload speeds by around 15x. Given that servers are often very fast machines with good network connections, unlike many clients, the data transfer load is clearly on the server. Also, the local game state updates are performed locally on the client for presentation to the user rather than expecting the server to send the entire updated scene to the user, which will create huge lag.

Hope this gives you an idea how to approach things for best performance.
Was This Post Helpful? 1
  • +
  • -

#3 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: multiplayer server/client basic structure?

Posted 14 December 2012 - 06:32 PM

Ok thanks i diddnt even think of only sending client info to the server when it has changed.

however, i have an issue with this line

Quote

Server - This broadcasts the overall game state to all clients for local game state updating.


How can i do this without sending over vectors and lists and large structures? My problem is that a client needs( i think ) to be able to Render other players to its own screen. It will need their position to do so. And when the game becomes more complicated it will need other players HP, damage state, etc.

As for my game's details, right now i am just trying to have players move their own ball around a screen while other players being able to see eachother. However, that isnt even close to happening at the moment.
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: multiplayer server/client basic structure?

Posted 14 December 2012 - 07:15 PM

See the server as a collector of information to broadcast to the clients who locally update the game state. The server should not have an internal representation of the game at all, all it knows is values.

As pointed out, if you send over std::map you are sending over an object that it larger than required. You need to create a structure that only holds coordinate, vector and player-controlled character/object state.
Was This Post Helpful? 0
  • +
  • -

#5 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: multiplayer server/client basic structure?

Posted 14 December 2012 - 07:35 PM

Im sorry but im still having trouble understanding. The server sends a list of all clients informatio; std::vector<Client> for example. A Client struct containing coordinates and HP ) to each client every time interval. This server code would look something like:
for( i; i != listOfClients.end(); ++i)
{
     send listOfClients[i] the listOfClients; 
}



Now you are saying is ineffective, and i agree. But you haven't really explained how to implement a server that can send a large amount of information without using lists.

Could you please psuedo-code an example of a server handling 3 clients that all have an x and y variable? Each client needs to be aware of the other 2 client's variables.
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: multiplayer server/client basic structure?

Posted 14 December 2012 - 08:28 PM

Okay let's break this down. Firstly, why specifically do you need to use a list? Wouldn't it be more effective to have an array of the player info struct type since you know you will have only up to three clients (each with one player)?
Was This Post Helpful? 0
  • +
  • -

#7 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: multiplayer server/client basic structure?

Posted 15 December 2012 - 06:52 AM

Well i just said three players so you could use that for an example, but ill give you a better scenario. Lets say i want the game to hold up to 8 players, but it can still be played with less than 8 players. I would need a dynamic list to hold each player's information. Dyanimic incase players leave or join in during a game.
Was This Post Helpful? 0
  • +
  • -

#8 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: multiplayer server/client basic structure?

Posted 15 December 2012 - 07:18 AM

No you don't. Eight players is a small number.

The point that I'm trying to get across here is that you only send and receive information that you need to: If there are two players use a simple structure to send info for two, likewise for up to eight. If you are using STL list you are passing a much larger object than you need to.
Was This Post Helpful? 0
  • +
  • -

#9 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: multiplayer server/client basic structure?

Posted 15 December 2012 - 07:30 AM

Oh okay i think i could make that work. So I should have an array of 8 players, and if a space is empty just have some kind of indicator in that space so i can skip over it. Right?

Edit: instead of making a new topic for a simple question ill ask it here. Should the client be receiving messages at all times?

If you haven't noticed by now i like drawing out diagrams to illustrate the question.

current setup:
every 10 millseconds
{
    if message received
    {
         handle the cleint's message.
         send array of all Client's Information to each client
    }
}



Is this a better approach?
while true
{
    if message received
    {
        handle the client's message
    }

    if 10 milliseconds are up
        send array; reset clock
}






Edit: Or should there be two separate threads for sending and receiving?

This post has been edited by Nano511: 15 December 2012 - 08:19 AM

Was This Post Helpful? 0
  • +
  • -

#10 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 688
  • Joined: 16-February 09

Re: multiplayer server/client basic structure?

Posted 15 December 2012 - 09:57 AM

I would try to receive a messages every chance you get. Networking in just another form of input for the client and I don't think you should skip out on frames. Of course, it depends on your game, I guess.
Was This Post Helpful? 0
  • +
  • -

#11 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: multiplayer server/client basic structure?

Posted 15 December 2012 - 10:50 AM

View PostNano511, on 15 December 2012 - 02:30 PM, said:

Oh okay i think i could make that work. So I should have an array of 8 players, and if a space is empty just have some kind of indicator in that space so i can skip over it. Right?

Edit: instead of making a new topic for a simple question ill ask it here. Should the client be receiving messages at all times?

If you haven't noticed by now i like drawing out diagrams to illustrate the question.

current setup:
every 10 millseconds
{
    if message received
    {
         handle the cleint's message.
         send array of all Client's Information to each client
    }
}



Is this a better approach?
while true
{
    if message received
    {
        handle the client's message
    }

    if 10 milliseconds are up
        send array; reset clock
}






Edit: Or should there be two separate threads for sending and receiving?

There are so many options out there that actually depend on how your game engine is built and the timings of screen buffer flips and how often IO is polled.

I doubt you will need a multithreaded environment, but a lot more about your overall game engine architecture must be known.
Was This Post Helpful? 0
  • +
  • -

#12 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: multiplayer server/client basic structure?

Posted 15 December 2012 - 03:02 PM

I suppose i should keep posting here as long as the poblem im facing is relevant to the title and the OP.

I am getting a break exception: stack overflow error trying to send the array from server to client. It seems to send fine but the error occurs during the receiving. I briefly researched the stack overflow break exception and it happens because the stack( amount of space my variables are allotted?) has exceeded its limits(?). Here is all the code i think is relevant.

Server file main.cpp. This is the code that sends the array.
//Global scope. This code is directly after the Client struct definition.
 sf::Packet& operator <<(sf::Packet& packet, const Client& A)
 {
	 return packet << A;
 }
  sf::Packet& operator >>(sf::Packet& packet, Client& A)
 {
	 return packet >> A;
 }

//in int main()
 // Send all client info to each client 
 for(int i = 0; i < 8; i++) // for each client...
 {
	if( clientInfo[i].IDnum != -1 )// ...except the empty clients
	{
		for( int j = 0; j < 8; i++)// send every clients information
		{
			packet.clear();
			packet << clientInfo[j]; // clientInfo is an array of Client's.
			socket.send(packet, clientInfo[i].address, port);
		}
	}
 }



Client file in main.cpp. This code should receive the array
//In global scope
 sf::Packet& operator <<(sf::Packet& packet, const Client& A)
 {
	 return packet << A;
 }
  sf::Packet& operator >>(sf::Packet& packet, Client& A)
 {
	 return packet >> A;
 }

// In a thread for recieving messages
for( int i = 0; i < 8; i++)
{
	packet >> clientsInfo[i]; // This is where the break exception occurs. This  
                              //line runs but it breaks before reaching the next line
}



Does anything look wrong?
Was This Post Helpful? 0
  • +
  • -

#13 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: multiplayer server/client basic structure?

Posted 15 December 2012 - 04:02 PM

What is the size of the packet vs. the array you are trying to send. I'm seeing you passing around references, but no information on the size of data being sent and received.
Was This Post Helpful? 0
  • +
  • -

#14 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: multiplayer server/client basic structure?

Posted 15 December 2012 - 04:15 PM

Have you had a look at something like this?

http://msdn.microsof...v=vs.85%29.aspx
Was This Post Helpful? 0
  • +
  • -

#15 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: multiplayer server/client basic structure?

Posted 15 December 2012 - 04:17 PM

Hm i did not know was supposed to do that. I am also unsure of how to do that. I don't see anything about it in the documentation so could you please explain what it is exactly i should do?


Edit: Are you saying i should change from sfml to winsock?

Edit 2: Okay after doing researched i found what you're talking about. After fixing it to this the problem persist.

Send
packet << static_cast<sf::Uint32>(clientInfo.size());

// Send all client info to each client 
for(int i = 0; i < 8; i++) // for each client
{
	if( clientInfo[i].IDnum != -1 )// dont send to empty spots
	{
		for( int j = 0; j < 8; i++)// for every client
		{
			packet << clientInfo[j];
			socket.send(packet, clientInfo[i].address, port);
		}
	}
}



Receive
sf::Uint32 size;
packet >> size;

for( sf::Uint32 i= 0; i < size; ++i)
{
         packet >> clientsInfo[i];
}


This post has been edited by Nano511: 15 December 2012 - 05:24 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2