ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Slackware, Debian, Mac OS X, Zenwalk, Puppy, Gentoo
Posts: 199
Rep:
socket IPC..
I am trying to get some IPC working within a program. Basically I need iostream to print text every so many seconds, and at the same time be able to accept input from the keyboard (which would execute a function).
Currently I have a select() function which executes some code when it times out. It is set within a loop, so it executes every few seconds, which works well.
I (thought I had) set it up to execute a different piece of code if iostream input is received, but this part never gets executed.
I am vaguely familiar with IPC methods, would a different IPC method be more(or evenly) practical for this situation? A pipe maybe?, or message queues?
Do I need to use fork() to have one process that controls the timeout function, and another which checks for input?
Some pseudocode and keywords would be appreciated.
I am trying to get some IPC working within a program. Basically I need iostream to print text every so many seconds, and at the same time be able to accept input from the keyboard (which would execute a function).
Currently I have a select() function which executes some code when it times out. It is set within a loop, so it executes every few seconds, which works well.
I (thought I had) set it up to execute a different piece of code if iostream input is received, but this part never gets executed.
I am vaguely familiar with IPC methods, would a different IPC method be more(or evenly) practical for this situation? A pipe maybe?, or message queues?
Do I need to use fork() to have one process that controls the timeout function, and another which checks for input?
Some pseudocode and keywords would be appreciated.
IIRC there are socket related functions which tell how much data is in the socket and do not block. So, one doesn't even need a separate thread/fork, i.e. one can more or less implement the following concept:
Code:
unsigned desired_number_of_items = ...; // put real value here
some_type time_to_sleep_between_check = ...; // put real value here
for(;;)
{
if(number_of_items_in_socket(socket) < desired_number_of_items)
{
sleep(time_to_sleep_between_checks);
}
else
{
read_data_from_socket(socket);
break;
}
}
You may easily add timeout to the above pseudocode.
Distribution: Slackware, Debian, Mac OS X, Zenwalk, Puppy, Gentoo
Posts: 199
Original Poster
Rep:
Quote:
Why do you set
timeout.tv_sec
timeout.tv_usec
in three places ?
If a key is pressed, an action occurs and the loop begins again. I would like to keep the structure in timeout section to occur every .5 secs, so if a key is pressed I dont want the timer to start over at .5, but begin where it left off when the key is pressed. I assumed this would be the best way to do this.
Do you mean the timeout section of the nested if structure would occur every .5 seconds, if a key was pressed or not?
Quote:
Why is 'rc' defined outside the 'while' body ? Is it needed outside the 'while' body ?
I can't give you a good answer here, I'm am still trying to figure this stuff out.
Last edited by vendtagain; 12-25-2009 at 07:42 PM.
If a key is pressed, an action occurs and the loop begins again. I would like to keep the structure in timeout section to occur every .5 secs, so if a key is pressed I dont want the timer to start over at .5, but begin where it left off when the key is pressed. I assumed this would be the best way to do this.
Do you mean the timeout section of the nested if structure would occur every .5 seconds, if a key was pressed or not?
I can't give you a good answer here, I'm am still trying to figure this stuff out.
Suppose you initialized the timeout value to be 0.5 seconds and a key was pressed 0.25 seconds after the initialization.
If I understand you correctly, you want to wait for the next key to be pressed again at most 0.5 seconds. If it's the case, which part of your code will set timeout to become 0.5 seconds again ?
Regarding 'rc' and stuff like that - the rule of thumb is: one creates an object (in human, not necessarily OO) sense as late as possible and disposes of it as early as possible. Believe me, it really helps to keep mess away regardless of programming language.
Distribution: Slackware, Debian, Mac OS X, Zenwalk, Puppy, Gentoo
Posts: 199
Original Poster
Rep:
I think I need to find some guides and docs relating to sockets, IPC or (multiplexing?). to read through. I haven't been able to find any good ones yet.
If a key is pressed, an action occurs and the loop begins again. I would like to keep the structure in timeout section to occur every .5 secs, so if a key is pressed I dont want the timer to start over at .5, but begin where it left off when the key is pressed. I assumed this would be the best way to do this.
Maybe you should use setitimer to have it executed every .5s, then use select with a timeout of 0s when the timer is triggered. To do that you might start a thread blocked with a pthread condition that the timer releases, that way you don't interrupt a read in progress.
Kevin Barry
I agree this is a superb guide.
This is actually the one I've been using, I'm on my 3rd go-around.
I still can't get my head around a client that would be ready to receive info from a server, and also send info if something is inputted.
code below is a server I've developed based on beej's guide which should send info to all clients when it is received from any client.
Now I still can't figure how to implement a client that would wait for info being sent from the server, but would also be able to send info inputted via local cin input.
*main server loop...
Code:
int sockfd, new_fd;
fd_set master,read_fds,write_fds;
int fdmax;
struct sockaddr_storage remoteaddr;
socklen_t addrlen;
sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
fdmax = sockfd; //max so far
FD_SET(sockfd, &master);
for(;;)
{
write_fds = read_fds = master;
select(fdmax+1, &read_fds, &write_fds, NULL, &time_out);
for(i = 0; i <= fdmax; i++) {
{
if (FD_ISSET(i, &read_fds))
{
if (i == sockfd)// handle new connections
{ addrlen = sizeof remoteaddr;
newfd = accept(sockfd,(struct sockaddr *)&remoteaddr,&addrlen);
FD_SET(newfd, &master); //add new connection to master set
fdmax = newfd;
}
else
{
//client on socket i is "send()"ing info
nbytes = recv(i,bufdata,sizeof bufdata,0);
for(jj=0;jj<=fdmax;jj++)
{
if (jj!=sockfd && jj!=i)
{ //send data to all but listener and sender
send(jj,bufdata,nbytes,0);
}
}
}
}
if (FD_ISSET(i, &write_fds))
{
}
}
}
I'm not even sure how to look at getting the client set up (to wait for keyboard input and server socket recv()), or what to look into, so thought maybe posting the server would help someone give me some insight.
Last edited by vendtagain; 01-02-2010 at 12:23 AM.
Reason: minor fixes
Distribution: Slackware, Debian, Mac OS X, Zenwalk, Puppy, Gentoo
Posts: 199
Original Poster
Rep:
if (cin >> jj_input)
send(...);
else
recv(...);
?
?Seems like a fairly simple logic...just need cin and recv to be ready for action, simultaneously...without blocking each other
can cin, getline(), or keyboard inputstream be a sort of socket descriptor?
Last edited by vendtagain; 01-02-2010 at 12:45 AM.
Distribution: Slackware, Debian, Mac OS X, Zenwalk, Puppy, Gentoo
Posts: 199
Original Poster
Rep:
I didnt quite realize how threading worked, but from a few tests with pthreads, it seems I should be able to use them in this case effectively. Thanks for the input.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.