recvfrom and timeout signal
Christopher Faylor
cygwin@cygwin.com
Tue Feb 19 15:17:00 GMT 2002
On Tue, Feb 19, 2002 at 11:51:59PM +0100, Piotr Stepien wrote:
>Christopher,
>
>Hope this working test case is small enough. I removed all unecessary code.
>Please let me know if you need anything else.
I wasn't asking for personal email. That's precisely why I set the
Reply-To and Mail-Followup-To to go to the cygwin mailing list.
"Someone" does not necessarily mean me.
cgf
>-----Original Message-----
>From: cygwin-owner@cygwin.com [mailto:cygwin-owner@cygwin.com]On Behalf Of
>Christopher Faylor
>Sent: Tuesday, February 19, 2002 10:42 PM
>To: cygwin@cygwin.com
>Subject: Re: recvfrom and timeout signal
>
>
>On Tue, Feb 19, 2002 at 10:28:02PM +0100, Piotr St?pie? wrote:
>>Hi All,
>>
>>I try to implement timeout for recvfrom:
>>
>> struct sigaction action;
>> action.sa_handler = alarm_signal;
>> action.sa_flags = 0;
>> sigemptyset(&(action.sa_mask)); /* ignore all known signals */
>> sigaction(SIGALRM,&action,NULL); /* ensures that SA_RESTART is NOT set
>*/
>>
>> alarm(TIMEOUT);
>> int st=recvfrom(sockfd,buff,sizeof(buff),0,(sockaddr*)&srv_addr,&sz);
>> if(st == -1){
>> if(errno==EINTR){
>> /* timeout */
>> }else{
>> alarm(0); /* reset alarm */
>> }
>> }
>>
>>but the process is never woken by SIGALRM when no packet is received.
>>The recvfrom waits forever.
>>
>>Has anybody an idea what can be wrong ?
>
>Nope. Send an actual small working test case and someone will probably
>investigate it.
-------------- next part --------------
#include<iostream.h>
#include<iomanip.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<string.h>
#include<errno.h>
#include<sys/wait.h>
#include<signal.h>
#define BADSIGNAL ((void(*)(int))-1)
#define TIMEOUT 5
void err_dump(const char* msg){
cerr << msg << ", errno=" << errno << endl;
exit(1);
}
void alarm_signal(int){
cout << "signal received\n";
cout.flush();
}
int main(){
struct sockaddr_in cli_addr,srv_addr;
bzero( (char*)&srv_addr, sizeof(srv_addr) );
srv_addr.sin_family = AF_INET;
srv_addr.sin_addr.s_addr = inet_addr("255.255.255.255");
srv_addr.sin_port = htons(192);
bzero( (char*)&cli_addr, sizeof(cli_addr) );
cli_addr.sin_family = AF_INET;
cli_addr.sin_addr.s_addr = htonl(INADDR_ANY);
cli_addr.sin_port = htons(0);
int sockfd = socket(AF_INET,SOCK_DGRAM,0);//IPPROTO_UDP);
if(sockfd == -1){
err_dump("can't open socket");
}
{
int on=1;
if(setsockopt(sockfd,SOL_SOCKET,SO_BROADCAST,&on,sizeof(on))==-1){
err_dump("error setsockopt");
}
}
if(bind(sockfd,(sockaddr*)&cli_addr,sizeof(cli_addr))== -1){
err_dump("can't bind to port");
}
char buff[0x100];
/*** BEGIN AREA OF INTEREST ***/
struct sigaction action;
action.sa_handler = alarm_signal;
action.sa_flags = 0;
sigemptyset(&(action.sa_mask)); /* ignore all known signals */
sigaction(SIGALRM,&action,NULL); /* ensures that SA_RESTART is NOT set */
int sz=sizeof(srv_addr);
alarm(TIMEOUT);
int st=recvfrom(sockfd,buff,sizeof(buff),0,(sockaddr*)&srv_addr,&sz);
/*** END AREA OF INTEREST ***/
if(st == -1){
if(errno==EINTR){
cout << "timeout occured\n";
}else{
alarm(0);
err_dump("error recvfrom");
}
}
close(sockfd);
return 0;
}
-------------- next part --------------
--
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Bug reporting: http://cygwin.com/bugs.html
Documentation: http://cygwin.com/docs.html
FAQ: http://cygwin.com/faq/
More information about the Cygwin
mailing list