Hello, I am trying to get a game server running based off of a tutorial I found. I copied all the code and have got the code to look like this
import socket
# Let's import the socket module import socket
# Assign a port number to a variable. Assigning values such as this makes
# it much easier to change, instead of having to edit every reference of the
# port in the script. Note, use a port value above 1024. Popular services, such
# as ftp, http, etc. use the lower port numbers and this will help to avoid conflict.
port = 2020
# Here is where we will create our socket. We assign the socket to 's' by calling
# the socket module and socket() function. The socket() attributes tell the socket
# to create a STREAM socket type from the INET socket family.
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Next, we bind the port to the hostname. This will tell the server that we want
# to use the specified port to listen on.
s.bind((socket.gethostname() , port))
# Now lets tell the server to start listening for client connections.
# For testing purposes, we'll setup the server to handle 1 simultaneous
# connection. You can change this later if you need. s.listen(1)
# Since this script will be run outside of the game, most likely in a terminal
# window, we can use print() to show that the server is running. I've included
# the port value in the command to display the port number.
print "Game server is running on port", port
# This is our main loop to handle incoming client connections.
# Connections are accepted and the message is stored into a variable.
# The connection information is print() 'ed to the terminal window and the message
# is then sent back to the client. Then the connection is closed.
while 1:
conn, addr = s.accept()
data = conn.recv(1024)
# display client connection info
print "connected...", addr, data
conn.send(data)
conn.close()
The problem is, I seem to get this error.
$ python server.py
Game server is running on port 2020
Traceback (most recent call last):
File "server.py", line 27, in <module>
conn, addr = s.accept()
File "/usr/lib/python2.6/socket.py", line 195, in accept
sock, addr = self._sock.accept()
socket.error: [Errno 22] Invalid argument
someone had told me it was something with the socket.connect. I can’t seem to find out on google how to fix the problem. If anyone can help, I would greatly appreciate it.