I'm trying to create a client/server console game, but I'm having trouble keeping the socket alive, it seems to be closing before I've called close()
and I can't figure out why.
I've read the thread here but I have called connect()
outside of the while loop since the logic of that already made sense to me before I attempted to run it, but I'm still getting the error from the server shell though:
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
So far I'm not getting an error on the client shell, it just exits running right after launching which is also when the server error occurs.
After fixing mistakes noted in the comments, the client now also shows an error stating that connected
is undefined when attempting to start the while loop but should be since Connect()
is run before entering the loop which should have set connected = True
thus the loop should run but does not. I doubt this is related to the server issue at all, but what maybe the problem here?
I'll put the code for both below:
Client
import socket
def GetData():
data = s.recv(1000).decode()
if (data == "closing"):
connected = False
else:
print(data)
def SendData():
tdata = input("")
s.send(data.encode())
def Connect():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", 2222))
connected = True
global connected
Connect()
while (connected == True):
GetData()
if (connected == False):
s.close()
break
SendData()
Server
import socket
import random
def TheMainGame():
print ("started TheMainGame") # Remove after debug
def within(val, goal):
print ("Runing 'within' function") # Remove after debug
i = abs(val - goal)
if (i <= 5):
return True
else:
return False
def Guess():
print ("Running 'Guess' function") # Remove after debug
while True:
print ("Entered while loop") # Remove after debug
data = "What is your guess?"
c.send(data.encode())
print ("Sent data") # Remove after debug
t = c.recv(1000).decode()
print ("Got data") # Remove after debug
if (t == x):
data = "Correct!"
c.send(data.encode())
data = "closing"
c.send(data.encode())
c.close()
break
elif (within(t,x) == True):
data = "You're close!"
c.send(data.encode())
elif (within(t,x) == False):
data = "Try again"
c.send(data.encode())
else:
data = "There was an error computing the value"
c.send(data.encode())
c.close()
break
x = random.randrange(1, 20, 1)
Guess()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("127.0.0.1", 2222))
s.listen(5)
global data
while True:
(c,a) = s.accept()
print("Received connection from", a) # Remove after debug
data = "Hello %s" %a[0] # Remove after debug
c.send(data.encode()) # Remove after debug
TheMainGame()
The error in the server shell relates to line 19 which is why the server attempts to ask the client the question, which is also the second time it attempts to send data to the client but the attempt never successfully happens, which is why I think the socket is being closed even though I never told it to anywhere before that point since it hasn't even been able to check if t == x
. What is causing the socket to close?
connected
inConnect()
in your client code. Also, if you intend to assign to a global variable, you must use theglobal
keyword with the variable name in the function scope. – Joel Cornettconnected
global (I'll edit my question to reflect the change) and now I'm getting an error in the client too at the while loop statingconnected
is not defined, but it should be since I ranConnect()
which would set it toTrue
before entering the loop... – PurplProto