non blocking recv

This is a discussion on non blocking recv within the Linux Programming forums, part of the Platform Specific Boards category; hey I was practising with the QT programs, in one of my programs i created a thread and in the ...

  1. #1
    Registered User rohit's Avatar
    Join Date
    Feb 2002
    Posts
    69

    non blocking recv

    hey

    I was practising with the QT programs, in one of my programs i created a thread and in the thread's execute statement i set the asynchronous socket mode but when i am recv it simply is blocking and is not freed unless i recv 2 connections.

    should i use select call,though i do not need the client sockets or is there any other way to do the non blocking recv

    any pointers please ...

    #include "thread.h"

    int sigflag=0;
    void sigio_func(int i)
    {
    sigflag=1;
    }

    void mythread::run()
    {
    signal(SIGIO,sigio_func);
    fcntl(0,F_SETOWN,getpid());
    fcntl(0,F_SETFL,FASYNC);

    int i=0;
    //while(i<2)
    {

    int sock,bytes_rec,fromlen;
    char buffer[65535];
    struct sockaddr_in from;
    struct ip* ipheader;
    struct tcphdr *tcpheader;
    char * buf;
    sock = socket(AF_INET,SOCK_RAW,IPPROTO_TCP );
    while(i<2)
    {


    sigblock(sigmask(SIGIO));
    while(sigflag=0)
    sigpause(0);

    i++;
    fromlen=sizeof(from);
    bytes_rec = recv(sock,buffer,sizeof(buffer),0);
    ipheader=(struct ip*)buffer;
    tcpheader = ( struct tcphdr *)(buffer + (ipheader->ip_hl));

    QString label1=inet_ntoa(ipheader->ip_src);

    QListViewItem * item = new QListViewItem(tview,label1);
    tview->insertItem(item);
    sigflag=0;
    sigsetmask(0);
    }


    }

    }

    void mythread::setList(QListView *view)
    {

    tview=view;

    }

  2. #2
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Hi Rohit,

    If you would like recv() to return whether or not there is data to be read from the socket, use ioctl() to make your socket non-blocking:
    Code:
    int Status, Blocking = 1;
    Status = ioctl(Socket, FIOSNBIO, &Blocking);
    if (Status == -1)
      perror( "ioctl()" );
    However, select() and poll() are also options if you want to wait up to a certain amount of time for something to come in on a socket, which is useful for 'connection timeouts'.
    Jason Deckard

  3. #3
    Registered User rohit's Avatar
    Join Date
    Feb 2002
    Posts
    69
    FIONSBIO -> are you sure there is 'S' .

    what i want is that the thread is intimated only when there is a data to be received, it must not keep on waiting as it is in a loop ... top shows it is taking 98% of CPU ,because of the same reason i did the async socket I/O code as shown below but it doesn not seem to work . i do not want my prog to hog down the cpu the kernel should tell the functin that it's tiem to get up.


    any pointers ...

    cheers
    Rohit

  4. #4
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Originally posted by rohit
    FIONSBIO -> are you sure there is 'S' .
    My mistake, I had HP/UX on the brain. The correct request argument is FIONBIO for Linux.

    I understand you don't want to block waiting on data to come in on a socket because you have other checks to make; however, you don't want to consume gobs of CPU time looping through these checks. What are the nature of the other checks? If they are IO, poll() would be perfect. Give me a hint and may be able to point you down the path to coding salvation.
    Jason Deckard

  5. #5
    Registered User rohit's Avatar
    Join Date
    Feb 2002
    Posts
    69
    HI,
    well my QT program does not seems to update and refresh the list view where I am adding the IP nos.

    It does only after it gets all the 20 IP(s) in list though <B>all this is done in a seperate thread</B>.

    (while(i<20))
    {
    socket calls
    listviewaddIPfunctions
    }

    it's not blocking as it is recursing and returning -1 from recv for those sockets which has no data.
    but it's not letting hold the list view to update. i will try more for now.

    thanx for your time
    cheers
    Rohit

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about recv
    By carrotcake1029 in forum Networking/Device Communication
    Replies: 2
    Last Post: 02-26-2009, 01:10 PM
  2. Need blocking for function
    By carrotcake1029 in forum Linux Programming
    Replies: 3
    Last Post: 01-10-2009, 04:50 PM
  3. How to initialize a non blocking socket using only winsock library
    By *DEAD* in forum Networking/Device Communication
    Replies: 4
    Last Post: 01-18-2008, 06:03 AM
  4. blocking recv
    By rohit in forum C Programming
    Replies: 0
    Last Post: 03-06-2002, 01:47 AM
  5. recv function (blocking)
    By aurumice in forum Linux Programming
    Replies: 0
    Last Post: 03-01-2002, 12:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21