Re: network programming: how does s.accept() work?
- From: 7stud <bbxx789_05ss@xxxxxxxxx>
- Date: Mon, 25 Feb 2008 03:08:09 -0800 (PST)
On Feb 25, 2:43 am, bock...@xxxxxxxxxxx wrote:
On 25 Feb, 09:51, 7stud <bbxx789_0...@xxxxxxxxx> wrote:
I have the following two identical clients
#test1.py:-----------
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = 'localhost'
port = 5052 #server port
s.connect((host, port))
print s.getsockname()
response = []
while 1:
piece = s.recv(1024)
if piece == '':
break
response.append(piece)
#test3.py:----------------
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = 'localhost'
port = 5052 #server port
s.connect((host, port))
print s.getsockname()
response = []
while 1:
piece = s.recv(1024)
if piece == '':
break
response.append(piece)
and this basic server:
#test2.py:--------------
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ''
port = 5052
s.bind((host, port))
s.listen(5)
while 1:
newsock, client_addr = s.accept()
print "orignal socket:", s.getsockname()
print "new socket:", newsock.getsockname()
print "new socket:", newsock.getpeername()
I started the server, and then I started the clients one by one. I
expected both clients to hang since they don't get notified that the
server is done sending data, and I expected the server output to show
that accept() created two new sockets. But this is the output I got
from the server:
original socket: ('0.0.0.0', 5052)
new socket, self: ('127.0.0.1', 5052)
new socket, peer: ('127.0.0.1', 50816)
original socket: ('0.0.0.0', 5052)
new socket, self: ('127.0.0.1', 5052)
new socket, peer: ('127.0.0.1', 50818)
The first client I started generated this output:
('127.0.0.1', 50816)
And when I ran the second client, the first client disconnected, and
the second client produced this output:
('127.0.0.1', 50818)
and then the second client hung. I expected the server output to be
something like this:
original socket: ('127.0.0.1', 5052)
new socket, self: ('127.0.0.1', 5053)
new socket, peer: ('127.0.0.1', 50816)
original socket: ('0.0.0.0', 5052)
new socket, self: ('127.0.0.1', 5054)
new socket, peer: ('127.0.0.1', 50818)
And I expected both clients to hang. Can someone explain how accept()
works?
I guess (but I did not try it) that the problem is not accept(), that
should work as you expect,
but the fact that at the second connection your code actually throws
away the first connection
by reusing the same variables without storing the previous values.
This could make the Python
garbage collector to attempt freeing the socket object created with
the first connection, therefore
closing the connection.
If I'm right, your program should work as you expect if you for
instance collect in a list the sockets
returned by accept.
Ciao
----
FB
The question I'm really trying to answer is: if a client connects to a
host at a specific port, but the server changes the port when it
creates a new socket with accept(), how does data sent by the client
arrive at the correct port? Won't the client be sending data to the
original port e.g. port 5052 in the client code above?
.
- Follow-Ups:
- Re: network programming: how does s.accept() work?
- From: Thomas Bellman
- Re: network programming: how does s.accept() work?
- From: bockman
- Re: network programming: how does s.accept() work?
- From: 7stud
- Re: network programming: how does s.accept() work?
- References:
- network programming: how does s.accept() work?
- From: 7stud
- Re: network programming: how does s.accept() work?
- From: bockman
- network programming: how does s.accept() work?
- Prev by Date: Re: float / rounding question
- Next by Date: Re: network programming: how does s.accept() work?
- Previous by thread: Re: network programming: how does s.accept() work?
- Next by thread: Re: network programming: how does s.accept() work?
- Index(es):
Relevant Pages
|