Unit2 1
Unit2 1
Introduction to Sockets
1
Socket Interface
What is the Socket interface?
It is a protocol independent interface to
multiple transport layer primitives
It is an interface (a “door”) into which an
application process can both send and receive
messages to/from another (remote or local)
application process
History
The first implementation was released along
with BSD 4.2. Since then, it has undergone
several improvements
2
Typical Client-Server interaction using
TCP
Client Server
socket()
well-known port bind()
socket() listen()
connect() accept()
Connection establishment
TCP three-way handshake Block until
connection from client
write()
Data (request)
read()
Process requests
close()
read()
End-of-file notification
close() 3
Creating a Socket
#include <sys/socket.h>
4
Closing a socket
#inlcude <unistd.h>
5
Establishing a connection
#include <sys/socket.h>
6
Background Information:
Socket Address Structure
This contains the protocol specific
addressing information
The general structure is named sockaddr
Each of the protocols supported by a socket
implementation have their own socket
address structure sockaddr_suffix
-Where ‘suffix’ represents the protocol
family
Eg: sockaddr_in – Internet/IPv4 socket
address structure
sockaddr_ipx – IPX socket address
structure
7
Structure(POSIX)
struct sockaddr {
sa_family_t sa_family; /*Address Family, AF_XXX*/
char sa_data[14]; /*14 bytes of protocol address*/
};
struct sockaddr_in {
sa_family_t sin_family; /* must be AF_INET */
in_port_t sin_port; /*16-bit TCP or UDP port number*/
___ /*network byte ordered*/
struct in_addr sin_addr; /*32-bit IPv4 address*/
----- /*network byte ordered*/
char sin_zero[8]; /* Not used, must be zero */
};
struct in_addr {
in_addr_t s_addr; /* 32-bit IPv4 address*/
/*network byte ordered */
};
8
Associating a local
address with a socket
#include <sys/socket.h>
10
Listening for a connection
#include <sys/socket.h>
12
Functions for
sending/receiving data
#include <sys/types.h>
#include <sys/socket.h>
13
Some additional functions
In network programming, we often
need the help of some additional
functions for tasks like:
Byte ordering
Byte operations
Address Conversions
14
Byte Ordering
Increasing memory address
Big-endian
byte order High-order byte Low-order byte
Address A Address A+1
Increasing memory address
15
Implications of Byte Order
Unfortunately there is no standard byte
order- some systems are big endian, the
others little endian
We refer to the byte ordering used by a
given system as host byte order
The sender and the receiver must agree
on the order in which the bytes of multi-
byte field transmitted.
Hence we specify the network byte
order, which is big-endian byte ordering
16
Byte Order Functions
#include <netinet/in.h>
#include <sys/types.h>
17
Byte Manipulation
Functions
#include <string.h>
18
Address Conversion
Functions
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
19
Address Conversion
Functions
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
const char *inet_ntop(int family, const void *addrptr, char *strptr, size_t
len);
/*returns pointer to result if OK, NULL on error*/
20
References
Unix Network Programming :Vol 1-
Stevens
Network Programming for
Microsoft Windows –Jones, Ohlund
Java Network Programming –
Rusty, Harold
TCP/IP Illustrated - Stevens
21