Computer Networks (CS425) : Unix Socket Programming
Computer Networks (CS425) : Unix Socket Programming
What is a Socket ?
In unix, whenever there is a need for inter process communication within the same machine, we use
mechanism like signals or pipes(named or unnamed). Similarly, when we desire a communication
between two applications possibly running on different machines, we need sockets. Sockets are treated
as another entry in the unix open file table. So all the system calls which can be used for any IO in unix
can be used on socket. The server and client applications use various system calls to conenct which use
the basic construct called socket. A socket is one end of the communication channel between two
applications running on different machines.
Steps followed by client to establish the connection:
1. Create a socket
2. Connect the socket to the address of the server
http://www.cse.iitk.ac.in/users/dheeraj/cs425/lec17.html
1/4
11/8/2014
3. Send/Receive data
4. Close the socket
Steps followed by server to establish the connection:
1.
2.
3.
4.
5.
Create a socket
Bind the socket to the port number known to all clients
Listen for the connection request
Accept connection request
Send/Receive data
struct sockaddrs {
unsigned short
char
};
sa_family;
sa_data[14];
AF stands for Address Family and PF stands for Protocol Family. In most modern implementations only
the AF is being used. The various kinds of AF are as follows:
Name
AF_UNIX, AF_LOCAL
AF_INET
AF_INET6
AF_IPX
AF_NETLINK
AF_X25
AF_AX25
AF_ATMPVC
AF_APPLETALK
AF_PACKET
Purpose
Local communication
IPv4 Internet protocols
IPv6 Internet protocols
IPX - Novell protocols
Kernel user interface device
ITU-T X.25 / ISO-8208 protocol
Amateur radio AX.25 protocol
Access to raw ATM PVCs
Appletalk
Low level packet interface
struct sockaddr_in {
short int
unsigned short int
struct in_addr
unsigned char
};
sin_family;
sin_port;
sin_addr;
sin_zero[8];
//
//
//
//
Address family
Port number
Internet address
Same size as struct sockaddr
Some systems (like x8086) are Little Endian i-e. least signficant byte is stored in the higher address,
whereas in Big endian systems most significant byte is stored in the higher address. Consider a situation
where a Little Endian system wants to communicate with a Big Endian one, if there is no standard for
data representation then the data sent by one machine is misinterpreted by the other. So standard has been
defined for the data representation in the network (called Network Byte Order) which is the Big Endian.
The system calls that help us to convert a short/long from Host Byte order to Network Byte Order and
viceversa are
http://www.cse.iitk.ac.in/users/dheeraj/cs425/lec17.html
2/4
11/8/2014
IP addresses
Assuming that we are dealing with IPv4 addresses, the address is a 32bit integer. Remembering a 32 bit
number is not convenient for humans. So, the address is written as a set of four integers seperated by
dots, where each integer is a representation of 8 bits. The representation is like a.b.c.d, where a is the
representation of the most significant byte. The system call which converts this representation into
Network Byte Order is:
int inet_aton(const char *cp, struct in_addr *inp);
inet_aton() converts the Internet host address cp from the standard numbers-and-dots notation into
binary data and stores it in the structure that inp points to. inet_aton returns nonzero if the address is
valid, zero if not.
For example, if we want to initialize the sockaddr_in construct by the IP address and desired port
number, it is done as follows:
This system call returns a Socket Descriptor (like file descriptor) which is an integer value. Details about
the Arguments:
1. Domain: It specifies the communication domain. It takes one of the predefined values described
under the protocol family and address family above in this lecture.
2. Type: It specifies the semantics of communication , or the type of service that is desired . It takes
the following values:
SOCK_STREAM : Stream Socket
SOCK_DGRAM : Datagram Socket
SOCK_RAW : Raw Socket
SOCK_SEQPACKET : Sequenced Packet Socket
SOCK_RDM : Reliably Delivered Message Packet
3. Protocol: This parameter identifies the protocol the socket is supposed to use . Some values are as
follows:
IPPROTO_TCP : For TCP (SOCK_STREAM)
IPPROTO_UDP : For UDP (SOCK_DRAM)
Since we have only one protocol for each kind of socket, it does not matter if we do not define any
protocol at all. So for simplicity, we can put "0" (zero) in the protocol field.
3/4
11/8/2014
The system call bind associates an address to a socket descriptor created by socket.
int bind
( int sockfd
int addrlen );
The second parameter myaddr specifies a pointer to a predefined address of the socket.Its structure is a
general address structure so that the bind system call can be used by both Unix domain and Internet
domain sockets.
http://www.cse.iitk.ac.in/users/dheeraj/cs425/lec17.html
4/4