Sponsored Content
Top Forums Programming Get the file descriptor of a socket file. C vs Python. Post 302204431 by goon12 on Wednesday 11th of June 2008 03:02:40 PM
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
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
SOCKET_SET_NONBLOCK(3)							 1						    SOCKET_SET_NONBLOCK(3)

socket_set_nonblock - Sets nonblocking mode for file descriptor fd

SYNOPSIS
bool socket_set_nonblock (resource $socket) DESCRIPTION
The socket_set_nonblock(3) function sets the O_NONBLOCK flag on the socket specified by the $socket parameter. When an operation (e.g. receive, send, connect, accept, ...) is performed on a non-blocking socket, the script will not pause its execu- tion until it receives a signal or it can perform the operation. Rather, if the operation would result in a block, the called function will fail. PARAMETERS
o $socket - A valid socket resource created with socket_create(3) or socket_accept(3). RETURN VALUES
Returns TRUE on success or FALSE on failure. EXAMPLES
Example #1 socket_set_nonblock(3) example <?php $socket = socket_create_listen(1223); socket_set_nonblock($socket); socket_accept($socket); ?> This example creates a listening socket on all interfaces on port 1223 and sets the socket to O_NONBLOCK mode. socket_accept(3) will immediately fail unless there is a pending connection exactly at this moment. SEE ALSO
socket_set_block(3), socket_set_option(3), stream_set_blocking(3). PHP Documentation Group SOCKET_SET_NONBLOCK(3)
All times are GMT -4. The time now is 12:27 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy