Get the file descriptor of a socket file. C vs Python.


 
Thread Tools Search this Thread
Top Forums Programming Get the file descriptor of a socket file. C vs Python.
# 1  
Old 06-11-2008
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
# 2  
Old 06-11-2008
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
# 3  
Old 06-11-2008
Quote:
Originally Posted by goon12
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);
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

18-Mar-2012 14:25:03.209 general: error: socket: file descriptor exceeds limit (4096/4096)

I have BIND 9.8.1-P1 cache only DNS server running in Solaris 10. I have upgraded the same from 9.6.1 to 9.8.1-P1. Now i am facing "file descriptor exceeds limit (4096/4096)" error frequently on the server. Please help me on this issue! (1 Reply)
Discussion started by: sandeep.tk
1 Replies

2. Shell Programming and Scripting

file descriptor count

I am trying to write a script which will only show me the file descriptor count for a process/pid. My script will return me the count only not the whole output. For example, I would like my script to return the output 23 this case, not the whole output. Can anybody please help me how do I get... (11 Replies)
Discussion started by: mohullah
11 Replies

3. Programming

Copying and overwriting a file using file descriptor

Hi , i have two basic requirement on linux platform . I am using C language to do this . 1) copying one file to another (assuming i know their file descriptors) 2) Overwriting a file using it file descriptor . Please guide. regards Aki (2 Replies)
Discussion started by: meet123321
2 Replies

4. UNIX for Dummies Questions & Answers

Difference between file descriptor and file pointer

hi...., can anyone tell me what is the exact difference between file descriptor and file pointer...... and why file descriptor takes integer value???:confused: (10 Replies)
Discussion started by: jimmyuk
10 Replies

5. Solaris

Polling on socket descriptor does not return pollhup

I have a query related to the functioning of poll() system call on solaris and linux platforms. When the client is abnormally terminated, it is observed that on Linux the socket is immediately closed and the server gets ECONNREFUSED. But in case of Solaris it is observed that the socket is not... (0 Replies)
Discussion started by: Amarjeet_7
0 Replies

6. UNIX for Dummies Questions & Answers

File Descriptor

Hi What the below path contains? /proc/<pid>/fd (1 Reply)
Discussion started by: siba.s.nayak
1 Replies

7. Shell Programming and Scripting

File Descriptor

Hello All, Im opening a file desciptor in perl and sending data using print CMD "$xyz". is there a limit to the length of the string that I can give to this CMD at a time. (3 Replies)
Discussion started by: rimser9
3 Replies

8. IP Networking

Can we write a multiple thread to receive from a single socket file descriptor

Hi Friends, I have written a program which will listener for more than 1000 requests per second from a single socket descriptor and then it will process those requestes. Its taking X amount of time. Now i want to reduce that time. Will I can write multiple threads to receive the... (2 Replies)
Discussion started by: pa.chidhambaram
2 Replies

9. UNIX for Dummies Questions & Answers

File Descriptor Help

What is a file descriptor in Unix?? How to find a file descriptor of a file in Unix?? Does it have anything to do with the Inode numbers?? (3 Replies)
Discussion started by: rahulrathod
3 Replies

10. UNIX for Dummies Questions & Answers

file activity (open/closed) file descriptor info using KORN shell scripting

I am trying to find a way to check the current status of a file. Such as some cron job processes are dependent on the completion of others. if a file is currently being accessed / modified or simply open state I will wait until it is done being processed before attempting the next process on that... (3 Replies)
Discussion started by: Gary Dunn
3 Replies
Login or Register to Ask a Question