Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

socket programming...

Status
Not open for further replies.

huskers

Programmer
Jan 29, 2002
75
US
I have a server running which accepts connections from clients and gets the data from them and puts it on a message queue. This has been running fine on AIX boxes till now. I am trying to port this code to Linux. The alarms aren't working as expected. When I accept a connection from a client, I use read() function to read the data from the socket. Now since alarms aren't working as expected on Linux is there a way to set a timeout in the read().

After accepting the connection I just want to read the data only till a certain time after which i would like to drop the client. Is there a way to do this?

Thanks...
 
> The alarms aren't working as expected.

The problem is most likely that you are using signal(), glibc follows the BSD signal behavior, so you need to use sigaction() not signal().

Hope this helps.
 
Thanks for the response. Can you give me any examples which use sigaction.
 
> Can you give me any examples which use sigaction.

Ok, I always get into trouble when I do this, but basically, here's some code that should be close.

signal() is just a wrapper around sigaction, so it doesn't modify the action, use sigaction directly to modify the action.

This code shows how to specify a signal handling function as well as modify the default action of the signal. This is not a working program (obviously), but should show what you need.

Code:
static void sig_alrm(int signo) { // our signal handler
  // do something
  return;
}
now in your function where you read from the socket...

Code:
  struct sigaction nact,oact;

  nact.sa_handler = sig_alrm;
  sigemptyset(&nact.sa_mask);
  nact.sa_flags = SA_INTERRUPT; // set signal to interrupt

  sigaction(SIGALRM, &act, &oact); // instead of signal, call sigaction.

  alarm(5); // SIGALRM after 5 seconds...

  if (read(socket,buff,BUFFLEN) < 0) {  // then timed out  
     // do something...
  }
  alarm(0);

Hope this helps. I believe it to be correct, but if something is amiss, let me know. I don't have any working code in front of me & I often make mistakes.

Good Luck!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top