What I'm trying to program is a simple multithreaded python game server for fun. After much frustration, I've not been able to figure out why my test client connection times out. Here's my server.py code:
import socket
import threading
import clientThread
import struct
import string
class Server:
def __init__(self):
self.HOST = 'localhost'
self.PORT = 22085
self.BUFFIZE = 1024
self.ADDRESS = (self.HOST,self.PORT)
self.clientList = []
input("Press enter to start the server. . .")
self.running = True
self.serverSock = socket.socket()
self.serverSock.bind(self.ADDRESS)
self.serverSock.listen(2)
self.clientThread = clientThread.clientThread(self)
print("Starting client thread. . .")
self.clientThreadObj = threading.Thread(target = self.clientThread.start, args = (self))
print("Awaiting connections. . .")
while self.running:
clientInfo = self.serverSock.accept()
print("Client connected from %s." % clientInfo[1])
# Append to clientThread list...
self.serverSock.close()
print("- end -")
serv = Server()
The server starts the thread for existing connections and starts listening. The thread built for existing connections, clientThread
, loops through a list of client objects which for now do nothing, they are simply architectural. Here is the clientThread.py
import socket
import threading
import struct
import string
class clientThread:
def __init__(self, serv):
self.server = serv
self.clientList = []
self.running = True
print("Client thread created. . .")
def start(self):
print("Beginning client thread loop. . .")
while self.running:
for client in self.clientList:
message = client.sock.recv(self.server.BUFFSIZE)
if message != None and message != "":
client.update(message)
And finally, the very simple client object:
import string
class clientObject:
def start(self,clientInfo):
self.sock = clientInfo[0]
self.address = clientInfo[1]
def update(self,message):
self.sock.send("Testamundo.
".encode())
Now, the problem here is that my client can't even connect to my server. It simply times out. Here is the code for my simple client test:
import socket
import string
address = ("192.168.1.1",22085)
mySocket = socket.socket()
mySocket.connect(address)
print("Connected successfully!")
This returns on the line that connects to address, "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond."
Any suggestions? Thanks! Sorry for all this code, I wasn't sure if I needed to post it all or not, so I figured it couldn't hurt too much.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…