From: npunmia@hss.hns.com
To: linux-kernel@vger.kernel.org
Subject: Problem with select in Linux!
Date: Fri, 11 May 2001 14:37:11 +0530 [thread overview]
Message-ID: <65256A49.0030CA26.00@sandesh.hss.hns.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 1099 bytes --]
Hi,
I am facing 2 problems in Linux 2.2.16-22:
[1] In the server program (tmgr_main1.c) attached herewith, i am waiting on
events on the select system call on 3 sockets.
First I run the server program and its 3 client programs. If do a sendto in
this program just before waiting for select or do a sendto in response to read
events received by select, the message sent goes to the respective client and
the programs work perfectly fine.
However If i run only the server and do not run the client programs, then the
sendto done in the server program sends a message/event to the server itself (
which is waiting on events on that sockfd also) .
When i tried the above thing on SunOS 5.5.1 it worked perfectly fine!
(See attached file: server.c)
The output of this program is as follows:
[npunmia@msatpd1 tm1]$ ./a.out
In Test Manager main function !
Signal Handler installed!
Message sent to MRS!
Waiting on select system call....
Data received from MRS!
Error in receive!
Servicing a MRS request!
Message received =
[-- Attachment #2: Type: text/plain, Size: 1676 bytes --]
?c
Waiting on select system call....
Shutting down the test manager!
[npunmia@msatpd1 tm1]$
[2] The second problem occurs when i do a sendto 2 or more times in this server
program. This time also I run only the server and do not run the
destination/clients programs. The point to note here is that i get a Connection
Refused error every second time i do a sendto! The first time the message is
sent correctly to the client , the second time i get Connection Refused error ,
third time the message is sent correctly to the client, and so on.
(I have commented the second sendto before select . To try problem no. 2 you
can remove the comments and run it)
a) Why am i not getting a connection refused error consistently every time
i do a sendto?
b) Why at all am i getting a connection refused error in UDP in the first
place??
c) When i try the above thing on SunOS, the message is sent from the server
correctly every time, even though the client processes are not alive and i do
not get any error!!!
The output on running the above program(remove the comments from the second
sendto before select) is as follows:
[npunmia@msatpd1 tm1]$ ./a.out
In Test Manager main function !
Signal Handler installed!
Message sent to MRS!
Error in send to MRS
Error: Connection refused
Waiting on select system call....
Shutting down the test manager!
[npunmia@msatpd1 tm1]$
Regards,
Niraj Punmia,
Software Engineer,
Hughes Software Systems,
Plot 31, Sector 18,
Gurgaon- 122 015,
Haryana, India.
Off: 0124-6346666. Extn 2350.
[-- Attachment #3: server.c --]
[-- Type: application/octet-stream, Size: 5570 bytes --]
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<signal.h>
#include<sys/param.h>
#include<sys/time.h>
#define TM_GUI_PORT 60000 // in TM for GUI
#define TM_MRS_PORT 60001 // in TM for MRS
#define TM_MP_PORT 60002
#define GUI_TM_PORT 50000 // in GUI for TM
#define MRS_TM_PORT 50001 // in MRS for TM
#define MP_TM_PORT 50002
//#define TM_HOST_ADDR "139.85.243.90"
int guiSockFd, mrsSockFd, mpSockFd;
void terminateTmgr()
{
printf("Shutting down the test manager!\n\t");
close(guiSockFd);
close(mrsSockFd);
close(mpSockFd);
exit(0);
}
void timerHandler()
{
printf("Send heartbeat signal to SSE\n");
}
int main()
{
int cli_len, maxfdp1;
struct sockaddr_in guiSockAddr, mrsSockAddr, mpSockAddr, cli_addr;
char mesg[50];
fd_set allset;
fd_set rset;
struct itimerval value;
printf("\n\tIn Test Manager main function !\n\t");
signal(SIGINT,terminateTmgr);
signal(SIGQUIT,terminateTmgr);
signal(SIGHUP,terminateTmgr);
signal(SIGALRM,timerHandler);
printf("Signal Handler installed!\n\t");
value.it_interval.tv_sec = 5; //reset value, actual timer value
value.it_interval.tv_usec = 0;
value.it_value.tv_sec = 1; // only once, first time, countdown
value.it_value.tv_usec =0;
/*if(setitimer(ITIMER_REAL,&value,NULL) < 0)
{
printf("Error in timer\n");
}*/
if ((guiSockFd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
printf("Socket System Call failed!\n\t");
exit(1);
}
bzero((char *) &guiSockAddr, sizeof(guiSockAddr));
guiSockAddr.sin_family = AF_INET;
guiSockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
guiSockAddr.sin_port = htons(TM_GUI_PORT);
if (bind(guiSockFd, (struct sockaddr *) &guiSockAddr, sizeof(guiSockAddr)) < 0) {
printf("Error in bind!\n\t");
exit(1);
}
if ((mrsSockFd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
printf("Socket System Call failed!\n\t");
exit(1);
}
bzero((char *) &mrsSockAddr, sizeof(mrsSockAddr));
mrsSockAddr.sin_family = AF_INET;
mrsSockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
mrsSockAddr.sin_port = htons(TM_MRS_PORT);
if (bind(mrsSockFd, (struct sockaddr *) &mrsSockAddr, sizeof(mrsSockAddr)) < 0) {
printf("Error in bind!\n\t");
exit(1);
}
if ((mpSockFd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
printf("Socket System Call failed!\n\t");
exit(1);
}
bzero((char *) &mpSockAddr, sizeof(mpSockAddr));
mpSockAddr.sin_family = AF_INET;
mpSockAddr.sin_addr.s_addr = htonl(INADDR_ANY);
mpSockAddr.sin_port = htons(TM_MP_PORT);
if (bind(mpSockFd, (struct sockaddr *) &mpSockAddr, sizeof(mpSockAddr)) < 0) {
printf("Error in bind!\n\t");
exit(1);
}
FD_ZERO(&allset);
FD_SET(guiSockFd, &allset);
FD_SET(mrsSockFd, &allset);
FD_SET(mpSockFd, &allset);
//printf("%d %d %d\n",guiSockFd,mrsSockFd,mpSockFd);
maxfdp1 = mpSockFd + 1;
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(MRS_TM_PORT);
if(sendto(mrsSockFd, &mesg, sizeof(mesg), 0, &cli_addr, sizeof(cli_addr)) < 0)
{
printf("Error in send to MRS\n");
perror("Error");
}
else
{
printf("Message sent to MRS!\n\t");
}
/* Remove the following comment for probelm no. 2 ! */
/*
if(sendto(mrsSockFd, &mesg, sizeof(mesg), 0, &cli_addr, sizeof(cli_addr)) < 0)
{
printf("Error in send to MRS\n");
perror("Error");
}
else
{
printf("Message sent to MRS!\n\t");
} */
while(1)
{
printf("Waiting on select system call....\n\t");
rset = allset;
if(select(maxfdp1, &rset, NULL, NULL, 0) < 0)
{
//printf("Error in select system call !\n\t");
/* Whenever the timer expires and SIGALRM signal is received , there is an error in the select system call */
continue;
/* since u are getting an error in select, it is entering gui portion and waiting on recvfrom , hence the "continue" workaround*/
}
if(FD_ISSET(guiSockFd, &rset))
{
printf("Data received from GUI!\n\t");
cli_len = sizeof(cli_addr);
if(recvfrom(guiSockFd, &mesg, sizeof(mesg), 0, &cli_addr, &cli_len) < 0)
{
printf("Error in receive!\n\t");
}
printf("Servicing a GUI request!\n\t");
printf("Message received = %s\n\t",mesg);
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(MRS_TM_PORT);
if(sendto(mrsSockFd, &mesg, sizeof(mesg), 0, &cli_addr, sizeof(cli_addr)) < 0)
{
printf("Error in send to GUI\n");
}
printf("Message sent to MRS!\n\t");
}
if(FD_ISSET(mrsSockFd, &rset))
{
printf("Data received from MRS!\n\t");
cli_len = sizeof(cli_addr);
if(recvfrom(mrsSockFd, &mesg, sizeof(mesg), 0, &cli_addr, &cli_len) < 0)
{
printf("Error in receive!\n\t");
}
printf("Servicing a MRS request!\n\t");
printf("Message received = %s\n\t",mesg);
}
if(FD_ISSET(mpSockFd, &rset))
{
printf("Data received from MP!\n\t");
cli_len = sizeof(cli_addr);
if(recvfrom(mpSockFd, &mesg, sizeof(mesg), 0, &cli_addr, &cli_len) < 0)
{
printf("Error in receive!\n\t");
}
printf("Servicing a MP request!\n\t");
printf("Message received = %s\n\t",mesg);
}
}
return 0;
}
reply other threads:[~2001-05-11 9:08 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=65256A49.0030CA26.00@sandesh.hss.hns.com \
--to=npunmia@hss.hns.com \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®