简体   繁体   English

c ++ Winsock发送,recv-他们的工作方式

[英]c++ Winsock send,recv -how they work

I'm new in network programming and I'm trying to understand how functions like send and recv work under the hood in a TCP connection.I know that in a connection between a client and a server for example ,when the client manage to send a message to the server ,the message is split in different packages and at it's arrival,the server part checks to see if the sum of the packages is the same as it was before sending,and if is ok it sends a message back to the client as an approval.If a problem appears the client resends the message. 我是网络编程的新手,我正在尝试理解发送和recv等功能如何在TCP连接下工作。我知道在客户端和服务器之间的连接中,例如,当客户端设法发送时一条消息发送到服务器,消息被分成不同的包,当它到达时,服务器部分检查包的总和是否与发送前相同,如果没有,它会将消息发送回客户端作为批准。如果出现问题,客户端将重新发送该消息。

What I don't understand is that if you send a message from the client and you sleep the server for 10 seconds,you can still do what you want in the client,like the send function is executing in another thread ,or if you use multiple times send function in these 10 seconds,the message arrives as a combination of the messages used in that time. 我不明白的是,如果你从客户端发送一条消息,你在服务器上睡10秒钟,你仍然可以在客户端做你想做的事情,比如send函数在另一个线程中执行,或者你使用在这10秒内多次发送功能,消息作为该时间内使用的消息的组合到达。

If anyone can explain the situation ,I'll be very grateful ! 如果有人能解释一下情况,我将非常感激!

This is implemented by the TCP/IP networking stack of your operating system. 这是由您的操作系统的TCP / IP网络堆栈实现的。

The TCP/IP stack ... TCP / IP堆栈......

  • provides a send buffer. 提供发送缓冲区。 When your program sends, the OS first fills internal buffers. 程序发送时,操作系统首先填充内部缓冲区。 You app can send immediately until the buffers are full. 您的应用程序可以立即发送,直到缓冲区已满。 Then your send will block. 然后你的发送将阻止。
  • takes data from the internal buffer and sends it out onto the network in single packets. 从内部缓冲区获取数据并以单个数据包将其发送到网络。
  • receives data over the network and fills internal receive buffers with that data. 通过网络接收数据并使用该数据填充内部接收缓冲区。
  • gives your program the data from the internal buffers when you call receive. 当您调用receive时,为您的程序提供来自内部缓冲区的数据。
  • takes care of the TCP/IP protocol stuff like establishing connections, acknowledging received data, resending data if no receive acknowledge was received. 负责TCP / IP协议的工作,如建立连接,确认收到的数据,如果没有收到接收确认,则重新发送数据。

In the case you wrote the client is filling the sender OS's send buffer and the receiver OS's receive buffer. 在您编写的情况下,客户端正在填充发送方OS的发送缓冲区和接收方OS的接收缓冲区。 Your client can send non-blocking until both buffers are full. 您的客户端可以发送非阻塞,直到两个缓冲区都已满。 Then it will block until the server calls recv again. 然后它将阻塞,直到服务器再次调用recv。

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

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