简体   繁体   English

如何在Python套接字中发送和接收?

[英]How sending and receiving works in Python sockets?

I'm working with python sockets for a while, and i wrote some good simple programs. 我正在使用python套接字一段时间,我写了一些很好的简单程序。

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? 所以问题是,为什么服务器在第一个s.recv()方法中同时接收uname和passw? It means data2 is always empty ! 这意味着data2总是空的!

I really don't know what happens when client use s.send(). 我真的不知道当客户端使用s.send()时会发生什么。 I was thinking that each s.send(), sends a "packet" to the destination (ip,port) !! 我在想每个s.send()都会向目的地发送一个“数据包”(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 希望这个链接能够提供很多帮助: LINK

socket.SOCK_STREAM means you're communicating via TCP . socket.SOCK_STREAM表示您正在通过TCP进行通信。 This means, if you call send , your data is pushed to the system's networking stack. 这意味着,如果您调用send ,您的数据将被推送到系统的网络堆栈。 Both send calls are called shortly one after another. 两个send呼叫都是一个接一个地调用。

If you use TCP, your system decides about the packet size. 如果使用TCP,则系统会决定数据包大小。 So the uname and passw might be sent in one packet or even split in any other way. 因此, unamepassw可以在一个数据包中发送,甚至可以以任何其他方式拆分。

On the receiver's side, data1 receives up to 64 bytes, which is enough for uname and passw . 在接收方, data1最多接收64个字节,这对于unamepassw来说已经足够了。

The explanation above also shows, why the second one works: 上面的解释也表明,为什么第二个工作原理:

Here you need some time between sending uname and passw . 在这里你需要一些时间发送unamepassw 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). send调用仅意味着:在我的网络堆栈上推送一些数据(如管道)。

If you are interested in packets, you might try to experiment with UDP: 如果您对数据包感兴趣,可以尝试使用UDP进行实验:

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

With such kind of socket your first sender would work as expected 使用这种类型的套接字,您的第一个发送方将按预期工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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