Sunday 15 February 2015

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



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

i trying write code client connects server on default port number, server sends port number client. client connects new port number.

client:

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 execute code, getting next errors on client , server sides.

server:

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

client:

traceback (most recent phone 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 homecoming getattr(self._sock,name)(*args) socket.error: [errno 111] connection refused

can explain me reason these errors , provide solution these errors.

before can listen tcp/ip socket (a connection based streaming socket) need utilize bind assign socket (created socket.socket()) . need listen prepare incoming connections , accept on prepared socket.

you appear missing sock.listen(0) after phone call sock.bind(("127.0.0.1", port)). python documentation short on details tcp/ip:

note server must perform sequence socket(), bind(), listen(), accept() (possibly repeating accept() service more 1 client), while client needs sequence socket(), connect(). note server not sendall()/recv() on socket listening on over new socket returned accept().

python bases socket module on berkeley socket model. can find more detailed info on berkeley sockets @ link . in particular says bind:

bind() assigns socket address. when socket created using socket(), given protocol family, not assigned address. association address must performed bind() scheme phone call before socket can take connections other hosts.

also consider happen if client gets sent port number (and tries connect) before server starts listening connections (on port 7777 in case). although not cause of problems, wanted point out scenario completeness. may consider not closing port 6667 socket until after have called listen on port 7777 socket. after calling listen can close downwards first socket. on client after reading port can wait until first connection (port 6667) closed downwards server , connect port 7777.

python sockets ports

No comments:

Post a Comment