By : Basant pandey
INTRODUCTION TO SOCKET
Provides an abstraction for
interprocess communication
Socket: An interface between an application
process and transport layer.
The application process can send/receive messages
to/from another application process (local or remote) via a
socket.
In Unix jargon, a socket is a file descriptor an
integer associated with an open file.
Types of Sockets: Internet Sockets, unix sockets,
X.25 sockets etc.
Internet sockets characterized by IP Address (4 bytes),
port number (2 bytes)
Basically, it says this: a packet is born, the packet is
wrapped ("encapsulated") in a header (and rarely a footer)
by the first protocol (say, the TFTP protocol), then the whole
thing (TFTP header included) is encapsulated again by the
next protocol (say, UDP), then again by the next (IP), then
again by the final protocol on the hardware (physical) layer
(say, Ethernet).
When another computer receives the packet, the hardware
strips the Ethernet header, the kernel strips the IP and UDP
headers, the TFTP program strips the TFTP header, and it
finally has the data.
SOCK_STREAM
a.k.a. TCP
reliable delivery
in-order guaranteed
connection-oriented
bidirectional
App
3
SOCK_DGRAM
a.k.a. UDP
unreliable delivery
no order guarantees
no notion of connection
app indicates dest. for
each packet
can send or receive
D1
App
socket
Dest.
3 2
D2
socket
D3
6
Each host has
65,536 ports
Some ports are
reserved for specific
apps
Port 0
Port 1
Port 65535
20,21: FTP
A socket provides an interface to
23: Telnet
send data to/from the network
80: HTTP
through a port
see RFC 1700 (about
2000 ports are
reserved)
Client
Server
Create a socket
Setup the server address
Connect to the server
Read/write data
Shutdown connection
Server high level view
Create a socket
Bind the socket
Listen for connections
Accept new client connections
Read/write to client connections
Shutdown connection
socket()
TCP Server
bind() Well-known port
TCP Client
listen()
Socket()
accept()
Connection establishment blocks until connection from client
connect()
Data(request)
write()
read()
process request
read()
close()
Data(reply)
End-of-file n
otification
write()
read()
close()
struct sockaddr_in {
short int
unsigned short int
struct in_addr
unsigned char
};
Here is the description of the
Attribute
Values
sin_family;
sin_port;
sin_addr;
sin_zero[8];
member fields:
Description
sa_family
AF_INET
It represents an address family. In most of the
AF_UNIX
Internet-based applications, we use AF_INET.
AF_NS
AF_IMPLINK
sin_port
Service Port A 16-bit port number in Network Byte Order.
sin_addr
IP Address
A 32-bit IP address in Network Byte Order.
sin_zero
Not Used
You just set this value to NULL as this is not being
used.
Address and port are stored as
integers
struct in_addr {
u_short sin_port; (16 bit)
u_long s_addr;
in_addr sin_addr; (32 bit)
};
Problem:
different machines / OSs use different word orderings
little-endian: lower bytes first
big-endian: higher bytes first
these machines may communicate with one another over the network
128.119.40.12
128
Big-Endian
machine
119
40
12
!!
12.40.119.128
!
G
N
O
Little-Endian
machine
119
R
W
128
40
12
16
Defs:
Host Byte-Ordering: the byte ordering used by
a host (big or little)
Network Byte-Ordering: the byte ordering used
by the network always big-endian
Any words sent through the network should be
converted to Network Byte-Order prior to
transmission (and back to Host Byte-Order
once received)
Q: should the socket perform the conversion
automatically?
17
Byte Ordering
Two types of Byte ordering
Network Byte Order: High-order byte of the number is
stored in memory at the lowest address
-
Host Byte Order: Low-order byte of the number is
stored in memory at the lowest address
-
Network stack (TCP/IP) expects Network Byte
Order
- htons() - Host to Network Short
Conversions:
-
htonl() - Host to Network Long
ntohs() - Network to Host Short
ntohl() - Network to Host Long
u_long htonl(u_long x);
u_short htons(u_short x);
u_long ntohl(u_long x);
u_short ntohs(u_short x);
On big-endian machines, these routines do nothing
On little-endian machines, they reverse the byte order
hto
128
119
40
12
Little-Endian 12
machine
128
119
40
40
128.119.40.12
119 128
12
ntohl
nl
128
119 40
128.119.40.12
Big-Endian
12machine
Same code would have worked regardless of endian-ness of
the two machines
19
bind() - what port am I on?
Used to associate a socket with a port on the local
machine
The port number is used by the kernel to match an
incoming packet to a process
-
int bind(int sockfd, struct sockaddr *my_addr, int addrlen)
-
sockfd is the socket descriptor returned by socket()
my_addr is pointer to struct sockaddr that contains
information about your IP address and port
addrlen is set to sizeof(struct sockaddr)
returns -1 on error
my_addr.sin_port = 0; //choose an unused port at
random
my_addr.sin_addr.s_addr = INADDR_ANY; //use my IP
adr
connect() - Hello!
Connects to a remote host
int connect(int sockfd, struct sockaddr *serv_addr, int
addrlen)
-
sockfd is the socket descriptor returned by socket()
serv_addr is pointer to struct sockaddr that
contains information on destination IP address and
port
addrlen is set to sizeof(struct sockaddr)
returns -1 on error
No need to bind(), kernel will choose a port
listen() - Call me please!
Waits for incoming connections
int listen(int sockfd, int backlog);
-
sockfd is the socket file descriptor returned by
socket()
backlog is the number of connections allowed on
the incoming queue
listen() returns -1 on error
Need to call bind() before you can listen()
socket()
bind()
listen()
accept()
accept() - Thank you for calling !
accept() gets the pending connection on the
port you are listen()ing on
int accept(int sockfd, void *addr, int *addrlen);
-
sockfd is the listening socket descriptor
information about incoming connection is stored in
addr which is a pointer to a local struct sockaddr_in
addrlen is set to sizeof(struct sockaddr_in)
accept returns a new socket file descriptor to use
for this accepted connection and -1 on error
send() and recv() - Let's talk!
The two functions are for communicating over
stream sockets or connected datagram sockets.
int send(int sockfd, const void *msg, int len, int
flags);
-
sockfd is the socket descriptor you want to send data to
(returned by socket() or got from accept())
msg is a pointer to the data you want to send
len is the length of that data in bytes
set flags to 0 for now
sent() returns the number of bytes actually sent (may
be less than the number you told it to send) or -1 on
error
send() and recv() - Let's talk!
int recv(int sockfd, void *buf, int len, int flags);
-
sockfd is the socket descriptor to read from
buf is the buffer to read the information into
len is the maximum length of the buffer
set flags to 0 for now
recv() returns the number of bytes actually read into
the buffer or -1 on error
If recv() returns 0, the remote side has closed
connection on you
sendto() and recvfrom() - DGRAM
style
int sendto(int sockfd, const void *msg, int len, int
flags, const struct sockaddr *to, int tolen);
-
to is a pointer to a struct sockaddr which contains the
destination IP and port
tolen is sizeof(struct sockaddr)
int recvfrom(int sockfd, void *buf, int len, int flags,
struct sockaddr *from, int *fromlen);
-
from is a pointer to a local struct sockaddr that will be
filled with IP address and port of the originating
machine
fromlen will contain length of address stored in from
close() - Bye Bye!
int close(int sockfd);
-
Closes connection corresponding to the socket
descriptor and frees the socket descriptor
Will prevent any more sends and recvs
Summary
Sockets help application process to communicate
with each other using standard Unix file
descriptors
Two types of Internet sockets: SOCK_STREAM
and SOCK_DGRAM
Many routines exist to help ease the process
of
communication
References
Books:
Unix Network Programming, volumes 1-2 by W.
Richard Stevens.
-
TCP/IP Illustrated, volumes 1-3 by W. Richard
Stevens and Gary R. Wright
-
Web Resources:
-
Beej's Guide to Network Programming
www.ecst.csuchico.edu/~beej/guide/net/