Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between file descriptor and socket file descriptor

Tags:

c

sockets

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?

like image 386
Sagar Patni Avatar asked May 22 '26 12:05

Sagar Patni


1 Answers

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.

like image 111
Some programmer dude Avatar answered May 24 '26 02:05

Some programmer dude



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!