Unit-5 Elementary Sockets
Unit-5 Elementary Sockets
Source: UNIX Network Programming: Chapter: 4 W. Richard Stevens, Bill Fenner, Andrew M. Rudoff
Why Socket?
● “To build any networked application”
● E.g.
WWW(Internet Explorer , Firefox)
FTP(WS FTP)
2
Network Programming ?
Telephone Analogy
● A telephone call over a “telephony network” works as follows :
Both parties have a telephone installed
A telephone number is assigned to each telephone
Turn on ringer to listen for a caller
Caller lift telephone and dials a number
Telephone rings and the receiver of the call picks it up
Both parties talk and exchange data.
After conversation over they hangs up the phone.
3
Network application
● An end point (telephone ) for communication is created on both the ends.
● An address (phone number ) is assigned to both ends to distinguish them from the rest of the network.
● One of the end points(caller) initiate a connection to the other.
● The other end (receiver) point waits for the communication to start
● Once a connection has been made. Data is exchanged (talk).
● Once data has been exchanged the end points are closed(hangs up)
4
Interface
● Interface to what? It is an interface to use networks
● API allows application programs to access certain resources through a predefined and
preferably consistent interface
● A connection to the transport layer
5
Unix/windows socket
● Berkeley socket interface(BSI) is the most popular among APIs that provide access to
network resources(Unix)
● Another popular socket interface which was derived from the BSI, is called Windows
sockets or Winsock(Windows)
● The socket mechanism allows programmers to write application programs without worrying
about the underlying networking details.
6
SOCKET Programming in UNIX
What is “socket”?
7
What is “socket”?
● It is an abstraction that is provided to an application programmer to send or receive data to
another process.
● Data can be sent to or received from another process running on the same machine or a
different machine.
● In short , it is an end point of a data connection.
8
Contd..
socket socket
Application 1 Application 2
interface interface
user user
kernel kernel
Socket Socket
Underlying Underlying
communication communication
Protocols Protocols
Communications
network
9
SOCKET Programming in UNIX
Host A Host B
Socket connection
(virtual connection)
Process 1 Process 2
Information Hiding
Network
(physical connection)
10
● http://diranieh.com/SOCKETS/Concepts
11
12
Socket System Calls for Connection-oriented Protocol
13
Server Side
● The server begins by carrying out a passive open as follows.
● (Server when it starts, opens the door for incoming requests but never initiates a service
until it is requested to do so. This is called passive open.)
● The socket call creates a TCP socket.
● The bind call then binds the well-known port number of the server to the socket.
● The listen call turns the socket into a listening socket that can accept incoming connections
from clients.
● The accept call puts the server process to sleep until the arrival of a client connection.
14
Client Side
● The client does an active open(A client opens the communications channel using the IP
address of the remote host and the well-known port address of the specific server program
running on that machine).
● The socket call creates a socket on the client side, and the connect call establishes the TCP
connection to the server with the specified destination socket address.
● When the TCP connections is completed
● The accept function at the server wakes up and returns the descriptor for the given
connection namely, the source IP address, source port number, destination IP address, and
destination port number.
● The client and server are now ready to exchange information.
15
Socket System Calls for Connectionless Protocol
16
CHAPTER 3
SOCKET
INTRODUCTIO
N
Socket Introduction 17
Abstract
● Socket Address Structure
● Value-Result Argument
● Byte Ordering Function
● Byte Manipulation Function
● Other important functions
Socket Address Structure
● Socket = IP address + TCP or UDP port number
● Used in a socket function as a argument (as pointer).
● IP address, TCP or UDP port number, length of structure .....
● Each protocol define its own Socket Address Structure(IPv4, IPv6....)
List the different types of sockets
● Based on Protocol
● Based on Domain/Address Family
● Based on Communication Model
● Specialized Sockets
20
Contd..
Based on Protocol
• Stream Sockets (SOCK_STREAM):
• Use TCP (Transmission Control Protocol).
• Provide reliable, connection-oriented communication.
• Example: Web browsers communicating with web servers.
• Datagram Sockets (SOCK_DGRAM):
• Use UDP (User Datagram Protocol).
• Provide connectionless, unreliable communication.
• Example: Video streaming, online gaming.
• Raw Sockets (SOCK_RAW):
• Allow direct access to lower-level protocols like IP.
• Useful for custom protocol implementation or network diagnostics.
• Example: Ping utility (ICMP protocol).
• Sequenced Packet Sockets (SOCK_SEQPACKET):
• Provide reliable communication like stream sockets but maintain message boundaries.
21
Contd..
Based on Domain/Address Family
• IPv4 Sockets (AF_INET): Use Internet Protocol version 4 (IPv4) for addressing.
• IPv6 Sockets (AF_INET6): Use Internet Protocol version 6 (IPv6) for addressing.
• Unix Domain Sockets (AF_UNIX or AF_LOCAL): Allow communication between
processes on the same host (Inter-Process Communication - IPC).
• Bluetooth Sockets (AF_BLUETOOTH): Used for communication over Bluetooth
protocols.
• CAN Sockets (AF_CAN): Used for Controller Area Network (CAN) communication,
common in automotive systems.
22
Contd..
Based on Communication Model
● Blocking Sockets: Operations like read() or write() block until the operation is complete.
● Non-Blocking Sockets: Operations return immediately, even if they aren’t complete,
enabling asynchronous communication.
● Socket Pairs: Special sockets created for full-duplex communication between processes.
23
Contd..
Specialized Sockets
• Multicast Sockets:
• Used for sending data to multiple recipients in a multicast group.
• Example: IPTV, conferencing systems.
• Broadcast Sockets:
• Used for sending messages to all devices on a network segment.
• Example: DHCP discovery.
• ZeroMQ or Custom Framework Sockets:
• High-level sockets implemented in middleware frameworks for distributed systems.
24
IPv4 Socket Address Structure
The structure sockaddr_in (defined in <netinet/in.h>) is as follows –
Struct in_addr{
in_addr_t s_addr; /*32bit IPv4 address*/
}; /*network byte ordered*/
struct sockaddr_in {
uint8_t sin_len; /* length of structure(16) */
sa_family_t sin_family; /* AF_INET */
in_port_t sin_port; /* 16bit TCP or UDP port number */ /*network byte ordered*/
struct in_addr sin_addr; /* 32bit IPv4 address */ /*network byte ordered*/
char sin_zero[8]; /* unused */
}; /* included in <netinet/in.h> */
Data types required by Posix.1g(protocol independent interface)
Datatype Description Header
int8_t Signed 8bit integer <sys/types.h>
uint8_t Unsigned 8bit integer <sys/types.h>
int16_t Signed 16bit integer <sys/types.h>
uint16_t Unsigned 16bit integer <sys/types.h>
int32_t Signed 32bit integer <sys/types.h>
uint32_t Unsigned 32bit integer <sys/types.h>
Sa_family_t Address family of socket <sys/socket.h>
address structure
Socklen_t Length od socket address <sys/socket.h>
structure normally uint32_t
In_addr_t Ipv4 address, normally <netinet/in.h>
In_port_t uint32_t <netinet/in.h>
TCP or UDP port, normally
uint16_t
Generic Socket address structure
● Socket address structure used as a pointer argument in a function. So used to handling the
unique structure of heterogeneous protocol ANSI C : void*
● <sys/socket.h> : Generic Socket address structure
struct sockaddr {
uint8_t sa_len;
sa_family_t sa_family; /*address family: AF_xxx value*/
char sa_data[14]; /*protocol specific address*/
}
Contd..
● The socket functions are then defined as taking a pointer to the generic socket address
structure as shown in ANSI C function prototype for bind function.
● int bind(int , struct sockaddr * , socklen_t);
Example:
struct sockaddr_in serv; /*IPv4 socket address structure*/
/* fill in serv{} */
bind(sockfd, (struct sockaddr *) &serv, sizeof(serv));
● That is the general structure pointer for handling the structure of heterogeneous protocol
(type casting notice)
IPv6 Socket Address Structure
Struct in6_addr{
uint8_t s6_addr[16]; /*128bit IPv6 address*/
}; /*network byte ordered*/
#define SIN6_LEN /* required for compile-time tests */
struct sockaddr_in6 {
uint8_t sin6_len; /* length of structure(24) */
sa_family_t sin6_family; /* AF_INET6*/
in_port_t sin6_port; /* Transport layer port# */
/*network byte ordered*/
uint32_t sin6_flowinfo; /* priority & flow label */
/*network byte ordered*/
struct in6_addr sin6_addr; /* IPv6 address */
/*network byte ordered*/
}; /* included in <netinet/in.h> */
Contd..
30
Comparison of socket address structure
IPv4 Header
Contd..
IPv6 Header
Value-Result Argument
● Socket address structure are passed as arguments to the socket function, are always passed
as a pointer. (Passed by reference.)
● Socket address structure goes from the process to the kernel methods and vice-versa
● Depending on which direction the structure being passed
Contd..
● Socket address structure pass.
● Kernel to process
struct sockaddr_un cli /* unix domain */
socklen_t len;
len = sizeof(cli);
getpeername(unixfd, (SA*)&cli,&len); /* len may have changed. */
Byte Ordering Function
Increasing memory
address
Address A+1 Address A
Little-endian byte order: High-order byte low-order byte
39
Figure3.9 determine host byte order
#include "unp.h"
int main(int argc, char **argv)
{
union {short s;
char c[sizeof(short)]; } un;
un.s = 0x0102;
printf("%s: ", CPU_VENDOR_OS);
if (sizeof(short) == 2) {
if (un.c[0] == 1 && un.c[1] == 2) printf("big-endian\n");
else if (un.c[0] == 2 && un.c[1] == 1) printf("little-endian\n");
else printf("unknown\n");
} else printf("sizeof(short) = %d\n", sizeof(short));
exit(0);
}
Contd..
● Network program must specify a network byte order
● Internet protocol uses big-endian byte (i384 series is little-endian byte)
● That is the byte order is different between the host byte order and network byte order conversion
needed
● #include<netinet/in.h>
uint16_t htons (uint16_t host16bitvalue); Return : value in network byte order
uint32_t htonl (uint32_t host32bitvalue);
Return : value in host byte order
uint16_t ntohs (uint16_t net16bitvalue);
uint32_t ntohl (uint32_t net32bitvalue);
• htonl and ntohl can be used while converting network IP addresses which are passed to
sin_addr.s_addr field of the sockaddr_in structure.
• htons and ntohs can be used while converting the 16-bit port number that is passed to the
sin_port field of the sockaddr_in structure.
• Example:
server.sin_port = htons(4000);
42
Contd..
● Convert host byte order of port number to network byte order of port number.
● Example: server.sin_port = htons(4000);
● 4000 decimal value is converted to hexadecimal value is (0FA0)16
● Little endian format is used in Intel processor but Internet protocol uses big-endian byte.
● Little endian = 0FA0 => big-endian = A00F
● Network byte order = A00F (A =10, F = 15)
= [ (10 x 163) + (0 x 162) + (0 x 161) + (15 x 160)]
= [ 40960 + 0 + 0 + 15 ]
= 40975
● While debugging the server or client program in VS code IDE you can observe this value in
variable section (server port = 40975 if given port number as 4000)
43
Byte Manipulation Function
● Berkeley-derived functions
● #include <strings.h>
void bzero(void *dest, size_t nbytes);
/* Sets specified no. of bytes to 0 in the dest */
1. inet_aton, inet_ntoa, and inet_addr converts an IPV4 address from a doted decimal string
(e.g ”203.255.74.129”) to its 32 bit network byte ordered binary values.
2. The newer function, inet_pton and inet_ntop, handle both IPv4 and IPv6 addresses.
Example:
server.sin_addr.s_addr = inet_addr("127.0.0.1");
Contd..
#include<arpa/inet.h>
int inet_aton(const char *strptr, struct in_addr *addrptr);
/* return : 1 if string was valid,0 on error */
Inet_aton converts the C character string pointed to by strptr into its 32 bit binary network
byte ordered value, which is stored through the pointer addrptr.
#include<arpa/inet.h>
int inet_pton(int family, const char *strptr, void *addrptr);
/* return: 1 if OK, 0 if input not a valid presentation format, -1 onerror */
/* by string as a binary value */
const char *inet_ntop(int family, const void *addrptr, char *strpt, size_t len);
/* return : pointer to result if OK, NULL on error */
/* len : size of the destination */
/* binary value to a string value */
readn, writen, and readline Functions
● A read or write on a stream socket might input or output fewer bytes than requested, but this is not an
error condition.
● The reason is that buffer limits might be reached for the socket in the kernel.
● All that is required to input or output the remaining bytes is for the caller to invoke the read or write
function again.
● It may read/write less than needed due to finite buffer → call function
● Only if write is nonblocking
#include "unp.h"
ssize_t readn(int filedes, void *buff, size_t nbytes);
ssize_t writen(int filedes, const void *buff, size_t nbytes);
ssize_t readline(int filedes, void *buff, size_t maxlen);
/*All return: number of bytes read or written, –1 on error*/ 51
read():
● Read(): used by a process to receive data from another process running on a remote
machine
int read(int sockfd, const void *buf, int buflen)
● Our three functions look for the error EINTR (the system call was interrupted by a caught
signal)
● It continue reading or writing if the error occurs.
● It handle the error here, instead of forcing the caller to call readn or writen again, since the
purpose of these three functions is to prevent the caller from having to handle a short count.
54
write():
● write(): used by a process to send data to another process running on a remote machine
int write(int sockfd, const void *buf, int buflen)
56