LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 08-20-2006, 06:30 PM   #1
DavidHB
Member
 
Registered: Feb 2006
Location: Chile
Distribution: Slackware 10.2
Posts: 53

Rep: Reputation: 15
bad file descriptor on PEEK of a connected socket


I'm connected to a socket which is accepted by the other side in a romote machine. The socket number is (3). I have another thread scanning the socket to see when it connects ( I tried select, and also recv with the PEEK flag ). They both give a "bad file descriptor" error.

Is there some problem that I'm scanning the socket in another thread ( child of the socket creator ).

I've tried both blocking and non-blocking. In blocking mode, the connect falls through right away. So why is the socket bad?

Dave
 
Old 08-21-2006, 03:57 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,536

Rep: Reputation: 148Reputation: 148
You should be very careful when sharing descriptors between threads, but it shouldn't result with bad descriptor. How do you know that you have a socket? Bad descriptor happens when you use any (not assigned to that process) descriptor number. Are you sure it's 3? And, are you sure it's connected? Remember that you have a new socket after accept().
 
Old 08-22-2006, 03:28 PM   #3
DavidHB
Member
 
Registered: Feb 2006
Location: Chile
Distribution: Slackware 10.2
Posts: 53

Original Poster
Rep: Reputation: 15
Mara,

Thanks for the response. I got it working, by keeping everything in the same thread.

The socket number '3' was returned as a socket in the call to socket(). I am the client, so the other side accepted the connection, also the connect() call passed through successfully ( in its regular BLOCKING mode ), so I knew it was accepted and connected.

But it just seems that the child thread didn't see the socket as valid in it's space. I suppose then, if I opened a new socket in the child, it could give me a "3" as well, and be a distinct socket? Or more likely would just give me another number that's not used anywhere?

I knew that variables had limited visibility between threads. I found out in this program as well, that memory allocated in the parent ( with calloc in this case ) also was not valid in the child. The pointer returned from calloc was stored in a global variable, but even so, the momory was not valid in the child.

So for anyone else that's intrested, that's the info.

Thanks again.
 
Old 08-23-2006, 04:57 PM   #4
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,536

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by DavidHB
But it just seems that the child thread didn't see the socket as valid in it's space. I suppose then, if I opened a new socket in the child, it could give me a "3" as well, and be a distinct socket? Or more likely would just give me another number that's not used anywhere?
There's no connection between descriptor numbers in different processes. Every process has its own descriptors and new numbers are given in order of creating files/sockets etc.

In fact, in your case, if you have SOCK_STREAM, the servers has socket no 3 (listening) and another socket (let's say it's 4) which connects with the client.

Quote:
I knew that variables had limited visibility between threads. I found out in this program as well, that memory allocated in the parent ( with calloc in this case ) also was not valid in the child. The pointer returned from calloc was stored in a global variable, but even so, the momory was not valid in the child.
Yes, it's the same mechanism. Variables of different processes are not shared. Every process has its own set. They can be shared only if you use IPC (inter-process communication) mechanisms.
 
  


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
Bad File Descriptor tech_user Linux - Hardware 0 10-14-2005 03:21 PM
Wireless : cardmgr[4292] : Open sock(socket 1) failed: Bad file descriptor mahendra Fedora 1 12-07-2004 04:36 PM
apt-file returns nothing; 'bad file descriptor' overbored Debian 3 10-03-2004 09:13 PM
Evolution: bad file descriptor bfibandit Mandriva 2 04-11-2004 03:00 PM
SIOCSIFADDR: Bad file descriptor egon Linux - Networking 0 02-13-2001 08:41 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration