简体   繁体   中英

How sending and receiving works in Python sockets?

I'm working with python sockets for a while, and i wrote some good simple programs.

The problem that i face every time, is about sending and receiving methods in sockets ! Giving you a basic and simple example:

This is the receiver (server!):

 import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(('', 4001)) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.listen(5) while True: conn, addr = s.accept() print conn, addr data1 = conn.recv(64) data2 = conn.recv(64) print 'uname is %s , password is: %s' %(data1, data2, ) conn.close() 

And this is the sender (or client!):

 import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('', 4001)) uname = raw_input('enter username>') passw = raw_input('enter password>') s.send(uname) s.send(passw) print 'exiting ...' s.close() 

So the problem is, why server receives both uname and passw in first s.recv() method? It means data2 is always empty !

I really don't know what happens when client use s.send(). I was thinking that each s.send(), sends a "packet" to the destination (ip,port) !!

And can someone explain to me why this second code (another client) is working ??

 import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(('', 4001)) uname = raw_input('enter username>') s.send(uname) passw = raw_input('enter password>') s.send(passw) print 'exiting ...' s.close() 

I also faced similar problem. Then I implemented a protocol to send and receive one message at a time. Hope this link will help a lot : LINK

socket.SOCK_STREAM means you're communicating via TCP . This means, if you call send , your data is pushed to the system's networking stack. Both send calls are called shortly one after another.

If you use TCP, your system decides about the packet size. So the uname and passw might be sent in one packet or even split in any other way.

On the receiver's side, data1 receives up to 64 bytes, which is enough for uname and passw .

The explanation above also shows, why the second one works:

Here you need some time between sending uname and passw . During this time, your OS decides to send the packet (ie to flush the network buffer).

When you are using streams, you should not think in terms of packets but in terms of streams. There a send call only means: push some data on my network stack(like a pipeline).

If you are interested in packets, you might try to experiment with UDP:

socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

With such kind of socket your first sender would work as expected

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM