UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2008-07-12 10:36 AM

david
Member
Registered: 2008-07-12
Posts: 9

Re: recv(): bad file description

hello!
i wrote a tcp client,which send a package to server and recv response each time in a while circle.when error ocurs ,the client reconnects.
the problem is the recv return a negtive every 3 or 4 minutes.
perror() gives bad file descriptor.

can anyone  give me  clues why this happens?

Offline

#2 2008-07-12 12:21 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: recv(): bad file description

Did the recv() return 0 prior to the -1? If so it means the peer closed the connection
(either willingly or because it crashed or something.)

If not you could have a buffer overflow scribbling over the memory where the filedescriptor is stored,
to check for that either add printfs or run it through Valgrind. Or just re-read the code well.

Offline

#3 2008-07-14 02:29 AM

david
Member
Registered: 2008-07-12
Posts: 9

Re: recv(): bad file description

As i cap the packages by tcpdump,i found that the client sent rst to the server first  which eventually closed the connection.

the attachment is the cap file.

I think it's probablely the memory overflow.
I had been looking for memcheck tools like valgrind.
Thanks!

i3839;24808 wrote:

Did the recv() return 0 prior to the -1? If so it means the peer closed the connection
(either willingly or because it crashed or something.)

If not you could have a buffer overflow scribbling over the memory where the filedescriptor is stored,
to check for that either add printfs or run it through Valgrind. Or just re-read the code well.

Offline

#4 2008-07-14 09:03 AM

david
Member
Registered: 2008-07-12
Posts: 9

Re: recv(): bad file description

i expected valgrand to find out where the bug was,but after running for 7 hours ,it just gave some irrelevant messages. When situation described before recured,nothing was printed by valgrand.
Is valgrand reliable ?

Offline

#5 2008-07-14 12:41 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: recv(): bad file description

Valgrind is pretty reliable, there are only a few situations it doesn't catch (e.g.
buffer overflows on zero intialized global data, and scribbling in adjacent data
will be hard to detect too). It's likely something else than a memory bug.

Looking at the dump the resets happen one second after the client sent data,
so it seems like a sort of time-out bug in your code. Wild guess: You're using
select() with a one second timeout and you don't handle select() returning zero
correctly.

Can't really say anything useful without seeing the code though.

Offline

#6 2008-07-14 12:50 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,826
Website

Re: recv(): bad file description

Yes, valgrind is VERY reliable...  I use it quite often, and find it one of those tools
that are so incredibly useful and marvelous that I can't imagine life without them
anymore... ;-)

I'm not sure why you think it's some kind of memory corruption...  You say a RST is
sent on behalf of your client to the server?  That likely means you are using
SO_LINGER and closing the socket while there's untransmitted data in the queue...
However, that's really not going to have anything to do with your EBADF error...
That is surely just some bug in your code whereby you're trying to continue using
the socket FD after you've already closed it...  Can you post the code?

Offline

#7 2008-07-15 03:21 AM

david
Member
Registered: 2008-07-12
Posts: 9

Re: recv(): bad file description

<code>
int RecvableTimeout( int fd , int sec )
{
        fd_set rset;
        struct timeval tv;
        time_t begin_t;
        int retval;

        if( sec == 0 )
        {
                fprintf(stderr,"isset = %d ???\n",FD_ISSET(fd,&rset));
                return( select(fd+1,&rset,NULL,NULL,NULL) );    /* wait for ever */
        }
        if( sec < 0 )
        {
                return -1;
        }

        while( 1 )
        {
                begin_t = time(NULL);
                FD_ZERO( &rset );
                FD_SET( fd , &rset );
                tv.tv_sec  = sec;
                tv.tv_usec = 0;
                if( (retval=select(fd+1,&rset,NULL,NULL,&tv)) < 0 )
                {
                        if( errno==EINTR )
                        {

                                sec = sec - ( time(NULL) - begin_t);
                                continue;
                        }
                        else
                        {
                                break;
                        }
                }
                else
                {
                        break;
                }
        }
        return retval;
}

int IMRecv( int fd , struct im_packet_t * readbuf,int sec )
{
        int retval,retval2;
        unsigned short nread=0;
        int timeout;

        timeout = sec;

        if (readbuf == NULL)
        {
                return -1;
        }
        /*
         * Waiting for read buffer ready
       
        if ( timeout < 0 ) timeout = 0 ;
        if( timeout > 0 )
        {
                if( (retval= RecvableTimeout(fd,timeout))<0 )
                {
                        close(fd);
                        fprintf(stderr,"IMRecv: RecvableTimeout for sth wrong...\n");
                        return -3;
                }
                else if( retval==0 )
                {
                        close(fd);
                        fprintf(stderr,"IMRecv: RecvableTimeout for time out...\n");
                        errno = 0;
                        return -2;
                }
                fprintf(stderr,"ww::fd = %d, retval = %d\n timeout = %d,  %s\n",fd,retval,timeout,strerror(errno));
               
        }
    */
        /*
         * Read head of the packet
         */
        memset( readbuf,0,sizeof(struct im_head_t) );
RECV:
        retval = recvn( fd,(char *)readbuf,sizeof(struct im_head_t),0 );
        if( retval<0 )
        {
                /* Add for nonblocking socket */
                if( errno == EWOULDBLOCK )
                {
                        return(0);
                }
                if(errno   ==   EINTR)
                        goto RECV;
                perror("IMRecv:recvn");
                fprintf(stderr,"recvn ww::fd = %d, retval = %d\n timeout = %d,  %s\n",fd,retval,timeout,strerror(errno));
                if( errno == ETIMEDOUT )
                {
                        /*
                         * on keepalive mode and no timed-out select,
                         * 2 hours later,recv timed out
                         */
                        close(fd);
                        return(-2);
                }
                else
                {
                        close(fd);
                        return(-3);
                }
        }
        else if ( retval == 0 ) /* disconnected */
        {
                fprintf(stderr,"IMRecv: client close socket by recvn...\n");
                close(fd);
                return(-1);
        }
        else if ( retval < sizeof(struct im_head_t) )  /* some exception occurs */
        {
                fprintf(stderr," Warning: Receving len < im_head_t...\n");
                close(fd);
                return(-4);
        }
        // nread = Ntohs(socket_corba, readbuf->iBufferLength );
        nread = ntohs(readbuf->head.length);

        if( nread == 0 )
        {
                return retval;
        }

        if( nread > MAX_IM_PACKET_BUF )
        {
                fprintf(stderr," Warning: Buffer length=%d| > MAX_IM_PACKET_BUF...\n",nread);
                nread = RADIUS_BUF_SIZE;
                close(fd);
                return (-5);
        }

        /*
         * Read the rest of the packet on length which specicated by nsend
         */
        retval2 = recvn( fd,(char *)readbuf+sizeof(struct im_head_t),nread,0 );
        if( retval2<0 )
        {
                /* Add it for nonblocking socket */
                if( errno == EWOULDBLOCK )
                {
                        return (-1);
                }
                perror("IMRecv:recvn2");
                close(fd);
                return(-3);
        }
        else if( retval2 == 0 )  /* disconnected */
        {
                fprintf(stderr,"IMRecv: client closed socket by recvn2...\n");
                close(fd);
                return (-1);
        }
        else if( retval2 < nread )
        {
                fprintf(stderr,"Warning: recvn2 len=%d| real_len=%d|...\n",retval2,nread);
                close(fd);
                return(-4);
        }
        return(retval+retval2); /* receving total length = (retval+retval2) */
}



</code>

Offline

#8 2008-07-15 03:22 AM

david
Member
Registered: 2008-07-12
Posts: 9

Re: recv(): bad file description

int RecvableTimeout( int fd , int sec )
{
        fd_set rset;
        struct timeval tv;
        time_t begin_t;
        int retval;

        if( sec == 0 )
        {
                fprintf(stderr,"isset = %d ???\n",FD_ISSET(fd,&rset));
                return( select(fd+1,&rset,NULL,NULL,NULL) );    /* wait for ever */
        }
        if( sec < 0 )
        {
                return -1;
        }

        while( 1 )
        {
                begin_t = time(NULL);
                FD_ZERO( &rset );
                FD_SET( fd , &rset );
                tv.tv_sec  = sec;
                tv.tv_usec = 0;
                if( (retval=select(fd+1,&rset,NULL,NULL,&tv)) < 0 ) 
                {
                        if( errno==EINTR )
                        {

                                sec = sec - ( time(NULL) - begin_t);
                                continue;
                        }
                        else
                        {
                                break;
                        }
                }
                else
                {
                        break;
                }
        }
        return retval;
}

int IMRecv( int fd , struct im_packet_t * readbuf,int sec )
{
        int retval,retval2;
        unsigned short nread=0;
        int timeout;

        timeout = sec;

        if (readbuf == NULL)
        {
                return -1;
        }
        /*
         * Waiting for read buffer ready
        
        if ( timeout < 0 ) timeout = 0 ;
        if( timeout > 0 )
        {
                if( (retval= RecvableTimeout(fd,timeout))<0 )
                {
                        close(fd);
                        fprintf(stderr,"IMRecv: RecvableTimeout for sth wrong...\n");
                        return -3;
                }
                else if( retval==0 )
                {
                        close(fd);
                        fprintf(stderr,"IMRecv: RecvableTimeout for time out...\n");
                        errno = 0;
                        return -2;
                }
                fprintf(stderr,"ww::fd = %d, retval = %d\n timeout = %d,  %s\n",fd,retval,timeout,strerror(errno));
                
        }
    */
        /*
         * Read head of the packet
         */
        memset( readbuf,0,sizeof(struct im_head_t) );
RECV:
        retval = recvn( fd,(char *)readbuf,sizeof(struct im_head_t),0 );
        if( retval<0 )
        {
                /* Add for nonblocking socket */
                if( errno == EWOULDBLOCK )
                {
                        return(0);
                }
                if(errno   ==   EINTR)
                        goto RECV;
                perror("IMRecv:recvn");
                fprintf(stderr,"recvn ww::fd = %d, retval = %d\n timeout = %d,  %s\n",fd,retval,timeout,strerror(errno));
                if( errno == ETIMEDOUT )
                {
                        /* 
                         * on keepalive mode and no timed-out select,
                         * 2 hours later,recv timed out 
                         */
                        close(fd);
                        return(-2);
                }
                else
                {
                        close(fd);
                        return(-3);
                }
        }
        else if ( retval == 0 ) /* disconnected */
        {
                fprintf(stderr,"IMRecv: client close socket by recvn...\n");
                close(fd);
                return(-1);
        }
        else if ( retval < sizeof(struct im_head_t) )  /* some exception occurs */
        {
                fprintf(stderr," Warning: Receving len < im_head_t...\n");
                close(fd);
                return(-4);
        }
        // nread = Ntohs(socket_corba, readbuf->iBufferLength );
        nread = ntohs(readbuf->head.length);

        if( nread == 0 )
        {
                return retval;
        }

        if( nread > MAX_IM_PACKET_BUF )
        {
                fprintf(stderr," Warning: Buffer length=%d| > MAX_IM_PACKET_BUF...\n",nread);
                nread = RADIUS_BUF_SIZE;
                close(fd);
                return (-5);
        }

        /*
         * Read the rest of the packet on length which specicated by nsend
         */
        retval2 = recvn( fd,(char *)readbuf+sizeof(struct im_head_t),nread,0 );
        if( retval2<0 )
        {
                /* Add it for nonblocking socket */
                if( errno == EWOULDBLOCK )
                {
                        return (-1);
                }
                perror("IMRecv:recvn2");
                close(fd);
                return(-3);
        }
        else if( retval2 == 0 )  /* disconnected */
        {
                fprintf(stderr,"IMRecv: client closed socket by recvn2...\n");
                close(fd);
                return (-1);
        }
        else if( retval2 < nread )
        {
                fprintf(stderr,"Warning: recvn2 len=%d| real_len=%d|...\n",retval2,nread);
                close(fd);
                return(-4);
        }
        return(retval+retval2); /* receving total length = (retval+retval2) */
}

Offline

#9 2008-07-15 03:38 AM

david
Member
Registered: 2008-07-12
Posts: 9

Re: recv(): bad file description

int CheckFd(int fd)
{
    fd_set rset,wset;
    struct timeval tv;
    int rc;

        if (fd <0)
        {
                return -1;
        }
    FD_ZERO( &rset );
    FD_ZERO( &wset );
    FD_SET( fd , &rset );
    FD_SET( fd , &wset );
    tv.tv_sec = 0;
    tv.tv_usec = 0;
    rc = select(fd+1,&rset,NULL,NULL,&tv);
    if(rc < 0)
    {
        return -1;
    }
    rc = select(fd+1,NULL,&wset,NULL,&tv);
    if(rc < 0)
    {
        return -1;
    }
    return 0;

}


CheckFd is the one and only function calling select.
it's used to check if the fd works well.

Offline

#10 2008-07-15 10:25 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: recv(): bad file description

But you don't do anything there... What's the purpose of that code?
You can as well just do whatever you want to do and block there instead.

That code doesn't handle select() returning zero, which it will return
when there's no read data.

You can also check both sets with one select() call.

You aren't calling CheckFd() in a loop with a one second sleep or something
like that, are you?

Offline

#11 2008-07-15 02:31 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,826
Website

Re: recv(): bad file description

Also, I thought you initially said the error was coming from recv() failing, NOT from
select()?  Which is it really??

And, just that bit of code on its own isn't much help, since we have no clue how you
are calling this function and what you're passing it for an "fd" value, which is kind of
important...  If you pass it a bogus FD, then yeah it's going to give you EBADF...

Not to mention the fact that the entire function seems rather useless, as i3839 said...
If you think it's verifying that the socket is both readable and writable, you're wrong,
because you never verify that the return values are >0 or do FD_ISSET() after...  If
its only purpose is to catch bogus FDs, then why 2 separate select() calls?  (In fact,
why 2 separate ones at all, no matter what the case, since a single call would also
suffice to test both readability and writability, as i3839 also pointed out?)

Offline

#12 2008-07-16 02:35 AM

david
Member
Registered: 2008-07-12
Posts: 9

Re: recv(): bad file description

CheckFd is called in the begining of a loop to check whether it needs to reconnect or not.

if((my_sock->socket <=0)||(my_sock->socket>0&&CheckFd(my_sock->socket)!=0)){
  //reconnect   
}


when i run the client in gdb ,everything is all right.
what's the difference between them,gdb client and ./client?

Offline

#13 2008-07-16 04:02 AM

david
Member
Registered: 2008-07-12
Posts: 9

Re: recv(): bad file description

==26159== 822,378,448 (9,120,236 direct, 813,258,212 indirect) bytes in 99,133 blocks are definitely lost in loss record 19 of 22
==26159==    at 0x401A951: malloc (vg_replace_malloc.c:207)
==26159==    by 0x807A28D: my_malloc (my_malloc.c:34)
==26159==    by 0x8093E15: mysql_store_result (client.c:2799)
==26159==    by 0x807134A: Query::query(char*) (Query.cpp:41)
==26159==    by 0x806F1D3: deal_logoff_session(user_info_t*, Connection*&) (pc_mysql.cpp:1091)
==26159==    by 0x8057EDF: conn_radius_by_radius(queue_link_t*, Connection*&) (pc_deal.cpp:567)
==26159==    by 0x80571A5: respond_radius (pc_deal.cpp:110)
==26159==    by 0x4129DD7: start_thread (in /lib/tls/libpthread-0.60.so)
==26159==    by 0x4211D19: clone (in /lib/tls/libc-2.3.2.so)
==26159==
==26159==
==26159== 800,847,212 bytes in 100,277 blocks are possibly lost in loss record 22 of 22
==26159==    at 0x401A951: malloc (vg_replace_malloc.c:207)
==26159==    by 0x807A28D: my_malloc (my_malloc.c:34)
==26159==    by 0x809181F: cli_read_rows (client.c:1318)
==26159==    by 0x8093E52: mysql_store_result (client.c:2810)
==26159==    by 0x807134A: Query::query(char*) (Query.cpp:41)
==26159==    by 0x806F1D3: deal_logoff_session(user_info_t*, Connection*&) (pc_mysql.cpp:1091)
==26159==    by 0x8057EDF: conn_radius_by_radius(queue_link_t*, Connection*&) (pc_deal.cpp:567)
==26159==    by 0x80571A5: respond_radius (pc_deal.cpp:110)
==26159==    by 0x4129DD7: start_thread (in /lib/tls/libpthread-0.60.so)
==26159==    by 0x4211D19: clone (in /lib/tls/libc-2.3.2.so)
==26159==
==26159== LEAK SUMMARY:
==26159==    definitely lost: 9,120,604 bytes in 99,137 blocks.
==26159==    indirectly lost: 813,258,212 bytes in 196,985 blocks.
==26159==      possibly lost: 800,850,556 bytes in 100,287 blocks.
==26159==    still reachable: 145,624,759 bytes in 57,446 blocks.
==26159==         suppressed: 0 bytes in 0 blocks.
==26159== Reachable blocks (those to which a pointer was found) are not shown.
==26159== To see them, rerun with: --leak-check=full --show-reachable=yes


When i ran another program with valgrind,i got this..!!
definitely lost: 9,120,604 bytes in 99,137 blocks.
it's really terrible.

Offline

#14 2008-07-16 01:35 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,826
Website

Re: recv(): bad file description

You're still not giving us enough code context to be able to tell if you might be doing
something to that passed-in FD value that renders it bogus...  Ie: closing it, and then
still passing it to your function...

Also, as a side note: you do realize that zero is a perfectly valid FD value, don't you?
Because, your posted test makes it seem like you consider it invalid...  (Of course, as
long as you don't close your stdin, you won't see any socket with an FD value of 0,
anyway...  Well, unless someone exec*()'s you with an already closed stdin...)

Also, you never answered my question: is the EBADF failure coming from recv() or
one of your posted select() calls?

when i run the client in gdb ,everything is all right.
what's the difference between them,gdb client and ./client?

Hmmm, well in that case, you might indeed have some kind of memory-trashing
going on somewhere...  Heisenbugs like that are usually signs of memory being
trashed somehow...  But, if so, I would have expected valgrind to have found it...
Perhaps it's subtle enough not to trip it...  If it's a stack overflow, I think it'll only see
it if you overflow enough to stomp on your frame-pointer/return-address/etc...  If you
just overflow enough to trash other nearby stack variables, then I don't think it has
any way of telling that...  So, that may be what's happening to you...

But, without full source (or at least a lot more that what you've shown us so far), we
can't really know what the real problem is...  If you don't want to post it for some
reason, then I'd recommend adding a whole bunch of debugging printf()'s to try to
track it down...  Print out the value of the FD at various points in processing, and see
if it changes value suddenly at some point, and see if the value looks obviously bogus
at the point that the EBADF error occurs...

Offline

#15 2008-07-16 01:59 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: recv(): bad file description

It seems Valgrind can't detect it when scribbling adjacent memory:

int main(void){
	int a = 1;
	int b = 2;
	int* c = &b;

	printf("a=%i, b=%i, c=%p\n", a, b, c);
	return c[1];
}


Isn't detected.

Interestingly that code returns 1 as expected when compiled without
optimisations, but random stuff otherwise.

Offline

#16 2008-07-16 08:12 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,826
Website

Re: recv(): bad file description

Yeah, I didn't figure it could possibly detect that, since you're still reading/writing valid
process memory...  With malloc()'d memory, it could simply replace malloc() and stick
some unmapped/protected memory after/before each chunk, but there's not much it
can realistically do to separate stack variables, I doubt...

But, if you overflow enough to trash your return address on the stack (try overwriting
"c[2]" in your example), you'll get a logged error and seg-fault on the return from
main(), due to the now bogus return address...  It still doesn't log the write that
caused the problem, merely the attempted read of the invalid return address when
attempting to jump there to continue execution...

Offline

#17 2008-07-16 08:18 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,826
Website

Re: recv(): bad file description

Oh, and as for the optimization difference, that's likely because the optimizer sticks
"a" into a register in that case (since its address is never needed, or anything else
that forces it to be a true stack variable), so "b" moves to the top of the stack frame,
and what you're seeing returned as "c[1]" is your return address (or part of it, if on a
64-bit platform), but truncated to an 8-bit standard exit value...

Offline

#18 2008-07-17 04:52 AM

david
Member
Registered: 2008-07-12
Posts: 9

Re: recv(): bad file description

hi&#65292;robsease&#65292;the EBADF failure comes from recv();
Finally , I found the reason. It's really beacause of timeout ,as i3839 mentioned.

RECV:
        retval = recvn( fd,(char *)readbuf,sizeof(struct im_head_t),0 );
        if( retval<0 )
        {
                /* Add for nonblocking socket */
                if( errno == EWOULDBLOCK )
                {
                        return(0);
                }
                if(errno   ==   EINTR)
                        goto RECV;
                perror("IMRecv:recvn");
                if( errno == ETIMEDOUT )
                {
                        /* 
                         * on keepalive mode and no timed-out select,
                         * 2 hours later,recv timed out 
                         */
                        close(fd);
                        return(-2);
                }
                else
                {
                        close(fd);
                        return(-3);
                }
        }
        else if ( retval == 0 ) /* disconnected */
        {
                fprintf(stderr,"IMRecv: client close socket by recvn...\n");
                close(fd);
                return(-1);
        }


recvn will return -1 on timeout.BUT I didn't close(fd) in recvn .
So I guess maybe errno was set by another call.

Offline

#19 2008-07-17 11:55 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,230

Re: recv(): bad file description

FYI, in general it's not save to check errno after calling any stdio functions,
including perror().

I meant a timeout in your select(), not in TCP. Getting ETIMEDOUT on recv()
just seems weird, would expect that to get when sending data.

But closing the fd isn't enough, the function calling the function containing this
code apparently didn't handle return values well enough. Calling recv() with
a closed fd or an invalid one won't work in either case, so you need to clean
up when it goes bad.

Offline

#20 2013-12-13 10:38 PM

FractalSpace
Guest

Re: recv(): bad file description

Pretty old thread, but this solved my problem: " ... Or just re-read the code well..." from Oddminstrator :)
Basically, I was launching a thread from a function call. The function allocated memory, and thread used is via recv(). But when the function exits, thread stays but object goes away. I am stupid.

#21 2014-01-20 09:58 AM

Bhawna Popli
Guest

Re: recv(): bad file description

//TCP Client Code in C++ on linux platform

#include<iostream>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<errno.h>

SockFd = socket(AF_INET,SOCK_STREAM,0);
if(setSockOpt() < 0)
{
	std::cout<<"SetSockopt Not Set \n";
}
else
{
	std::cout<<"SetSockopt Set Successfully \n";							    
	Port = 30082;
	memset(Addr,0x00,sizeof(Addr));
	strcpy(Addr,addr);	   
	SockAddr.sin_family = AF_INET;
	SockAddr.sin_port = htons(Port);
	SockAddr.sin_addr.s_addr = inet_addr("192.168.3.38");
	printf("Waiting for Connection\n");   	
}  
std::cout<<"Port:::   "<<Port<<std::endl; 



using namespace std;
char logmsg[150];
    cout<<"EISCONN::: "<<EISCONN<<" ETIMEDOUT "<<ETIMEDOUT<<" ECONNREFUSED"<<ECONNREFUSED<<endl;
  	while(1)
 	{
  		printf("trying to connect to Sever sock<%d>\n",SockFd);
     		//sleep(5);            		
   		if((connect(SockFd,(struct sockaddr*)&SockAddr,sizeof(SockAddr))) > -1)
   		{
    			cout<<"SockFd while connecting:::::::::::::::::  "<<SockFd<<endl;
    			cout<<"errno:::::::::::::::::  "<<errno<<endl;
    			cout<<"Enter in connect if loop"<<endl;
    			if(setSockOpt()< 0)
   				cout<<"\n SetSockopt Not Set \n\n\n";
   				else if(setSockOpt() >= 0) 
   				{   					
   					cout<<"\n SetSockopt Set \n\n\n";
   					break;
   				}
    		}
   		else if( errno == EISCONN )
		{
			close(SockFd);
			shutdown(SockFd,2);
			SockFd=-1;
			createSocket();
		}
   		else
    		{
        		if (errno == ETIMEDOUT)
        		{
       				//printf("SYSTEM ERR MSG : %s\n",strerror(errno));
       				sleep(10);
       				//continue;
          		}
				else if (errno == ECONNREFUSED)
				{
					close(SockFd);
					shutdown(SockFd,2);
					SockFd = -1;
	     			sleep(5);
					createSocket();
				}
        		else
         		{
					sleep(5);
					close(SockFd);
					createSocket();
	          			//printf("SYSTEM ERR MSG : %s\n",strerror(errno));
					//return(-1);
           		}
     		}
 	} 
int length = -1;  
char msg[1500];  
length = recv(SockFd,msg,1500,0);      




int setSockOpt()
{
	struct linger ling;
	int flag;
	ling.l_onoff = 1;
	ling.l_linger = 0;
	flag = 1;
	if(setsockopt(SockFd,SOL_SOCKET,SO_LINGER,(char *)&ling,sizeof(ling))<0)
	{
		return -1;
	}
	else
	{
		return 0;
	}     
}

int createSocket()
{
	std::cout<<"In createSocket()"<<std::endl;
	std::cout<<"Port:::    "<<Port<<"  Addr    "<<Addr<<std::endl;
	SockFd=-1;
	SockFd = socket(AF_INET,SOCK_STREAM,0);
	/* if(setSockOpt()< 0)
	   // cout<<"\n SetSockopt Not Set \n\n\n";
	   else
	   {}      //cout<<"\n SetSockopt Set Successfully \n\n\n";*/
	SockAddr.sin_family = AF_INET;
	SockAddr.sin_port = htons(Port);  
	SockAddr.sin_addr.s_addr = inet_addr(Addr);
	//printf("TCPClientSocket Port<%d> Socket<%d> Addr<%s>\n",Port,SockFd,Addr);
	return 0;
}

#22 2014-01-20 04:01 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,826
Website

Re: recv(): bad file description

Um, yes?  And, the point of this code is what?  Did you have an accompanying question or problem to go along with it?

Offline

Board footer

Powered by FluxBB