Professional Documents
Culture Documents
What Is A Socket? Using Sockets
What Is A Socket? Using Sockets
What is a socket?
Using sockets
Types
(Protocols)
Associated functions
Styles
What is a socket?
An interface between application and
network
SOCK_STREAM
a.k.a. TCP
reliable delivery
in-order guaranteed
connection-oriented
Bidirectional
Bind(), listen(),accept()
and connect()
App
3 2
1
a.k.a. UDP
unreliable delivery
no order guarantees
Send or receive
D1
App
socket
Dest.
3 2
1
D2
socket
D3
3
Model of Socket
Step 1 : Create a socket
Step 2: connect the socket to the address
of the server
Model of Socket
Step 1 : Create a socket
Step 2: connect the socket to the address
of the server
sa_family
specifies which
address family is
being used
determines how the
remaining 14 bytes
are used
The Internet-specific:
struct sockaddr_in {
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
sin_family = AF_INET
sin_port: port # (0-65535)
sin_addr: IP-address
sin_zero: unused
Struct in_addr
Struct hostent
Struct servent
Ports
Each host has 65,536
ports
Some ports are
reserved for specific
apps
20,21: FTP
23: Telnet
80: HTTP
Port 0
Port 1
Port 65535
10
integers
struct in_addr {
u_long s_addr;
};
Problem:
different machines / OSs use different word orderings
little-endian: lower bytes first
big-endian: higher bytes first
128.119.40.12
128
Big-Endian
machine
119
40
12
Little-Endian
machine
N
O
R119
W
128
!
!
!
G
40
12.40.119.128
12
11
order
hto
128
119
40
12
Little-Endian12
machine
128
119
40
128.119.40.12
40
119 128
12
ntohl
nl
128
119 40
128.119.40.12
Big-Endian
12machine
12
IPv4
Inet_aton
it converts address
to number
Inet_ntoa
network address to
number
Inet_addr
internet host
address to number
Function converting
IPv6
Inet_pton
It convert text to
binary
Inet_ntop
It converts IPv4
and IPv6 from
binary to text form
13