non blocking recv [Archive] - C Board

PDA

View Full Version : non blocking recv


rohit
03-05-2002, 04:59 AM
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;

}

Deckard
03-05-2002, 06:42 AM
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:

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'.

rohit
03-05-2002, 07:11 AM
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 ... :D

cheers
Rohit

Deckard
03-05-2002, 09:52 AM
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.

rohit
03-05-2002, 09:35 PM
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