Using Sockets for Online Multiplayer

Hello, I’ve been going through a lot of tutorials, examples, Ect. On how to use sockets in python to create an online game.
I came up with this:

import socket

class World(DirectObject)
  def __init__(self):
    self.host = 'localhost'
    self.port = '25565'

  def open_socket(self):
    self.server = socket.socket(socket.AF_INET,
                                                socket.SOCK_STREAM)
    self.server.bind((self.host,self.port))
    self.server.listen(5)

  def Run(self):
    self.open_socket()
    running = 1
    id = 0
    while running:
      id +=1
      
      c = Client(self.server.accept())
      c.start()

When I run it, I get this error:

DirectStart: Starting the game.
Known pipe types:
  wglGraphicsPipe
(all display modules loaded.)
Worked!
Traceback (most recent call last):
  File "Blaze3D.py", line 238, in triggerStartAction
    self.Run()
  File "Blaze3D.py", line 249, in Run
    self.open_socket()
  File "Blaze3D.py", line 245, in open_socket
    self.server.bind((self.host,self.port))
  File "<string>", line 1, in bind
TypeError: an integer is required
:task(error): Exception occurred in PythonTask triggerStartAction
Traceback (most recent call last):
  File "Blaze3D.py", line 655, in <module>
    run()
  File "C:\Users\Britany\Desktop\Panda3D\Panda3D-1.7.2\direct\showbase\ShowBase.py", line 2630, in run
    self.taskMgr.run()
  File "C:\Users\Britany\Desktop\Panda3D\Panda3D-1.7.2\direct\task\Task.py", line 502, in run
    self.step()
  File "C:\Users\Britany\Desktop\Panda3D\Panda3D-1.7.2\direct\task\Task.py", line 460, in step
    self.mgr.poll()
  File "Blaze3D.py", line 238, in triggerStartAction
    self.Run()
  File "Blaze3D.py", line 249, in Run
    self.open_socket()
  File "Blaze3D.py", line 245, in open_socket
    self.server.bind((self.host,self.port))
  File "<string>", line 1, in bind
TypeError: an integer is required

**** End of process output ****

Out of this,
I cannot seem to find the error.
Or even if this will work.
I’ve found a lot of unreliable sources,
Do you think this one is correct?
It’s nothing too big, Just something I can work with for now. I’ll be adding more to it later on, But i first have to know if this is going to work to accept other players.

Thanks for the help,
Luna

The error is right in front of you.

  File "Blaze3D.py", line 245, in open_socket
    self.server.bind((self.host,self.port))
  File "<string>", line 1, in bind
TypeError: an integer is required 

This tells you that the function is expecting an integer number, but what you are providing is not an integer.

Hint:

    self.port = '25565' 

I don’t understand how it is not an integer.
Or even how to make it accept it without being an integer.

EDIT: I got it, Seems I didn’t need the quotes aroung the port.
Anywho, I got a new problem:

  File "Blaze3D.py", line 256, in Run
    c = Client(self.server.accept())
NameError: global name 'Client' is not defined

I understand “Client” Isn’t defined, But this is what people used in their examples without an import for it.
How can I fix this?

EDIT(2): Fixed it. I don’t believe it’s needed, So I removed it.
Testing out my socket (which, I’m guessing works),
I now get this error:

DirectStart: Starting the game.
Known pipe types:
  wglGraphicsPipe
(all display modules loaded.)
Worked!
Traceback (most recent call last):
  File "Blaze3D.py", line 238, in triggerStartAction
    self.Run()
  File "Blaze3D.py", line 249, in Run
    self.open_socket()
  File "Blaze3D.py", line 245, in open_socket
    self.server.bind((self.host,self.port))
  File "<string>", line 1, in bind
socket.error: [Errno 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted
:task(error): Exception occurred in PythonTask triggerStartAction
Traceback (most recent call last):
  File "Blaze3D.py", line 651, in <module>
    run()
  File "C:\Users\Britany\Desktop\Panda3D\Panda3D-1.7.2\direct\showbase\ShowBase.py", line 2630, in run
    self.taskMgr.run()
  File "C:\Users\Britany\Desktop\Panda3D\Panda3D-1.7.2\direct\task\Task.py", line 502, in run
    self.step()
  File "C:\Users\Britany\Desktop\Panda3D\Panda3D-1.7.2\direct\task\Task.py", line 460, in step
    self.mgr.poll()
  File "Blaze3D.py", line 238, in triggerStartAction
    self.Run()
  File "Blaze3D.py", line 249, in Run
    self.open_socket()
  File "Blaze3D.py", line 245, in open_socket
    self.server.bind((self.host,self.port))
  File "<string>", line 1, in bind
socket.error: [Errno 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted

**** End of process output ****

Again, I’m guessing my connect hosting is working,
But it only allows one players.
Ideas on how to accept more than one player to the server?

EDIT(3): I did happen to find a ton of topics on this problem. But I cannot find a decent website to explain it. And i’m getting kind of pissy not being able to figure this thing out.
I just want to allow more than one person, is all.
And I see no websites explain JUST what i’m needing.
ANY help would be MUCH appreciated.

I suggest you look into twisted. It’s the python networking library and there are many examples all around the web.

I’m looking for a good example without any downloads.
Would you happen to know a good website for multiplayer using Sockets?
I usually find ones on Blogs which seem less reliable.

Mainly, I have it figured out, Just not the part with allowing multiple people,
And then ClientSide things I can figure out with ease.

I found a great and reliable source :smiley:
It seems to work out great (as people say), and it’s very relevant to my code.
However,
I did not have:

c = Client(self.server.accept())
c.start()
self.threads.append(c)

When I add this and run my file,
I get the error:

  File "Blaze3D.py", line 307, in Run
    c = Client(self.server.accept())
NameError: global name 'Client' is not defined

How can I fix this/import Client?

How about asking the author of the actual code?

We can’t really tell anything from the little code you posted.

It’s not a blog site, or something on comments, it’s an actual source of examples explaining what each things do. It’s a Client error, which is in that little code I posted, but here you go:

def open_socket(self):
  self.server = socket.socket(socket.AF_INET,
                                              socket.SOCK_STREAM)
  self.server.bind((self.host,self.port))
  self.server.listen(5)

def Run(self):
  self.open_socket()
  running = 1
  id = 0
  id +=1
  print "id: "+str(id)+" logged in."
  self.open_socket()
  c = Client(self.server.accept())
  c.start()
  self.threads.append(c)

I then get that Client error.

" NameError: global name ‘Client’ is not defined"

It is complaining that Client is not defined. You need to import this class with:

from import Client

Where module is the file with this Client class implementation.

Yes, I understand such. However, what module?
I looked it up and found nothing of the sort, other than “It’s a bug”.

Is the code that contains the “Client” class in another file somewhere? If so, and presuming that it’s in the same directory as the code that you present above, try “from import *” (or “from import Client” if there are things in that you don’t want imported), where “” is the name of the file in which “Client” is declared.

If said file is in another directory, however, then where is that?

If “Client” isn’t in another directory, then where is it?

You mention that you got hold of some source - could you point us to that, please?

Idk where Client is listed AT.

sure:
svn.code.sf.net/p/scratchspace/c … /server.py

Ah, there it is - thank you. :slight_smile:

“Client” appears to be defined in that file (below “Server”, I believe).

Presuming that the file “server.py” is in the same directory as your code, try “from server.py import *”, or, if you are confident that you only want “Client”, “from server.py import Client”.