Sockets 101218053457 Phpapp02 PDF
Sockets 101218053457 Phpapp02 PDF
com
NETWORK
SOCKETS
INTRODUCTION TO NETWORK SOCKET
PROGRAMMING AND CONCEPTS
Peter R. Egli
© Peter R. Egli 2015
INDIGOO.COM 1/28
Rev. 4.60
Network Sockets indigoo.com
Contents
1. What is a socket?
2. Socket = Interface to transport API
3. Routing in Network Layers
4. TCP socket „spawning“
5. Socket interface functions
6. Socket calls versus TCP segments
7. Socket calls versus UDP datagrams
8. Socket handle
9. Parameter marshalling / RPC transparency
10. Low level socket programming
11. UDP multicast sockets
12. TCP server socket: C/C++ versus Java example
13. Client socket: C/C++ versus Java example
14. IPv6 sockets
2/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
1. What is a socket?
A socket is an interface for an application to connect to a host‘s network stack (part of the OS).
After connecting, an application is able to bidirectionally exchange data with other processes
on the same or another host.
Socket interface
Network Network
stack (OS) stack (OS)
IP network
3/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
2. Socket = Interface to transport API (host‘s transport protocols)
A socket has a binding to an NSAP (with an IP address) and a TSAP (with a
TCP/UDP/SCTP port number). The NSAP may have a specific IP address or may represent all IP
addresses of the host (unspecified IP address = wildcard address = 0.0.0.0 = inaddr_any).
App
Socket Interface
Socket
Socket
TSAP (Port #)
OSI Layer 4 (transport) TCP
NSAP (IP Address)
OSI Layer 3 (network) IP
IP layer is router
TSAP: Transport Service Access Point (between interfaces
NSAP: Network Service Access Point and transport layer)
4/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
3. Routing in Network Layers (1/4)
The routing of packets from and to a socket depends on the bind IP address.
App
Socket 1. 1. 2.
7 80 4567
TCP
IP1 IP2 IP3 IP4
IP
DL DL
5/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
3. Routing in Network Layers (2/4)
1. Specific IP address binding:
UDP socket:
If a UDP socket is bound to a specific IP address, only IP packets with this destination IP
address are routed to and received by this socket.
TCP socket:
In case of a listening TCP socket, only connection requests (inbound connection) addressed to
the bind IP are accepted by the socket.
2. inaddr_any binding:
If a socket is NOT bound to a specific IP address (INADDR_ANY = 0.0.0.0, wildcard IP address),
the socket is bound to all existing interfaces.
UDP socket:
A UDP socket receives any packet that contains the bind port number as target port.
TCP socket:
A listening TCP-socket bound to 0.0.0.0 is able to accept connections on all interfaces provided
that the destination port of the incoming connection request equals the bind port number.
Once the incoming connection is accepted, the created TCP-socket is bound to the destination
IP address of the incoming connection request.
6/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
3. Routing in Network Layers (3/4)
Localhost binding and routing of outbound packets:
App App
Socket
3.
7 4567 23
TCP
IP1 IP2 IP3 IP4 127.0.0.1
IP
DL DL
7/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
3. Routing in Network Layers (4/4)
3. localhost binding:
If a socket is bound to “localhost”=127.0.0.1, then this socket receives only from applications
but not from the network.
Besides the local loopback interface (127.0.0.1 for IPv4, ::1 for IPv6), applications on the same
machine can also use an interface IP address for communication.
4. Outbound IP address:
The source address of outbound packets is either the bound IP address or the address of
the interface over which the packet is sent (if the socket is bound to INADDR_ANY).
N.B.: An outbound packet may also be sent over an interface other than the socket is bound to,
i.e. the routing is based on the IP layer’s routing table.
8/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
4. TCP socket „spawning“
In TCP there exist 2 different socket types: server socket and client socket.
The server socket is used to accept incoming connections. When TCP receives
an incoming connection request on a server socket (SYN) it spawns a new (client) socket
on which the server process can send and receive data (after passing the new socket to a
newly „forked“ server process).
(5) „Forking“ of
Client Server new process Server
AP AP AP
(4) Spawning of
(2) connect(…) (1) accept(…) new TCP client socket
Client: Server:
10/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
5. Socket interface functions (2/2)
UDP Socket Interface Functions:
Client and server have the same socket functions.
There are no functions for connection setup / shutdown since UDP is connectionless.
With one UDP socket it is possible to send to different destination hosts (sendTo() function).
11/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
socket() Function call
6. Socket calls versus TCP segments (1/3) and function
Connection establishment: return
socket()
bind()
listen()
accept()
socket()
Server is blocked
connect() SYN on incoming
requests (listening).
Client SYN ACK
blocked
ACK unblock
return new Server directly services
socket handle new socket (single thread)
or starts a new thread
Rx Buffer
receive() (multithreaded).
Server is blocked
on reading.
12/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
socket() Function call
6. Socket calls versus TCP segments (2/3) and function
Socket send / receive (symmetric for client and server): return
Rx Buffer
receive()
Tx Buffer
send() Rx Buffer
Data Server is blocked
Segment on reading.
ACK
Rx Buffer
unblock
receive
Server handles
the request.
13/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
socket() Function call
6. Socket calls versus TCP segments (3/3) and function
Socket close: return
receive()
Server is blocked
on reading.
close() FIN EOF
ACK
14/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
socket() Function call
7. Socket calls versus UDP datagrams and function
return
socket()
socket() bind()
bind()
receive()
Rx Buffer
send() Datagram
Server is blocked
receive() Rx Buffer on reading.
Rx Buffer
unblock
read
close()
close()
15/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
8. Socket handle
In Unix a socket is like a file descriptor.
Same handling as file (open, close, EOF).
Input stream / output stream to read / write to / from socket (like file).
File: Socket:
16/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
9. Parameter marshalling / RPC transparency (1/4)
Problem:
Different implementations (C/Java, processor architecture, compiler) have different
representations of data. A local data structure sent by application on host 1 may look
differently to application on host 2.
TCP TCP
IP IP
RPC:
Message carrying data structure Remote Procedure Call
17/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
9. Parameter marshalling / RPC transparency (2/4) LSByte Least Significant Byte
MSByte Most Significant Byte
E.g. Endianness:
Endianness is the ordering of bytes of a multibyte data type (integer, long integer etc.).
Network order is the way bytes (and bits) go out to the network. Network order is big endian
(MSByte first).
//the following integer is represented differently on different
//processor architectures / operating systems
int i = 14;
Memory address n n+1 n+2 n+3
int value ‚14‘ in C/C++
0 (MSByte) 0 0 14 (LSByte) on 32Bit big endian machine
App App
Socket Socket
TCP TCP
IP IP
Stubs on the client and server marshal parameters into a standard format and vice versa.
App App
Client / server stubs
client stub are linked between app server stub
and socket and perform
parameter / message
marshalling.
Socket Socket
TCP TCP
IP Marshalled messages IP
between client and server
E.g. IDL/CORBA, Interface Description Language, generates client & server stubs from abstract interface
description. The stubs are then compiled by compiler together with application code.
20/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
10. Low level socket programming (1/2)
Socket Options (SO):
Socket options allow modifying the behavior of sockets.
Generally such options should be used with caution as this makes applications dependent on
the underlying socket layer (violation of layering principle).
21/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
10. Low level socket programming (2/2)
Socket raw interfaces:
A raw socket is directly attached to the network layer without a transport layer
(no TCP, UDP or SCTP layer).
This allows direct access to ICMP (e.g. for traceroute), or IP (e.g. for IPSec).
The raw interface is not available in Java due to security concerns (access to raw interface
requires root access rights since the network stack runs in the kernel space).
App
Socket
Socket
Raw sockets TSAP (Port #)
TCP / UDP / SCTP
NSAP (IP Address)
ICMP IP
Data Link
Physical Link
22/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
11. UDP multicast sockets (1/2)
How does multicasting work? 2. MCGA 224.0.0.1
if0 host1
if0 host2
1.
if1 host3 Host1
4.
if0
MOSPF or Host2
PIM 5.
if1 3.
Stream server
(e.g. audio/video) Host3
4.
3.
1. Hosts join multicast groups by sending IGMP (Internet Group Management Protocol) membership
reports (on multicast address of interest, e.g. 224.0.1.1).
2. Multicast routers keep a table to know on which interface multicast packets are to be sent.
3. Multicast routers send periodic IGMP queries to the multicast hosts to check if they are still member
of the multicast group (again sent on multicast address of interest, e.g. 224.0.1.1).
4. Upon reception of a multicast packet the multicast router performs a lookup (multicast group table with
multicast group addresses MCGA) and sends the packet to all interfaces that have multicast hosts
attached. The packet is sent using the corresponding multicast link address and is thus received by all
multicast hosts.
5. The best (and only) route through the network (no loops etc.) is established with
multicast routing protocols such as MOSPF (Multicast OSPF), PIM (Protocol Independent Multicast) etc.
23/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
11. UDP multicast sockets (2/2)
Multicast is only supported on UDP (TCP is connection-oriented and thus not suitable
for multicast).
Multicast addresses:
Multicast addresses are class D IP addresses in the range 224.0.0.0 to 239.255.255.255.
For example:
224.0.0.9 RIP Version 2
224.0.1.1 Network Time Protocol (NTP)
224.0.0.5 All MOSPF routers
24/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
12. TCP server socket: C/C++ versus Java example
#include <sys/socket.h> import java.net.*;
import java.io.*;
// request[] initialisieren...
26/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
14. IPv6 sockets (1/2):
Most host platforms (Linux, Windows, Sun) already support IPv6.
27/28
© Peter R. Egli 2015 Rev. 4.60
Network Sockets indigoo.com
14. IPv6 sockets (2/2):
Scenarios:
Dual stack: Separate stacks:
Listening Listening Listening
socket on socket on socket on
:: port Socket 0.0.0.0 IPv4 Socket IPv6 Socket :: port
12345 port 12345 12345
The listening socket accepts connections IP4 socket accepts connections only on
to 172.20.92.89 and 172.20.92.89.
fe80::511a:886c:a8cc:dc66 on port 12345.
IPv6 socket accepts connections only on
Windows is dual stack since Windows Vista. fe80::511a:886c:a8cc:dc66.
28/28
© Peter R. Egli 2015 Rev. 4.60