02 March, 2015

Berkeley Socket API

Berkeley Socket API:
Developed in the early 1980s at the University of California at Berkeley.  There are communications tools that are built on tool of Berkeley sockets.  (E.g. RPC ).
 It is an API.  Its implementation is usually requires kernel code. It is the defacto standard for communications programming.  There are higher level tools for programs that span more than one machine.  RPC, DCOM and windows remoting are examples.
Used for point-to-point communications between computers through an inter-systems pipe.  Namely can use the UNIX read, write, close, select, etc. system calls. 
Supports broadcast.  This is where the same message may be delivered to multiple systems on a network without additional overhead.  Available on every UNIX system.  Build for client/server development.  That is having one system provide a service to other systems.
e.g.  Remote Procedure Call (RPC)

Types of sockets :
Two types of socket one is TCP (Stream socket) and second is UDP (Datagram socket).

TCP (Stream socket)
Connection-oriented (includes establishment + termination),
 Reliable, in order delivery
At-most-once delivery, no duplicates E.g., ssh, http




UDP (Datagram socket)

Connectionless (just data-transfer)
“Best-effort” delivery, possibly lower variance in delay
E.g., IP Telephony, streaming audio.


Commmon System Calls/functions used for TCP and IP

Socket() - creates a socket for network communications.  Returns a file descriptor for the socket.
Prototype:    int Socket (family, type, protocol)
Family- IPv4 or IPv6
Type- TCP or UDP
Protocol: 0 not used for internet socket.
bind()- assigns a socket to an address. When a socket is created using socket(), it is only given a protocol family, but not assigned an address. This association with an address must be performed with the bind() system call before the socket can accept connections to other hosts. bind() takes three arguments:
Prototype: int bind(int fd, const struct sockaddr *my_addr, socklen_t addrlen);
              fd, a descriptor representing the socket to perform the bind on.
             my_addr, a pointer to a sockaddr structure representing the address to bind to.
             addrlen, a socklen_t field specifying the size of the sockaddr structure.
Bind() returns 0 on success and -1 if an error occurs.Syntax: Bind(Fd,addr,addrlen)

Listen - After a socket has been associated with an address, listen() prepares it for incoming connections. However, this is only necessary for the stream-oriented (connection-oriented) data modes, i.e., for socket types (SOCK_STREAM, SOCK_SEQPACKET). listen() requires two arguments:
 backlog, an integer representing the number of pending connections that can be queued up at any one time. The operating system usually places a cap on this value. Once a connection is accepted, it is dequeued. On success, 0 is returned. If an error occurs, -1 is returned.
Prototype: int listen(int sockfd, int backlog);
Backlog: Maximum no of connection.

Connect - called by the client process to establish a connection between it and a server process.  Need the server's address and port number.  The file descriptor from the socket call is one of the arguments.  Once the connection is made, this file descriptor may be used for communications. Times out after 75 seconds if no response.
 Syntax: Connect(Fd,addr,addrlen)
Addr: Pointer to a struct sockaddr
Addrlen: Size of the struct in byte

Accept - When an application is listening for stream-oriented connections from other hosts, it is notified of such events (cf. select() function) and must initialize the connection using the accept() function. The accept() function creates a new socket for each connection and removes the connection from the listen queue. It takes the following arguments:
The accept() function returns the new socket descriptor for the accepted connection, or -1 if an error occurs. All further communication with the remote host now occurs via this new socket. Datagram sockets do not require processing by accept() since the receiver may immediately respond to the request using the listening socket.
Prototype: int accept(int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen);
                   sockfd, the descriptor of the listening socket that has the connection queued.
                   cliaddr, a pointer to a sockaddr structure to receive the client's address information.
 addrlen, a pointer to a socklen_t location that specifies the size of the client address structure passed to accept(). When accept() returns, this location indicates how many bytes of the structure were actually used.

write - to send data on the connection.   If connection broken your program will receive a SIGPIPE signal or an error is returned. 

read - to get data that was sent on the connection.  It returns the number of bytes read.  0 is returned if the connection broken.

select - used to determine if there is data available on a socket or if there is a client queued up for a connection to a server. 

close - to de-allocate the socket.

No comments:

Post a Comment