Socket.error: [Errno 111] when trying to connect to a socket

I was trying to write code where the client connects to the server on the default port number, then the server sends a different port number to the client. The client now connects to the new port number.

Customer:

import socket
import sys
import os
import signal
import time
s = socket.socket()
s.connect(("127.0.0.1", 6667))
line = s.recv(1024)
if line.strip():
    port = int(line)
    s.close()
    soc = socket.socket()
    soc.connect(("127.0.0.1", port))
    print soc.recv(1024)
    soc.close()
else:
    s.close()

      

Server:

import socket
import sys
import os
import signal
import time
port = 7777
s = socket.socket()
s.bind(("127.0.0.1", 6667))
s.listen(0)
sc, address = s.accept()
print address
sc.send(str(port))
sc.close()
s.close()
sock = socket.socket()
sock.bind(("127.0.0.1", port))
soc, addr = sock.accept()
print addr
soc.send("Success")
soc.close()
sock.close()

      

When I execute this code, I get the following client and server side errors.

Server:

('127.0.0.1', 36282)
Traceback (most recent call last):
  File "server.py", line 17, in <module>
    soc, addr = sock.accept()
  File "/usr/lib/python2.7/socket.py", line 202, in accept
    sock, addr = self._sock.accept()
socket.error: [Errno 22] Invalid argument

      

Customer:

Traceback (most recent call last):
  File "client.py", line 13, in <module>
    soc.connect(("127.0.0.1", port))
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 111] Connection refused

      

Can someone explain to me the cause of these errors and provide a solution for these errors.

+3


source to share


1 answer


Before you can listen

connect to a TCP / IP (connection based streaming socket) socket, you need to use bind

to assign a socket (created with socket.socket()

). Then you need to do listen

to prepare it for incoming connections and then finally do accept

on the prepared socket.

It looks like you are lost sock.listen(0)

after your call sock.bind(("127.0.0.1", port))

. The Python documentation is short, but it talks about TCP / IP:

Note that the server has to do the sequence socket () , bind () , listen () , accept () (perhaps repeating accept () to serve more than one client), whereas the client only needs the sequence socket (), connect () ... Also note that the server is not sending all () / recv () on the socket it is listening on, but on the new socket returned by accept ().



Python bases its socket module on the Berkeley Socket model. You can find more information on Berkeley Sockets at this link . In particular, it speaks about bind

:

bind () assigns a socket to an address. When a socket is created using socket (), it is only given the protocol family, but not assigned an address. This connection to the address must be done with the bind () system call before the socket can accept connections to other hosts.

Also consider what happens if your client sends a port number (and tries to connect) before the server starts listening for connections (in this case, on port 7777). While not the cause of your problems, I would like to point out the completeness scenario. Something you might think of does not cover the port 6667 socket until you name listen

on the port 7777 connector. After the call, listen

you can close the first socket. On the client, after reading the port, you can wait until the first connection (port 6667) is closed by the server, and then connect to port 7777.

+1


source







All Articles