Networking/Internet Play

This may be purely a python question, but looking through the python stuff quickly I couldn’t find a good answer.

I used Legion’s code as a guide, and now I basically have my network code written for my project.

It works fine in a LAN. The clients connect to the server and it all works fine.

The problem that I have right now is that the IP address of the server computer is hardcoded into the code. The clients know where to look for the server for this reason.

Is it possible for each computer in the LAN to ping the other IP addresses asking each IP address if they are a server?

Lots of games use this type of function for LANs. One computer will host a game and the other computers in the LAN can see that a game is being hosted and choose to join it. They don’t need to know the IP address of the host computer on start-up.

Any help would be great.

Second question:

This one is about WAN/Internet play. Eventually the program that I am working on could be run on the internet using a server to find other players. Once a group is formed and the game play actually started, however, I’d like control of the game (collisions, points, etc) to go to one computers for performance reasons- I don’t think a server would be able to handle computing collisions for one game, let alone multiple games.

When the level is complete, I’d like each client and the temporary server to connect back to the internet server.

Any ideas on how to do this?

This is all for the space game that I’m writing. As always, if anyone wants to help- let me know, I’ll send you the code.

1 question … no clue but you could go though the 256 addresses of 10.1.10.***.

2 question … most games that use a server to find other players to play a game have the sim running on the ‘host’ of the game any ways. The server that just finds people normally only gives the ip address of the host-server to the people willing to connect.

But if you are talking about mmorpg’s that a different story.

On the first one:

Write a new message type (MSG_PING) that will only trigger an immediate response from the server. And implement a very short timeout for connection attempts in LAN.
Then all you need is a function, that’ll try to connect to every IP address in your range.
That’ll be ~255 connection attempts in the best case, therefore you need the short timeout.
You write the successfull connection attempts into an array, after every IP has been scanned you can compile a server list by requesting server informations from each ip in the array.

on the second one:

Well, if you pass on the server function to one of the Clients you’ll still have “one server that handles all the collisions and computing”, except that the internet connection of a player will be much slower than that of a real server on the internet. And a player based server will have to display all the fancy graphics, a pure server won’t.
What you really want is a dedicated server.
As I already mentioned in the thread of my game, I’m rewriting the whole concept of my game.
This time I’m going to write three different programs for the three different tasks of internet gaming.

There’ll be a MASTERSERVER, a SERVER and the CLIENT.

The masterserver will only hold the list of all available servers.
Servers connect to the masterserver to be registered (read: appear in the server list), clients will connect to the master server to get that server list.

The server will handle the gaming. It’ll do collision detection, computing stuff, and everything a server is supposed to do.

I don’t think the client needs further explaination here :wink:

Unfortunately I don’t have much time to work on my own game atm.
But when I’ve got a playable Version done (which will include a fully operational masterserver and server) I’ll release the source code in the forums, as usual :wink: (if I’m able to write this game, then it’s only because of this community, so it’s only fair that my first game goes straight back to the community)

Well here is how I have done it in the past…

A server opens a known port(UDP) in a port rang lets say 7000 to 7010  in none blocking mode or a thread.

Clint open a anonymous port for udp listening in none blocking mode or on a thread. 

Clint sendw a UDP pack to the local lan broadcast address with a payload that contains there open port address and a magic number or 2 to each port in the range.

When server receives packet it checks the magic’s reads the port and then responds to the sender on the port provided with something like gamename, myipaddress, tcp listener port or whatever…… Enough to get the client on to next step of connecting.

Of course you have application timeout and such to make the clean.

… Ok #2

Lets have the game server send  a message to a central  game server that has a  known name or IP . The message is a “please let me register my selfâ€


I don’t know how that can be done with Panda’s built in networking…


I have a master server, a server and a client.
The server registers itself with the master server.
The master server checks every ten minutes if the registered servers are still online.

The client registers itself with the master server via a username/password and the master server returns a login-key that expires if it isn’t refreshed after a while.
Then the client requests a list of servers (with filter functionality) from the master server.

When the client then connects to one of the servers in the list, it’ll send his login key to the server, so the server can check if the client is valid and retrieve the highscores / update the highscores at the master server.

Right now the master server is in working beta state.
The rest is still a lot of work to be done…

Yeah, I was just looking at this thread again today too.


I don’t know either. The solution of trying all 256 ip addresses just doesn’t work. At least the way I have it set up. I still don’t know what the problem is. It doesn’t register whenever there is more than one connection attempted (as happens in a loop)

this code doesn’t work.

x = self.ip.split('.')
		for i in range(256):
			item = x[0]+"."+x[1]+"."+x[2]+"."+str(i)
			self.Connection = self.cManager.openTCPClientConnection(item, self.port, self.timeout) 
			print item
			if self.Connection is not None: 
				print "sending ping"
				#Sends a datagram to each potential host
				dta = PyDatagram()
				info = [PING,0]
				self.cWriter.send(dta, self.Connection)

while this does work.

x = self.ip.split('.')
i = 13
item = x[0]+"."+x[1]+"."+x[2]+"."+str(i)
self.Connection = self.cManager.openTCPClientConnection(item, self.port, self.timeout) 
print item
if self.Connection is not None: 
	print "sending ping"
	#Sends a datagram to each potential host
	dta = PyDatagram()
	info = [PING,0]
	self.cWriter.send(dta, self.Connection)

the local IP of the server I need to ping is thus i = 13 works. But in the loop the address timesout just like all the rest.

#2 This sounds fine but the master server has to be actively running code. Have you got that kind of access on a web server? or are you just going to turn one of your boxes at home into the masterserver.

Thanks to bigfoot I’ll have an online host for the master server…

To your code…
You don’t need to ping the server… if you can connect, it’s there! And the ping time over LAN is just so NOT important…

So try it with something like this:

x = self.ip.split('.')
      for i in range(256):
         item = x[0]+"."+x[1]+"."+x[2]+"."+str(i) 

if self.clientConnect(item, self.port):

def clientConnect(self, server, port):
        cTimeout = 100
        Connection = self.cManager.openTCPClientConnection(server, port, cTimeout)
        if (Connection):
            return 1
        return 0

If the cTimeout=100 doesn’t work, try a higher value…

The reason not to just try 255 address is that assuming the net is a class network is only ok for tests not public code. Not all network are segmented this way. Sounds like why IP has UDP broadcasts. and a query interface to find the broadcast address.

I do believe python has native socket bindings shipped with most builds … or maybe not try import socket

What network Functions are you using in panda If you must use panda networking try one in the NativeNet Directory of panda/src/


The only function I do not think is exposed in panda is the internet query of network mask and address … This is needed to find the local broadcast address… Hmm… maybe on windows the stupid call to tell Winsock we will be using sockets not a probel on other OS’s.

	Legion I like your approach.    Are you just using TCP for all the master server connections. If so do they stay open or ?

I would be a little careful about just blindly picking from the list and trying to open it. I find you need a second level of round trip to just prove you can plow threw firewalls and such to get to the Game Server from any given  client. A good side effect of this is that you are effetely sending an ack/nack ping from client to game server and back and can use the roundtrip time to help select the game. 


@Legion: Well, I tried your code- still can’t get it to work.

Don’t quite know what to do.

@Roger: Did see your post til just now. I agree there has to be a better way than polling all 255 address in a LAN.

Currently I’m already importing socket as it was the easiest way to obtain the machine’s IP.

hostname = socket.gethostname()
self.ip = socket.gethostbyname(hostname)

But I still don’t quite understand what you’re saying. Are the other computers in the network actively broadcasting some useful information so that I can form a connection?

My real world situation for testing is a home LAN and two computers. The one acting as server has a prefered IP address of so hardcoding this IP address makes the program work fine, what I want is to automate the process.

This may help runs on OSX …quick hack but seems to work… It a simple example of send and receiving udp on the broadcast address to a lan using python socket module.

So how to abuse it for what I think you are doing.

Simples one …

( Service advertiser model)

When Game Server want to allow people in to the game it send a broadcast every 3 seconds or so… Something like “Come and Play With me on port 21â€

Well, I got to hand it to you Roger, your code did the trick.

I used the code you labeled “client” for the server to broadcast its IP address and the “server” code on the clientside to receive the IP address and then link to it.

works great and it makes the multiplayer aspect of my game completely intuitive for the user which is what I wanted.

…which means that all your clients will stay within a single network segment - aka home network.
Don’t get me wrong - a nice way to reduce configuration troubles if you are playing in a “local” LAN, but as soon as one of your players is in another subnet (such as 192.168.15.x) a broadcast won’t help much.

You can either create a completely p2p-based “network” finding, where you broadcast the local subnet for other clients or give them the ability to connect to a known machine to get into the game network - or you have to set a “hardcoded” (e.g. configs) masterserver, that can handle a list of working gameservers.

While I like the first approach more (p2p has advantages if you don’t want to rely on a static masterserver or do have networks that are in a private LAN), the whole handshake and authentication thing is much simpler with a static masterserver.

I worked on a 3D-Chatsystem (people that are further away needed to “scream” or travel the space to talk to folks that aren’t near you) for some time. It was based on an own written P2P-Protocoll - means: all you needed to know was your neighbor - then you know some more peers to connect to - it really IS a pain in the ass regarding what you have to take care about. :wink:

But the advantage still remains… as long as you know a friend of yours who is in the network - or you know an official entry point - you will never find an easier way to get known in/to a network if the stable masterserver-option isn’t given (or you want to make sure that a broken single connection can’t kill a whole part of the network - they take other peers to connect to the world they need to be at.

Biggest disadvantage is of course: A space shooter will be a bit off the limits - even if ping time is 20ms - if you have to connect through 2-3 peers to get onto the server you have to interact with, you lag like hell.
That was one of the reasons why I have had to freeze my ambitions there - maybe an MORPG or slow MMORPG would fit for it but meh, I am a bit off the topic. :slight_smile:

Short summary:
In case you want to keep your shooter in a private LAN, your current approach is (simply put) the best. If you want to use the net later on, you will have to consider how to proceed. Masterserver or P2P - at least for finding a gameserver.

Regards, Bigfoot29