Library Functions, System Calls, and other stuff you need to know about sockets

compiled by Lauren Jantz

This is a quick reference list of functions that might be useful in writing clients and servers and terms that you will encounter in working with them. If you need to know more, look up the man page.

accept

extracts the first connection on the queue of pending connections and creates a new socket with the same properties as s

int accept( int s, struct sockaddr *addr, int *addrlen )

where s is a socket, addr is a struct which contains information about the socket, and addrlen contains the amount of space pointed to by addr

accept returns -1 if there is an error. If it is successful, a non-negative integer that is a descriptor for the socket is returned.

address family

several address representations for one type of address

AF_INET

the address family of TCP/IP protocol (see address family)

bcopy

copies a string of a specified length into another string

void bcopy(char *source, char *destination, int length)

where source and destination are pointers to strings and length is the length of the section of source one wants to copy

bind

specifies a local IP address and protocol port number for a socket

int bind(int s, struct sockaddr *name, int namelen)

where s is a socket descriptor created by the socket call (see socket), name is a pointer to a structure that specifies an IP address and protocol port number, and namelen is the size of the object in bytes

bind returns 0 if successful and -1 if an error has occurred.

bzero

replaces a string with a specified number of zeros

void bzero(char *b, int numberOfZeros)

where b is a pointer to a string and numberOfZeros is however many zeros you want in the string

connect

allows the caller to specify the remote endpoint address for a previously created socket. If the socket uses TCP, connect the 3-way handshake to establish a connection; if the socket uses UDP, connect specifies the remote endpoint but does not transfer any datagrams to it..

int connect(int s, struct sockaddr *name, int namelen)

where s is the socket, name is another socket that the program connects to (this only happens when the socket is of type SOCK_STREAM), namelen is the length of the name of the second socket

connect returns 0 if successful and -1 in the case of an error.

errno

an integer used to point to an error message in an array of possible messages (see sys_errlist). when an error occurs, errno is set automatically to point to the appropriate message.

exit

terminates a process

void exit(int status);

fork

creates an exact copy of the calling or "prompt" process except:

·The child process has a unique process ID.

·The child process has a different parent process ID.

·The child process has its own copies of the parent's descriptors ( file and socket).

·The child processes resource utilizations are set to 0.

pid_t fork(void);

Upon successful completion, fork returns a value of 0 to the child process and returns the process ID of the child process to the parent process. Otherwise, a value of 1 is returned to the parent process, no child process is created, and the global variable errno is set to indicate the error.

gethostbyname

returns pointer to a struct hostent (see hostent)

struct hostent *gethostbyname(char *name)

where name is the name of the host

getprotobyname

returns pointer to a struct protoent (see protoent)

struct protoent *getprotobyname(char *name)

where name is the type of protocol you're using (either "tcp" or "udp")

hostent

structure that contains information about the host (server)

struct hostent {

char *h_name; /* Official name of the host. */

char **h_aliases; /* A zero terminated array of alternate names for the host.*/

int h_addrtype; /* The type of address being returned; currently always AF_INET. see AF_INET */

int h_length; /* The length, in bytes, of the address. */

char **h_addr_list; /* A zero terminated array of network addresses for the host. Host addresses

}; are returned in network byte order. */

htons

converts a short (a 16 bit quantity) in host form to a short in network form

u_short htons(u_short hostshort)

htonl

converts a long(a 32 bit quantity) in host form to a long in network form

u_long htonl(u_long hostlong)

inet_addr:

interprets a character string representing numbers expressed in the Internet standard "." notation and returns a number suitable for use as an Internet address (translates the host name to the IP address)

u_long inet_addr( char *cp )

where cp is a pointer to the name of the machine for which one wants an IP address

inet_addr returns either a valid IP address or -1 for a name that doesn't match

listen

prepares the socket to wait for requests

int listen(int s, int backlog)

where s is the socket and backlog is the maximum length of the queue of pending connections

a return of 0 is a success and -1 is an error

ntohs

converts a short in network form to a short in host form (see htons)

u_short ntohs(u_short netshort)

ntohl

converts a long in network form to a long in host form (see htonl)

u_long ntohl(u_long netlong)

protoent

structure that contains information about each type of protocol

struct protoent {

char *p_name; /*Official name of the protocol*/

char **p_aliases; /*A zero terminated list of alternate names for the protocol*/

int p_proto; /*The protocol number*/

};

read

reads "nbytes" of data from the object referenced by a descriptor d

int read(int d, char *buf, int nbytes)

nbytes is the number of bytes you want read into buf, and if read is successful, the number of bytes actually read is returned; otherwise, 1 is returned and errno is set to indicate the error. 0 is returned when the EOF character is read.

SIGCHLD

SIGCHLD 20 child status has changed

signal

Most signals cause termination of the receiving process if no action is taken; some signals instead cause the process receiving them to be stopped, or are simply discarded if the process has not requested otherwise. Except for the SIGKILL and SIGSTOP signals, the signal call allows signals either to be ignored or to cause an interrupt to a specified location.

int *signal(int sig, void (*func()))

where sig is a predefined signal found in <sys/signal.h> that is sent to another function (we use will use SIGCHLD which is defined above). The value of signal is the value of the function that the signal is sent to. If the call is unsuccessful, signal returns 1 (we'll void it and not worry about the return).

sockaddr

only contains information about the type of address of the server

struct sockaddr { /*struct to hold an address*/

u_short sa_family; /*type of address*/

char sa_data[14]; /*value of address*/

}

sockaddr_in

can contain more information about the server

struct sockaddr_in {

u_short sin_family; /*type of address*/

u_short sin_port; /*protocol port number*/

u_long sin_addr; /*IP address*/

char sin_zero[8]; /*unused (set to zero)*/

}

socket

creates an endpoint for communication and returns a descriptor

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

domain is the protocol family which should be used choose from. We will always use PF_INET which are the ARPA Internet protocols. There are others which you can find listed on the man page.

type is the type of service choose from:

SOCK_STREAM (stream service - tcp)

SOCK_DGRAM (datagram service - udp)

protocol is the particular protocol to be used with the socket - either tcp or udp.

sys_errlist

an array of error messages

va_list

(for questions about all of the va stuff see the Kernighan and Ritchie book)

a variable that will refer to each argument in turn

va_start

initializes an argument pointer to the first unnamed argument. there must be at least one named argument to get va_start started

va_start(va_list pvar);

pvar is the argument pointer

va_end

finishes up (comes after whatever comes after va_start)

va_end(va_list pvar);

pvar is the argument pointer

wait3

function explained in the variables below

int wait3(union wait *statusp, int options, struct rusage *rusage)

If status is not a NULL pointer, then on return from a successful wait() call the status of the child process whose process ID is the return value of wait() is stored in the wait() union pointed to by status. The options parameter is used to indicate the call should not block if there are no processes that have status to report (WNOHANG). If rusage is not a NULL pointer, a summary of the resources used by the terminated process and all its children is returned. When the WNOHANG option is specified and no processes have status to report, wait3() returns 0. Otherwise, 1 is returned and the errno is set to indicate the error.

write

permits an application to transfer data to a remote machine

int write( int socket, char *buf, int buflen );

socket is the socket or file descriptor. buf is the address of a buffer containing data, buflen is the number of bytes in buf. If the transfer is successful, write returns the number of bytes transferred. If not, it returns 1.



Jeff Ondich, Department of Mathematics and Computer Science, Carleton College, Northfield, MN 55057
(507) 663-4364, jondich@carleton.edu