LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-12-2007, 09:43 AM   #1
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Rep: Reputation: 32
fgets on socket


I have written a simple server ( which uses select() ) and until now I have tested it with telnet. Now I started to write a simple client, but came across a problem.

Because my client (like the server) needs to respond immediately, I can't risk letting it to block. So, I decided to multiplex STDIN_FILENO and the socket I use in the client with select. It's all just like in the server, just that there I multiplex more sockets. So, I use this code. sock is the socket, rx is the fd_set

PHP Code:
//.....
rx=fdopen(sock,"r");
for(;;)
{
    
select_status=select(sock+1,&rx_set,NULL,NULL,&tv);
    if(
select_status==-1)
    {
        
cout<<"out\n";
        return(
1);
    }
    else
    {
        if(!
select_status)
        {
            
cout<<"timeout\n";
            continue;
        }
    }
/*    if(FD_ISSET(STDIN_FILENO,&rx_set))
    {
        fgets(m_buf,sizeof(m_buf),stdin);
        fputs(m_buf,tx);
        fflush(tx);
    }*/

    
if(FD_ISSET(sock,&rx_set))
    {
        
fgets(buffer,sizeof(buffer),rx);
        if(
feof(rx))
        {
            return(
1);
        }
        
cout<<buffer<<endl;
    } 
I commented out the first FD_ISSET because it was blocking. Now, I have a problem. Sure enough, the server is sending a string as greeting to the client, yet the client prints nothing, AND it blocks on fgets. Please help. Thank you.
 
Old 05-13-2007, 08:36 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
If the server is not sending a line feed at the end of its message, chances are you will block, because fgets() won't return until error or end of file or buffer has been completely filled or a line feed is encountered, whichever occurs first.

But beyond that, just on general principle, you shouldn't mix fgets() with select(), because fgets() works at a higher level (of buffering). If you're going to use select(), then use read() instead.

Another problem, which may be contributing to your symptoms, is that you need to reset the file descriptor set each time through the loop. The safest way is to reset it completely each time, thus:

Code:
FD_ZERO(&rx_set);
FD_SET(sock,&rx_set);
This should appear just before the select() call, each time through the loop.

Also, what's the value of tv? (TV is worthless, but I'm not talking about that here.) If you don't want timeout at all, but instead want to wait forever for something to happen, don't say &tv; say NULL instead.

If you want to return immediately instead of waiting, set the fields of tv to zero.

If you use &tv (whether zero or nonzero), you must set the value of tv each time through the loop, just before select(), just like rx_set.

Before continuing, please please please do this, and read the page carefully:

Code:
man 2 select
Hope this helps.
 
Old 05-14-2007, 07:54 AM   #3
nc3b
Member
 
Registered: Aug 2005
Posts: 330

Original Poster
Rep: Reputation: 32
Smile

Yup, thank you. Before you posted, I have made some changes. I am resetting rx_set at each loop and I am reading with read().

I could have used fread and fgets, by getting the number of characters to read without blocking using ioctl, but I decided that for now, read() is best.

By the way, thanks for explaining, but I have read the man page for select (every time I'm not sure what a function does I read some documentation), it's just that the server wasn't sending the damn newline (it sent some message and then it wouldn't send anything until the client would do so, and because the client didn't do that, the client remained locked.

And tv in my program was fine, it was one second, because for my application it's not ok to block forever. Again, thank you for your post. Cheers.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
fgets never stop reading from socket!? Thinking Programming 1 04-06-2005 09:38 AM
Is fgets functions secure? mullog Programming 2 10-02-2004 01:22 PM
fgets() problems in C AMMullan Programming 4 03-12-2004 04:39 AM
fgets h/w Programming 23 12-21-2003 06:20 PM
fgets vs gets cxel91a Programming 2 12-01-2003 12:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration