I have two files. server.py and client.py.
Client's code:
import socket
s = socket.socket()
try:
s.connect(("localhost", 17894))
except socket.error:
print "Couldn't connect."
raise # Useless, I know, but once I fix it I will change to exit()
data = ' '
BUFFER_SIZE = 1024
WELCOME_MSG = """Connecting to server.
The server should send you a list of commands when connected.
"""
print WELCOME_MSG
print s.recv(BUFFER_SIZE) # Receive server's welcome message
while data:
cmd = raw_input(">>> ")
s.send(cmd)
stuff = 1
data = s.recv(BUFFER_SIZE)
print data
print "Server closed connection."
s.close()
Server's code:
import socket
s = socket.socket()
s.bind(("localhost", 17894))
s.listen(1)
BUFFER_SIZE = 1024
WELCOME_MSG = """...stuff..."""
while True:
con, addr = s.accept()
con.send(WELCOME_MSG)
while True:
data = s.recv(BUFFER_SIZE).split(" ")
# some data processing, sets ans to a string
con.send(str(ans))
con.close()
I am running the server and then the client. Everything as expected, I receive the welcome message and it is displayed, the problem starts when I try and enter a command (or just try to send anything to the server)
Client's error:
Traceback (most recent call last):
File "...path.../Client.py", line 22, in <module>
data = s.recv(BUFFER_SIZE)
socket.error: [Errno 10053] An established connection was aborted by the software in your host machine
Server's error:
Traceback (most recent call last):
File "...path.../Server.py", line 32, in <module>
data = s.recv(BUFFER_SIZE).split(" ")
socket.error: [Errno 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
The line number is different because I removed some imports
Socket programming is pretty new to me, so I don't know what is happening and how to fix it. Help would be appreciated!