3次握手过程中丢包
我需要在用 C++(使用 Winsock 2)编写的客户端和服务器应用程序之间执行 3way 停止并等待握手以进行分配。客户端发送 SYN。服务器递增客户端序列号并发送 SYN-ACK。客户端递增服务器序列号并发送 ACK。
为了模拟数据包丢失,客户端和服务器连接到中间路由器程序,该程序偶尔不转发数据包,从而导致丢失。我应该如何构建我的 sendto()、select()、recvfrom() 语句以解决可能的数据包丢失问题?
例如,如果SYN丢失并且客户端超时,则重新发送。如果 SYN-ACK 丢失,客户端超时并重新发送 SYN。我不知道如果 ACK 丢失了该怎么办。
编辑:在我的客户端上基本上是这样的:
- 发送 SYN。
- select() 语句失败时循环并重新发送 SYN
- 接收 SYN-ACK
- 发送 ACK
- 如果 ACK 丢失,则循环可能重新接收 SYN-ACK。
我如何知道何时停止循环?
I need to perform a 3way stop and wait handshake between client and server applications written in c++ (with winsock 2) for an assignment. The client sends a SYN. The server increments the client sequence number and sends a SYN-ACK. The client increments the server sequence number and sends an ACK.
To simulate packet loss, the client and server are connected to an intermediary Router program that occasionally does not forward packets, causing loss. How should I structure my sendto(), select(), recvfrom() statements to account for possible packet loss?
For example, if the SYN is lost and client times out, resend it. If the SYN-ACK is lost, client times out and resends SYN. I have no idea what to do if the ACK is lost though.
Edit: It basically goes like this on my client:
- Send SYN.
- Loop while select() statement fails and resend SYN
- Receive SYN-ACK
- Send ACK
- Loop to possibly re-receive SYN-ACK if ACK was lost.
How do I know when to stop looping?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
select
可用于检查何时可以在不阻塞的情况下读取套接字。至于检查 ACK 属于哪个发送的数据包,您可以检查序列号。
如果您在超时时间内没有收到 ACK,您只需重新发送最后一个数据包即可。无论您发送哪种数据包,如果您没有收到 ACK,您都应该重新发送它。
select
can be used to check when a socket can be read from without blocking.As for checking which sent packet the ACK belongs to, you check the sequence number.
If you don't receive an ACK within the timeout, you simple re-send the last packet. It doesn't matter what kind of packet you sent, if you don't receive an ACK you should resend it.