473,398 Members | 2,113 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How to design multiple timeout in Socket Programming(Linux C)

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
Oct 22 '07 #1
12 14512
sicarie
4,677 Expert Mod 4TB
What language are you implementing this in?
Oct 22 '07 #2
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?
Oct 22 '07 #3
sicarie
4,677 Expert Mod 4TB
Moved to C/C++ Forum.
Oct 22 '07 #4
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
Oct 22 '07 #5
sicarie
4,677 Expert Mod 4TB
Sophie000-

I have moved your original question (above). There is no reason to double post (which happens to be against the rules of this site). Please have patience for someone to answer your question.
Oct 22 '07 #6
RRick
463 Expert 256MB
Select is the function that is usually used for these types of socket problems.

The problem is that select has a global timeout, which means that the application maintains the value.

If your server is idle, and has no clients, then the timeout is NULL.

If you are processing one or more requests, then you need to keep a list of timeout values for each request and update them each time select returns.

For example, lets assume you are waiting for a single client with a timeout of 10 seconds. If a new request comes in after 4 seconds, then you create a client request with a timeout of 10 seconds and send the request out. The existing request must have its timeout adjusted to 6 seconds.

Now you start up select again, with the smallest timeout (i.e. 6 seconds). And away you go.
Oct 22 '07 #7
Thanks RRick. I Got it. So I have to create a list that stores all tasks, their corresponding information and their timeout. And it seems that I do not need fork().

Is there other ways to do it? such as interrupt. signal, wait..? Anyway, it is a very good idea to use select() in this way. I never thought it before.

P.S. Sorry for duplicating my article in two broads since I thought the forum leader wanted me to move to another broad.
Oct 22 '07 #8
RRick
463 Expert 256MB
You don't want to use signals (like alarm) unless you really know what you are doing. Signals and select and IO generally don't mix very well. There are lots of caveats and each platform is different.

Fork/exec creates a new process and this is a another common approach to socket communication. Now you can have multiple selects running and they don't get in the way of each other. You just have to make sure that you can complete the task at hand in a single executable.

One downside to forking is the time it takes to start a new process. The process creation time can be longer than it takes to complete the task.
Oct 22 '07 #9
Based on my understanding, we use select() to monitor multiple tasks which are TO BE DONE. And when these tasks are BEING DONE, in order to avoid them from blocking other tasks, we can set the sockets of these tasks as non-block before using them. My questions are:

1. Why some examples I saw use select() + block-based socket mode and some others are select() + non-block-based socket, why not just using select() + non-block-based socket?
2. Suppose one socket is doing send() but TCP buffer is full, if we use select() + block-based socket mode, then send() will block. If finally send() fails, error is reported. While if we use select() + non-block-based socket mode, when data is waiting to be moved to TCP, computer will execute other tasks and let kernel to keep on moving data to TCP buffer. In this case, if send() finally fail, where does computer catch error?
Oct 23 '07 #10
RRick
463 Expert 256MB
As for #2, don't expect select to fix problems with send and other IO routines. The problem you describe is an IO deadlock situation between two processes and select won't/can't solve it.

Your protocol (i.e. comunication between 2 processes) has to deal with situations like this and make sure the buffers are dealt with in a timely manner. I like to think of the bare socket communication as a "Wild West" scenario. Anything goes and anything can happen! Its up to you to control the situation.
Oct 23 '07 #11
It is very kind of you to answer me so many question. I appreciate it.
Would please let me know if I have understood your meaning:

suppose I have a select() in loop:
while(1){
...
select();
...
if(...){
recv a response packet from a remote server
connect(sockfd, &remove_server, sizeof(remote_server));
...
}
}
unfortunately, the remote server is busy with a lot of other connections and it does not have time to accept my connection. If sockfd is non-blocked, then this connection not be blocked. And later on, nobody wiil know if it is finally connected or gives up?
Oct 24 '07 #12
And it is my work to design the code to catch the result? I do not have good idea how to solve it. Maybe in this case, blocked socket is better.

It is very kind of you to answer me so many question. I appreciate it.
Would please let me know if I have understood your meaning:

suppose I have a select() in loop:
while(1){
...
select();
...
if(...){
recv a response packet from a remote server
connect(sockfd, &remove_server, sizeof(remote_server));
...
}
}
unfortunately, the remote server is busy with a lot of other connections and it does not have time to accept my connection. If sockfd is non-blocked, then this connection not be blocked. And later on, nobody wiil know if it is finally connected or gives up?
Oct 24 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: yoelgold | last post by:
Hi I want to start writing a new site. It will include 3 forms that will collect information from the user. I know how to keep the info in sessions etc. my question is about the design of the...
7
by: TrickyNick | last post by:
Hi, I am wondering how I can design multiple stylesheets for my webpage and let the user choose the one that they would like to use. Any help would be appreciated. If you want, check out my...
1
by: Tim Smith | last post by:
Hi, I have been considering how to personalize mini functional applications and I was wondering if there is an easy way to do the following for an client application design a) User runs...
1
by: Joe Hughes | last post by:
Hi All I'm a seasoned ASP programmer and have been writing ASP for years. I've decided now to stop writing ASP and jump onboard the ASP.NET bandwagon (albeit a bit late). I've been...
0
by: Marc DVer | last post by:
I am at kind of a loss on how to design a certain database project I am working on. Basically, we have a proprietary program with a standard backend (though we do not have direct write access to...
4
by: clintonG | last post by:
I'm using the 2.0 Wizard control which includes three basic control types in its various steps; TextBox, DropDownList, and CheckBoxList controls. I'd like to pass the results of the Wizard to a...
10
by: ColoradoGeiger | last post by:
I have a fairly standard server application that I am using asynchronous socket calls to make connections, send and receive data, and all of that jazz. No blocking methods was my goal in writing...
3
by: Jaco Naude | last post by:
Hi I am trying to think of an efficient way of implementing a logging system for a big program. My thoughts are to create a message logging base class and letting all objects that need logging...
1
by: Jetean | last post by:
Hi: How to make connections to multiple different Socket Servers Asyncronously? What I looking at is to have a program (C#) that log into different servers. I know to to code 1 client to 1...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.