The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Can we write a multiple thread to receive from a single socket file descriptor pa.chidhambaram IP Networking 2 03-17-2008 06:55 AM
Problems with file descriptor teo High Level Programming 11 05-09-2005 08:47 AM
File Descriptor Help rahulrathod UNIX for Dummies Questions & Answers 3 10-14-2004 02:08 AM
file activity (open/closed) file descriptor info using KORN shell scripting Gary Dunn UNIX for Dummies Questions & Answers 3 06-07-2004 10:54 AM
bad file descriptor? ftb UNIX for Dummies Questions & Answers 1 02-20-2002 04:19 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-11-2008
Registered User
 

Join Date: Oct 2001
Location: Worcester, MA
Posts: 11
Get the file descriptor of a socket file. C vs Python.

Hi,

I want to validate that a file is a socket file on Linux. I know I can do this using the S_ISSOCK macro, but I am not sure how to get the file descriptor for the socket file.

For example, I know that /tmp/mapping-foo is a socket file. In Python I can do something like this:
Code:
>>> import os
>>> import stat
>>> sb = os.stat('/tmp/mapping-foo')
>>> print stat.S_ISSOCK(sb.st_mode)
True
>>>
How ever in C, I am unable to get the file descriptor for the file /tmp/mapping-foo. I tried using open, fopen and fileno as follows:
Code:
        file_des = open("/tmp/mapping-foo", O_RDONLY);
        if( file_des == -1 ) {
                perror("open(2)");
                if((file = fopen("/tmp/mapping-foo", "r")) == NULL ) {
                        perror("fopen()");
                } else {
                        file_des = fileno(file);
                        if (file_des == -1 ) {
                                perror("fileno()");
                        }
                }
                if( file_des == -1 )
                        exit(1);
        }
But, I am getting this error message:
Code:
$ ls -l /tmp/mapping-foo
srwxrwxr-x 1 joe joe 0 Jun  9 09:53 /tmp/mapping-foo
$ file /tmp/mapping-foo
/tmp/mapping-foo: socket
$ ./pc /tmp/mapping-foo
Stat file /tmp/mapping-foo
open(2): No such device or address
fopen(): No such device or address

I am trying to load the correct file.

Thanks,
Joe
Reply With Quote
Forum Sponsor
  #2  
Old 06-11-2008
Registered User
 

Join Date: Oct 2001
Location: Worcester, MA
Posts: 11
I seem to have found a working solution: lstat.
Code:
        if (lstat("/tmp/mapping-foo", &sbuf) <= -1) {
                perror("lstat()");
        }
I am still curious to know how to get the file descriptor of a socket file (/tmp/mapping-foo) though.

Thanks again,
Joe
Reply With Quote
  #3  
Old 06-11-2008
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,663
Quote:
Originally Posted by goon12 View Post
I am still curious to know how to get the file descriptor of a socket file (/tmp/mapping-foo) though.
Try something like:
un.sun_family=AF_UNIX;
strcpy(un.sun_path, "(/tmp/mapping-foo");
fd=socket(AF_UNIX,SOCK_STREAM,0);
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:31 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0