Basant
Basant
Basant
INTRODUCTION TO SOCKET
Provides an abstraction for
interprocess communication
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
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
socket()
TCP Server
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
sin_addr
IP Address
sin_zero
Not Used
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
Byte Ordering
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
returns -1 on error
connect() - Hello!
returns -1 on error
socket()
bind()
listen()
accept()
Summary
Sockets help application process to communicate
with each other using standard Unix file
descriptors
References
Books:
Unix Network Programming, volumes 1-2 by W.
Richard Stevens.
-
Web Resources:
-
www.ecst.csuchico.edu/~beej/guide/net/