如何在套接字编程中设计多个超时(Linux C) [英] How to design multiple timeout in Socket Programming(Linux C)

查看:61
本文介绍了如何在套接字编程中设计多个超时(Linux C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施点对点程序:当计算机A收到?获取文件XXX?来自stdio,它广播(泛洪)请求数据包以询问其他人。计算机B和C都有文件,因此他们发回回复。计算机A拿起B并同时向B.发送确认,它创建一个服务器套接字来监听连接。在计算B收到来自A的确认之后,它创建一个连接到A的客户端套接字。


我设置超时请求和确认数据包。如果A超时后没有收到响应,则A需要再次泛洪新的请求报文。否则,A取消请求的超时。出于同样的原因,如果B没有连接到A,A会向C发送一个确认,以便C可以连接到A


我的问题:如何设置超时?

a。我可以使用select()。但是因为我已经在while循环中将一个带超时参数的select()作为NULL来监视stdio,请求,确认/响应数据包,所以似乎没有地方可以将其他选择与超时参数放在一起。

b。我可以使用alarm()。但是无法知道哪个请求/确认alarm()用于哪个文件。如果我使用alarm(0)进行超时,则使用alarm()的其他超时也会被取消。

c。我还考虑过使用fork()+ select()或alarm:但是在子进程终止后,我怎么知道它因超时或接收到响应或连接而被终止?此外,如果由于时间问题而终止,我应该在哪个部分程序重新发送请求或重新发送确认数据包?

d。也许我可以使用SIGALRM在我的子进程中设置状态,并使用wait()/ waitpid?但我不知道在哪里放置waitpid(),以便父进程不会阻止其他进程。


我想解决问题并需要你的帮助。任何宗师都可以回答这个问题吗?谢谢

I am implementing a peer-to-peer program: When a computer A receives ?get file XXX? from stdio, it broadcasts (floods) a request packet to ask others. Both Computer B and C have the file so they send back responses. Computer A pick up B and sends confirm to B. at the same time, it creates a server socket to listen to connection. After Compute B receives the confirm from A, it creates a client socket to connects to A.

I set timeouts to both request and confirm packets. If no response is received by A after timeout, A needs to flood a new request packet again. Otherwise, A cancels timeout of the request. For the same reason, if B does not connect to A, A will send a confirm to C so that C can connect to A

My Questions: How to set timeout?
a. I can use select(). But since I already put a select() with timeout parameter as NULL in a while loop to monitor stdio, request, confirm/response packets, it seems that there is no place to put other selects with timeout parameters。
b. I can use alarm(). But there is no way to know, which request/confirm of which file the alarm() is for. And if I cancle a timeout using alarm(0), other timeout using alarm() is also canceled.
c. I also thought about using fork()+ select() or alarm: But after the child process is terminated, how do I know it is terminated because of timeout or because response or connection is received? Besides, If it is terminated because of timout, In which part of program should I re-flood the request or re-send the confirm packet?
d. maybe I can use SIGALRM to set status in my child process, and use wait()/waitpid? But I do not know where to put waitpid() so that the parent process does not block other process.

I want to solve the problem and need your help. Can any guru answer the question? Thanks

推荐答案

你在linux.thanks中实现了什么语言?
What language are you implementing this in?


c。


我想我需要实施某事。这样父进程可以随时捕获子进程的中断。同时,父进程可以执行其他操作而不会被其子进程阻止。但是如何?
c in linux.thanks.

I think I need to implement sth. so that the interrupts of child processes can be caught by parent process anytime. At the same time, parent processes can do other things without being blocked by its child process. But how?


已移至C / C ++论坛。
Moved to C/C++ Forum.


这篇关于如何在套接字编程中设计多个超时(Linux C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆