473,387 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

having trouble with select()

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
2 1917
gpraghuram
1,275 Expert 1GB
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
Thanks Raghu... I got it...:)
Mar 24 '09 #3

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

Similar topics

1
by: Anand | last post by:
Hi i am having trouble adding a recordset into the access database, the code seems to be working fine it passs and parses through all variables just fine without showing any errors and also when i...
9
by: TD | last post by:
I am trying to add transactions to my code. The original code worked fine until I followed an example to setup transactions, now the code does strange things, but no error messages. Could...
3
by: RSH | last post by:
I am slowly getting the hang of objects and creating my own. I was given some help in assigning an object to a ComboBox collection. I have been looking at it and I get the concept which is very...
1
by: MLH | last post by:
Am having trouble with the filter property setting below. Would like to filter the listing to car makes beginning with "D". I'm blowing it on the filter spec somehow??? Sub OpenRecordsetX() ...
0
by: harry12 | last post by:
Hello- I'm fairly new at using Microsoft Access and I'm having trouble getting a couple of things to work in my database. The first is that I have yet to find a way to get an append query to...
2
by: Damas | last post by:
Hello, First of all please excuse my vocabulary, english isn't my mother tong. I'm having a hard time trying to do this. I have two tables in a MySQL db, first one is for members datas, second one...
3
by: Jay Ruyle | last post by:
I'm trying to figure out a way to list several items in a listbox and let the user select any number of items in the listbox. I have tried to code in the items as bitwise items but all it stores...
1
keyvanrahmadi
by: keyvanrahmadi | last post by:
I have created a form which is as Follow: <form action="fleet-of-car.php" method="post"> <br />SELECT A FORM TO SEE : <select name="FORM"> <option value="ALL CARS"/>All Cars <Option...
6
by: ashraf02 | last post by:
basically i am trying to create an online storefront. i have three seperate tables where which i want to show on the page. three table are linked to one table called Smart. when i execute the code...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.