I'm trying to understand socket programming in python by experimenting a little. I'm trying to create a server that you can connect to by telnet and that echoes what you type in the telnet prompt. I don't want to start using threads just yet. This is my code.
import socket
host = "127.0.0.1"
port = 8080
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((host,port))
sock.listen(1)
remote, address = sock.accept()
print "Connection from", address
while True:
data = sock.recv(1024)
remote.send(data)
The server starts without errors, but when I connect with the telnet client I get, on the client side:
> telnet 127.0.0.1 8080
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Connection closed by foreign host.
and on the server side:
> python server_test.py
Connection from ('127.0.0.1', 35030)
Traceback (most recent call last):
File "server_test.py", line 16, in <module>
data = sock.recv(1024)
socket.error: [Errno 107] Transport endpoint is not connected
Can anyone tell me what this error comes from and how to solve it?