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
BIO_s_socket(3) 						      OpenSSL							   BIO_s_socket(3)

NAME
BIO_s_socket, BIO_new_socket - socket BIO SYNOPSIS
#include <openssl/bio.h> BIO_METHOD *BIO_s_socket(void); long BIO_set_fd(BIO *b, int fd, long close_flag); long BIO_get_fd(BIO *b, int *c); BIO *BIO_new_socket(int sock, int close_flag); DESCRIPTION
BIO_s_socket() returns the socket BIO method. This is a wrapper round the platform's socket routines. BIO_read() and BIO_write() read or write the underlying socket. BIO_puts() is supported but BIO_gets() is not. If the close flag is set then the socket is shut down and closed when the BIO is freed. BIO_set_fd() sets the socket of BIO b to fd and the close flag to close_flag. BIO_get_fd() places the socket in c if it is not NULL, it also returns the socket. If c is not NULL it should be of type (int *). BIO_new_socket() returns a socket BIO using sock and close_flag. NOTES
Socket BIOs also support any relevant functionality of file descriptor BIOs. The reason for having separate file descriptor and socket BIOs is that on some platforms sockets are not file descriptors and use distinct I/O routines, Windows is one such platform. Any code mixing the two will not work on all platforms. BIO_set_fd() and BIO_get_fd() are macros. RETURN VALUES
BIO_s_socket() returns the socket BIO method. BIO_set_fd() always returns 1. BIO_get_fd() returns the socket or -1 if the BIO has not been initialized. BIO_new_socket() returns the newly allocated BIO or NULL is an error occurred. SEE ALSO
TBA 0.9.7a 2000-10-19 BIO_s_socket(3)
All times are GMT -4. The time now is 01:06 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy