• C


Hi there i'm working with a mail client in linux.
My problem is : when i use the func read to read from the socket it prompts until data arrives so if
no data arrives it just prompts forever. I wonder if there is a possibility to abort it after sometime or with another function??
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

To deal with socket, you'd rather use send() and recv() instead of write() and read(), to gain greater control over data transmission. For a guide for start, take a look at http://www.ecst.csuchico.edu/~beej/guide/net/
Use the select system call. It allows you to specify for what time you want to monitor a number of file descriptors. It is very good for monitoring a no. of fds, including sockets, at the same time.
       Specify your socket fd as the one to monitor for reading in select() alongwith the timeout. When the select call returns, check its reason for return i.e. data has arrived or timeout occurred. U can then read data from socket accordingly.
marcooAuthor Commented:
Ehh.. i don't quite understand do you think you can give me an example??
I'd appreciate it Marcoo..
Here's the example :
Suppose ur socket fd is sockfd.

fd_set rfds;
struct timeval tv;
int retval;

FD_SET(sockfd,&rfds);  /*Set sockfd for read monitoring*/

/*Set wait time to say 5 sec*/
tv.tv_sec = 5; /*Seconds*/
tv.tv_usec = 0; /*Milliseconds*/

retval = select(1,&rfds,NULL,NULL,&tv);

/*At this point the select call will block waiting for either some data to appear in sockfd or timeout to expire*/

 /*Check if select call has returned due to data in sockfd*/
 {/*Process sockfd data*/}
else {/*No data within 5 seconds*/}

I hope its clear now.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
marcooAuthor Commented:
OK thanx i'll will try it out..
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.