Thursday, March 25, 2010

Lecture 15: Socket Programming

The lecture for Wednesday, March 25, focused itself primarily on Socket Programming. Sockets are and Application Programming Interface (API) for use with TCP/IP. The network API are services that provide an interface between the application and the protocol software. When we look at a network API, we want to have certain features. These features include, a Generic Programming interface which supports multiple communication protocol suits. Other features include support for both message oriented and connection oriented communication, the ability to work with existing I/O services, and be independent of the OS. There are also certain functions needed, such as specify local and remote connection endpoints, initiate the connection, wait for incoming connections, send and recieve data, terminate a connection, and handle errors.

A socket is an abstract representation of a communication endpoint that needs to establish a connection and specify communication endpoint addresses. In order to create a socket we must call a function, int socket(int family, int type, int proto); where family specifies the protocol familty, type specifies the type of service and protocol specifies the specific protocol. This function will return a socket descriptor or -1 on an error, and will allocate the resources needed for a communication endpoint, but endpoint addressing has yet to be dealt with. Generic socket addresses are added through a struct, that contains three values, a address length, family, and the address value. This addressing can be done bothe in IPv4 and IPv6.

Once the addressing has been handled it is time to bind the address to a socket. To do this we use the bind() system call. If the bind returns successfully it will return a 0 or a -1 on error. Calling bind() assigns the address specified by the structure to the socket descriptor. There are a number of uses for bind, such as allowing a client to bind to a port, or a server to a well known address.

At the end of the lecture we went over some more socket system calls, such as the general use ones of read(), write(), or close(). There are also specific calls for connection oriented(TCP), connect(), listen(), and accept(). As well as calls for connectionless service(UDP), send(), and recv().

No comments:

Post a Comment