By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,083 Members | 1,608 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,083 IT Pros & Developers. It's quick & easy.

having trouble with select()

P: 3
hi,
am doing a multi server chat program using select()..in the client side i need to multiplex thei/p from server and stdin.
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include<fcntl.h>
#include<string.h>
#include<errno.h>
void error(char *msg)
{
perror(msg);
exit(0);
}

int main(int argc, char *argv[])
{
int sockfd, portno, n;
int retval,loop;
char c;
struct sockaddr_in serv_addr;
struct timeval timeout;
fd_set list,list1;
char buffer[256];
if (argc < 3)
{
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
printf("port no = %d\n",portno);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
FD_ZERO(&list);
FD_ZERO(&list1);
FD_SET (0,&list);
FD_SET (sockfd,&list);
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
bzero(buffer,256);
timeout.tv_sec = 3*60;
timeout.tv_usec = 0;
fcntl (0,F_SETFL,O_NONBLOCK);
fcntl (sockfd,F_SETFL,O_NONBLOCK);
printf("%d\n",FD_ISSET(0,&list));
read (sockfd,buffer,100);
printf("%s\n",buffer);
while (1)
{
//open(stdin,O_RDWR);
printf ("waiting on select\n");
retval = select (sockfd+1,&list,NULL,NULL,&timeout);
if (retval < 0)
{
printf ("select failed\n");
exit (1);
}
if (retval == 0)
{
printf ("select timed out\n");
exit (1);
}
printf ("%d\n",FD_ISSET(3,&list));
printf("retval = %d,sockfd = %d\n",retval,sockfd);
for (loop =0;loop<=sockfd;loop++)
{
if (loop != 0)
{
printf ("%d\n",loop);
if (FD_ISSET(loop,&list))
{
printf ("read returns : %d\n",read(loop,buffer,100));
if (read(loop,buffer,100)!=0)
write(1,buffer,strlen(buffer));
//fflush(stdout);
}
}
else
{
if(read(0,buffer,100) < 0)
error("read error");
close(0);
}
}
}
return 0;
}
the error i get is that select waits for i/p for the first time,but from the second it shows tat stdin has i/p,even though it doesn't and read(stdin) returns -1.. the error "resource currently not available"...I need to make the select() wait for i/p from the socket or from the user infinitely... pls help me solve this issue

further i need to know whether i can know the exact descriptor which has i/p as a result of select() or i can know only the no.of descriptors which has i/p which the select returns?
Mar 20 '09 #1
Share this Question
Share on Google+
2 Replies


gpraghuram
Expert 100+
P: 1,275
Everytime befor calling select you have to call these methods
Expand|Select|Wrap|Line Numbers
  1. FD_ZERO(&list);
  2. FD_ZERO(&list1);
  3. FD_SET (0,&list);
  4. FD_SET (sockfd,&list);
But u are calling it only for the first time.
After select returns you have to call the above methods so that select works fine.

NOTE:
PLEASE USE CODE TAGS while posting code

Raghu
Mar 24 '09 #2

P: 3
Thanks Raghu... I got it...:)
Mar 24 '09 #3

Post your reply

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