Sagar Patni
Sagar Patni

Reputation: 312

difference between file descriptor and socket file descriptor

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?

Upvotes: 3

Views: 1788

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409136

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.

Upvotes: 3

Related Questions