3

Can I bind a file descriptor returned by open call to a socket?

I am trying to do something like,

 filefd = open("path",O_RDWR);

 ...

 bind (filefd, (struct sockaddr *) &servaddr, sizeof(servaddr));
 connfd = accept (filefd, (struct sockaddr *) &cliaddr, &clilen);

Why does the accept call return -1?

Alex Celeste
  • 12,824
  • 10
  • 46
  • 89
Sagar Patni
  • 312
  • 2
  • 11

1 Answers1

3

From an applications point of view, the difference is how you create and use the descriptor. Some system-calls can take any kind of descriptor, while others require a specific type of descriptor.

In your case the bind call would have returned -1 too, if you checked for the error. When a system-call returns -1 you should check errno to see what went wrong. You can use strerror to get a printable string of the error, or perror to print it directly.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • is it possible for a file and socket to share same fd ? – Sagar Patni Mar 19 '14 at 08:14
  • @SagarPatni No. You can actually think of descriptors as indexes into an array, and if you have two different indexes they will "point" to different places in the array. – Some programmer dude Mar 19 '14 at 08:17
  • what i am trying to do is , provide a socket over character device , for that the overhead of system call is too large , so somehow i am trying to map both files – Sagar Patni Mar 19 '14 at 08:17