Sockets

Establishing Communications

Connection Oriented Server

Connection Oriented Client

Connectionless Server

Connectionless Client

System Calls

socket()

socket (int family, int type, int protocol)

Where:
family Address family:
  • AF_UNIX - Unix internal protocol.
  • AF_INET - Internet protocol.
  • AF_NS - Xerox NS protocol.
  • AF_IMPLINK - IMP Link Layer.
ftype Socket type:
  • SOCK_STREAM - Stream socket, e.g. TCP.
  • SOCK_DGRAM - Datagram socket, e.g. UDP.
  • SOCK_RAW - Raw socket.
  • SOCK_SEQPACKET - Sequenced packet socket.
  • SOCK_RDM - Reliable deliverd message socket.
protocol Usually this is set to 0, as the choice is obvious given the family and type. Where the choice is not obvious, the protocol parameter is used to decide the protocol to use. For example, ICMP is specified by:

socket (AF_INET, SOCK_RAW, IPPROTO_ICMP)

Raw data over IP is specified by:

socket (AF_INET, SOCK_RAW, IPPROTO_RAW)

bind()

bind (int sockfd, struct sockaddr *myaddr, int addrlen)

A call to bind() specifies the local endpoint address. Where:
sockfd The socket descriptor returned from a socket() call.
myaddr The address of this endpoint.

in_addr.s_addr = htonl (INADDR_ANY) means that the process will accept connections on any internet interface. sin_port = htons (0) means that the system will assign an unused port between 1024 and 5000.

addrlen The length of the address structure.

connect()

connect (int sockfd, struct sockaddr *myaddr, int addrlen)

A call to connect() connects to a remote endpoint Where:
sockfd The socket descriptor returned from a socket() call.
myaddr The address of the remote endpoint.
addrlen The length of the address structure.

The system calls sets errno to EINPROGRESS if the socket is non-blocking and the connection is not made immediately (which is normally the case). Successful connection is determined by a call to select() indicating writable and getsockopt (sockfd, SO_ERROR, SOL_ERROR,,) returning 0.

listen()

listen (int sockfd, int backlog)

A call to listen() registers the process as willing to accept connections. Where:
sockfd The socket descriptor returned from a socket() call.
backlog The maximum size of the connection queue. Usually 5

accept()

accept (int sockfd, struct sockaddr *peer, int *addrlen)

A call to accept() returns a socket representing a new, incoming connection. Where:
sockfd The socket descriptor returned from a socket() call.
peer The address of the remote endpoint. This area of memory is written by the accept() call.
addrlen The length of the address structure. On entry this contains the size of the peer structure. On return it contains the amount of memory written.

close()

close(int sockfd)

This call closes the socket and connections (if any). The closedown is orderly, data in transit is delivered to the peer. Where:
sockfd The socket descriptor.

Note that this is the standard Unix system call for closing files.

shutdown()

shutdown(int sockfd, int howto)

This call closes the socket and connections (if any). The closedown is orderly, data in transit is delivered to the peer. Where:
sockfd The socket descriptor.
howto
  • 0 - Receives are not allowed.
  • 1 - Socket output is not allowed.
  • 2 - Sends and receives are not allowed.

Data Transfer

send (int sockfd, char *buff, int nbytes, int flags)
sendto (int sockfd, char *buff, int nbytes, int flags, struct sockaddr *to, int addrlen)
recv (int sockfd, char *buff, int nbytes, int flags)
recvfrom (int sockfd, char *buff, int nbytes, int flags, struct sockaddr *from, int addrlen)

These calls send and receive data. Where:
sockfd The socket descriptor.
buff Contains the data to be sent or the data received. The memory is always provided to the call.
nbytes In send calls, the amount of data to transmit.

In receive calls; this value is set by the caller to indicate the number of bytes provided in buff. It is set by the receive functions to indicate the amount of data received.

flags The flags value is the OR of:
  • MSG_OOB - Send or receive out-of-band data.
  • MSG_PEEK - Peek at the incoming data.
  • MSG_DONTROUTE - Bypass routing.
to The address to which the data should be sent.
from The recvfrom() system call fills this with the address of the sender.
addrlen In sendto() calls, the size of the address structure.

In recvfrom() calls, this value is set by the caller to indicate the number of bytes providedin the In from() address memory. It is set by recvfrom() to indicate the size of the received address.

The read(), write(), readv(), writev(), fcntl(), poll() and select() system calls can be used to transfer data, using a socket descriptor instead of a file descriptor. Additionally, the sendmsg() and recvmsg() calls can be used.

All functions return the amount of data sent or received. In the case of the sending functions, a return value smaller that nbytes indicates that only part of the data was accepted by the operating system, the program should retry the rest of the data later.

An EPIPE error or SIGPIPE signal indicates that the remote process is no longer connected.

Functions and Macros

Byte Ordering

The following functions resolve any differences there might be between the representation of integers on the network (TCP/IP) and in the local host.

#include <sys/types.h>
#include <netinet/in.h>

u_long  htonl (u_long hostlong);
u_short htons (u_short hostshort);
u_long  ntohl (u_long netlong);
u_short ntohs (u_short netshort);

Byte Operations

These functions are for setting the value of memory. They are similar to the System V memory (3) functions.

bcopy (char *src, char *dest, int nbytes);
bzero (char *dest, int nbytes);
int bcmp (char *ptr1, char *ptr2, int nbytes);

Address Conversion


#include        <sys/socket.h>
#include        <netinet/in.h>
#include        <arpa/inet.h>

unsigned long   inet_addr (char *ptr);
char            *inet_ntoa (struct in_addr inaddr);

These functions convert an internet address between dotted decimal format and 32 bits. format and 32 bits.

Socket Options


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

int getsockopt (int sockfd, int level, int optname, char *optval, int *optlen)
int setsockopt (int sockfd, int level, int optname, char *optval, int *optlen)
These calls get and set socket options at various levels. Where:
sockfd The socket descriptor.
level Indicates which part of the system is responsible for the option:
  • IP_PROTO_IP - IP, for options in the IP header.
  • IP_PROTO_TCP - TCP. Segment size, delay.
  • NSPROTO_xxx - XNS.
  • SOL_SOCKET - Sockets. Debugging, buggersizes, timeouts,...
optname The parameter to read/write. This is specific to level.
  • IP_xxx - IP.
  • TCP_xxx - TCP.
  • SO_xxx - Sockets.
optval Buffer for reading or writing.
optlen Length of buffer.

fcntl() options

The fcntl system call can be used to modify the behaviour of the socket interface as follows:

#include <fcntl.h>

int fcntl (int fd, int cmd, int arg);

where:
fd The socket descriptor.
cmd The control command. Values important to sockets are: F_GETOWN, F_SETOWN are used to get/set the process or process group that receives SIGIO and SIGURG events for this class.

F_GETFL, F_SETFL are used to set options.

arg An argument that qualifies the cmd value. In the case of the process commands, this contains the process or group id. For the flag commands, the following values are of interest:

FNDELAY sets the socket as non-blocking. Subsequent systems calls set errno to EWOULDBLOCK instead of blocking.

FASYNC allows the receipt of SIGIO for asynchronous I/O.

ioctl()

The ioctl() system call can be used on sockets. It can be used to examine and change the routing tables, interface addresses, interface parameters, ARP entries etc.

Name conversion

Addressing information for use in the socket calls can be obtained from DNS servers or the /etc/hosts() and /etc/services() files using the following functions:

#include <netdb.h>

struct hostent *gethostbyname (char *hostname);
struct servent *getservbyname (char *servname, char *protname);

References:


Author: Stewart Smith
Last update: 27th October 2009
©Copyright: Pentagon Computer Consultants Ltd 2003-9.