Taken from the original at http://beej.us/guide/bgnet/ ... see end of writeup for Copyright statement.

socket()--Get the File Descriptor!

I guess I can put it off no longer -- I have to talk about the socket() system call. Here's the breakdown:

#include <sys/types.h>
#include <sys/socket.h>

int socket(int domain, int type, int protocol);

But what are these arguments? First, domain should be set to "AF_INET", just like in the struct sockaddr_in (above.) Next, the type argument tells the kernel what kind of socket this is: SOCK_STREAM or SOCK_DGRAM. Finally, just set protocol to "0". (Notes: there are many more domains than I've listed. There are many more types than I've listed. See the socket() man page. Also, there's a "better" way to get the protocol. See the getprotobyname() man page.)

socket() simply returns to you a socket descriptor that you can use in later system calls, or -1 on error. The global variable errno is set to the error's value (see the perror() man page.)


Prev | Up | Next


Copyright © 1995, 1996 by Brian "Beej" Hall. This guide may be reprinted in any medium provided that its content is not altered, it is presented in its entirety, and this copyright notice remains intact. Contact beej@ecst.csuchico.edu for more information.