![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
>>>
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);
}
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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
I seem to have found a working solution: lstat.
Code:
if (lstat("/tmp/mapping-foo", &sbuf) <= -1) {
perror("lstat()");
}
Thanks again, Joe |
|
#3
|
||||
|
||||
|
Quote:
un.sun_family=AF_UNIX; strcpy(un.sun_path, "(/tmp/mapping-foo"); fd=socket(AF_UNIX,SOCK_STREAM,0); |
||||
| Google The UNIX and Linux Forums |