SlideShare una empresa de Scribd logo
1 de 28
Descargar para leer sin conexión
© Peter R. Egli 2015
1/28
Rev. 4.60
Network Sockets indigoo.com
Peter R. Egli
INDIGOO.COM
INTRODUCTION TO NETWORK SOCKET
PROGRAMMING AND CONCEPTS
NETWORK
SOCKETS
© Peter R. Egli 2015
2/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
© Peter R. Egli 2015
3/28
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.
Application Application Application
Socket interface
IP network
Network
stack (OS)
Network
stack (OS)
© Peter R. Egli 2015
4/28
Rev. 4.60
Network Sockets indigoo.com
IP
Socket
TCP
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).
Network Ports
(e.g. Ethernet)
App
OSI Layer 1 (physical)
OSI Layer 2 (data link)
OSI Layer 3 (network)
OSI Layer 4 (transport)
Socket Interface
TSAP (Port #)
NSAP (IP Address)
Socket
Binding to port and
specific IP address
IP layer is router
(between interfaces
and transport layer)
Binding to port and
to inaddr_any
TSAP: Transport Service Access Point
NSAP: Network Service Access Point
© Peter R. Egli 2015
5/28
Rev. 4.60
Network Sockets indigoo.com
DL
PL
IP
Socket
TCP
DL
PL
3. Routing in Network Layers (1/4)
The routing of packets from and to a socket depends on the bind IP address.
7 80
IP1 IP2
4567
IP3 IP4
IP24567IPn9999
App
IP280IPn9999
IP17IPn9999
IP34567IPn9999
1.
1.
2. 2.
Network Ports
(e.g. Ethernet)
Source port
and IP addr.
1.1. 2.
Dest. port
and IP addr.
© Peter R. Egli 2015
6/28
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.
© Peter R. Egli 2015
7/28
Rev. 4.60
Network Sockets indigoo.com
DL
PL
IP
Socket
TCP
DL
PL
3. Routing in Network Layers (3/4)
Localhost binding and routing of outbound packets:
7
IP1 IP2
4567
IP3 IP4
IP24567IPn9999
App
IP17IPn9999
IP34567IPn9999
127.0.0.1
23
App
3.
Outbound packet
Outbound packet
Outbound packet
Dest. port
and IP addr.
Source port
and IP addr. Network Ports
(e.g. Ethernet)
© Peter R. Egli 2015
8/28
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.
© Peter R. Egli 2015
9/28
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).
Client
AP
(2) connect(…)
TCP
Socket = API
TCP
Socket = API
Server
AP
(1) accept(…)
TCP client socket (for sending and receiving data).
(3) TCP 3-way handshake
Full-duplex TCP connection between 2 TCP client sockets
(4) Spawning of
new TCP client socket
Server
AP
(5) „Forking“ of
new process
TCP server socket (for accepting new connections).
© Peter R. Egli 2015
10/28
Rev. 4.60
Network Sockets indigoo.com
5. Socket interface functions (1/2)
 TCP Socket Interface Functions:
Depending on the platform (Java, C, Python ...) client and server sockets may be
implemented differently. In C (BSD sockets, Winsock) there is only 1 socket type
while in Java client and server sockets are represented by different classes.
Client: Server:
socket() Create client socket
connect() Create a connection
send() Send data
receive() Blocking receive data
close() Close client socket
serversocket() Create server socket
bind() Bind server socket to socket
address (IP+port)
listen() Create queues for requests
accept() Block on incoming requests
close() Close server socket
© Peter R. Egli 2015
11/28
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).
Client & Server:
socket() create client / server socket
bind() bind client / server to socket address (IP+port)
send() send data (client and server)
receive() receive data (client and server)
close() close client / server socket
© Peter R. Egli 2015
12/28
Rev. 4.60
Network Sockets indigoo.com
6. Socket calls versus TCP segments (1/3)
 Connection establishment:
TCPTCPClient AP Server APClient Socket Server Socket
SYN
Server is blocked
on incoming
requests (listening).
SYN ACK
ACK
Server directly services
new socket (single thread)
or starts a new thread
(multithreaded).
socket()
bind()
listen()
accept()
unblock
return new
socket handle
socket()
connect()
socket() Function call
and function
return
receive()
Client
blocked
Rx Buffer
Server is blocked
on reading.
© Peter R. Egli 2015
13/28
Rev. 4.60
Network Sockets indigoo.com
TCPTCPClient AP Server APClient Socket Server Socket
6. Socket calls versus TCP segments (2/3)
 Socket send / receive (symmetric for client and server):
socket() Function call
and function
return
send()
Data
Segment
ACK
Tx Buffer
Rx Buffer
receive()
Server is blocked
on reading.
unblock
receive
Rx Buffer
Rx Buffer
Server handles
the request.
© Peter R. Egli 2015
14/28
Rev. 4.60
Network Sockets indigoo.com
TCPTCPClient AP Server APClient Socket Server Socket
6. Socket calls versus TCP segments (3/3)
 Socket close:
socket() Function call
and function
return
close() FIN
ACK
receive()
Server is blocked
on reading.
EOF
Server closes its socket.
close()FIN
ACK
© Peter R. Egli 2015
15/28
Rev. 4.60
Network Sockets indigoo.com
UDPUDPClient AP Server APClient Socket Server Socket
7. Socket calls versus UDP datagrams
socket()
socket()
send()
Rx Buffer
Datagram
Rx Buffer
receive()
socket() Function call
and function
return
bind()
bind()
Server is blocked
on reading.
Server handles
the request.
receive()
send()Datagram
Rx Buffer
Rx Buffer
close()
close()
unblock
read
© Peter R. Egli 2015
16/28
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).
fhdl = fopen(filename,“rw“);
while not (EOF) {
s = gets(fhdl);
}
puts(fhdl,”hello”);
fclose(fhdl);
Socket sock = new Socket(destHostIP,destHostPort);
while not (rx = EOF) {
rx = sock.read();
}
sock.write(“I’m done”);
sock.close
Socket
TCP
File: Socket:
© Peter R. Egli 2015
17/28
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.
Local data
structure
IP
Socket
TCP
App
IP
Socket
TCP
App
Message carrying data structure
Data structure may
look differently
when received by
peer application.
RPC:
Remote Procedure Call
© Peter R. Egli 2015
18/28
Rev. 4.60
Network Sockets indigoo.com
9. Parameter marshalling / RPC transparency (2/4)
 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).
Memory address
0 (MSByte) 0 0 14 (LSByte)
n n+1 n+2 n+3
int value ‚14‘ in C/C++
on 32Bit big endian machine
int value ‚14‘ in C/C++
on 32bit little endian machine0 (MSByte)0014 (LSByte)
int value ‚14‘ in Java
on 32Bit/64Bit big/little endian
machine
0 (MSByte) 0 0 14 (LSByte)
n n+1 n+2 n+3
n n+1 n+2 n+3
//the following integer is represented differently on different
//processor architectures / operating systems
int i = 14;
int value ‚14‘ in C/C++
on 64bit big endian machine
n n+1 n+2 n+3
0 (MSByte) 0 0 0
0 0 0 14 (LSByte)
n+4 n+5 n+6 n+7
int value ‚14‘ in C/C++
on 8Bit big/little endian machine14
n n+1 n+2 n+3
LSByte Least Significant Byte
MSByte Most Significant Byte
© Peter R. Egli 2015
19/28
Rev. 4.60
Network Sockets indigoo.com
IP
Socket
TCP
App
IP
Socket
TCP
App
9. Parameter marshalling / RPC transparency (3/4)
 E.g. complex data structures with references:
Complex data structures contain references to to other data structures or objects.
Such references make only sense on the local machine but not on another host.
Local object with
reference to other
local object.
Message carrying object with
detached reference
Reference broken (points
to non-existent object).
© Peter R. Egli 2015
20/28
Rev. 4.60
Network Sockets indigoo.com
9. Parameter marshalling / RPC transparency (4/4)
 Solution:
When sending parameters over the network it is mandatory to bring them into a
standard ‚canonical‘ format. This is called parameter marshalling (or serialization).
Stubs on the client and server marshal parameters into a standard format and vice versa.
IP
Socket
TCP
App
client stub
IP
Socket
TCP
App
server stub
Client / server stubs
are linked between app
and socket and perform
parameter / message
marshalling.
 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.
Marshalled messages
between client and server
© Peter R. Egli 2015
21/28
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).
Java (1.6) socket option support:
socket.setSoLinger(boolean on, int linger) SO_LINGER: Define time that socket remains active to
send unsent data after close() has been called
(send data in transmit buffer).
socket.setSoTimeout(int timeout) SO_TIMEOUT: Specify a timeout on blocking socket
operations (don‘t block forever).
socket.setTcpNoDelay(boolean on) SO_NODELAY: Enable/disable Nagle‘s algorithm.
socket.setKeepAlive(boolean on) SO_KEEPALIVE: Enable/disable TCP keepalive timer
mechanism.
socket.setReceivedBufferSize(int size) SO_RCVBUF: Set the size of the receive buffer.
socket.setSendBufferSize(int size) SO_SNDBUF: Set the size of the send buffer.
socket.setReuseAddress(boolean on) SO_REUSEADDR: Enable reuse of port number
and IP address so that after a restart an application
can continue using open connections.
C/C++ socket option support:
In C/C++ many more socket options can be set through setsockopt() and getsockopt()
socket API calls.
© Peter R. Egli 2015
22/28
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).
IP
Socket
Data Link
Physical Link
TCP / UDP / SCTP
TSAP (Port #)
NSAP (IP Address)
Socket
ICMP
App
Raw sockets
© Peter R. Egli 2015
23/28
Rev. 4.60
Network Sockets indigoo.com
11. UDP multicast sockets (1/2)
How does multicasting work?
Stream server
(e.g. audio/video)
Host2
Host3
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.
1.
2.
3.
4.
MOSPF or
PIM
Host1
if0
if1
5.
3.
4.
MCGA 224.0.0.1
if0 host1
if0 host2
if1 host3
© Peter R. Egli 2015
24/28
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
 Java multicast socket class:
Class MulticastSocket
MulticastSocket(int port) Creates a multicast socket on specified port.
joinGroup(InetAddress mcastaddr) Join a multicast group.
leaveGroup(InetAddress mcastaddr) Leaves a multicast group (no IGMP report sent,
only for hosts internal bookkeeping).
send() and receive() Inherited methods from DatagramSocket class.
© Peter R. Egli 2015
25/28
Rev. 4.60
Network Sockets indigoo.com
12. TCP server socket: C/C++ versus Java example
#include <sys/socket.h>
int main()
{
struct sockaddr_in serv, cli;
char request[REQUEST], reply[REPLY];
int listenfd, sockfd, n, clilen;
if ((listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
err_sys("socket error");
memset($serv, sizeof(serv), 0);
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = htonl(INADDR_ANY);
serv.sin_port = htons(TCP_SERV_PORT);
if (bind(listenfd, (SA) &serv, sizeof(serv)) < 0)
err_sys("bind error");
if (listen(listenfd, SOMAXCONN) < 0)
err_sys("listen error");
for (;;) {
clilen = sizeof(cli);
if ((sockfd = accept(listenfd, (SA) &cli, &clilen)) < 0)
err_sys("accept error");
if ((n = read_stream(sockfd, request, REQUEST)) < 0)
err_sys("read error");
// n Bytes in request[] verarbeiten, reply[] erzeugen
if (write(sockfd, reply, REPLY) != REPLY)
err_sys("write error");
close(sockfd);
}
}
import java.net.*;
import java.io.*;
public static void main(String[] args)
{
ServerSocket serv;
Socket cli;
PrintStream out;
InputStream in;
try {
serv = new ServerSocket(33333);
} catch(IOException e) { ... }
while(true) {
try {
cli = serv.accept();
} catch(IOException e) { ... }
try {
out = cli.getOutputStream();
in = cli.getInputStream();
String request = in.readln();
// reply erzeugen...
out.println(reply);
cli.close();
} catch (IOException e) { ... }
}
try {
serv.close();
} catch (IOException e) { ... }
}
© Peter R. Egli 2015
26/28
Rev. 4.60
Network Sockets indigoo.com
13. TCP client socket: C/C++ versus Java example
#include <sys/socket.h>
int main(int argc, char *argv[])
{
struct sockaddr_in serv;
char request[REQUEST], reply[REPLY];
int sockfd, n;
// Prüfen der Parameter...
memset(&serv, sizeof(serv), 0);
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = inet_addr(argv[1]);
serv.sin_port = htons(TCP_SERV_PORT);
if (connect(sockfd, (SA) &serv, sizeof(serv)) < 0
err_sys("connect error");
// request[] initialisieren...
if (write(sockfd, request, REQUEST) != REQUEST)
err_sys("write error");
if (shutdown(sockfd, 1) < 0)
err_sys("shutdown error");
if ((n = read_stream(sockfd, reply, REPLY)) < 0)
err_sys("read error");
// n Bytes von reply[] verarbeiten...
exit(0);
}
import java.net.*;
import java.io.*;
public static void main(String[] args)
{
Socket clnt;
PrintStream out;
InputStream in;
try {
clnt = new Socket("localhost", 33333);
} catch(IOException e) { ... }
try {
out = clnt.getOutputStream();
in = clnt.getInputStream();
out.print("Hallo Server!");
String reply = in.readln();
clnt.close();
} catch (IOException e) { ... }
}
© Peter R. Egli 2015
27/28
Rev. 4.60
Network Sockets indigoo.com
14. IPv6 sockets (1/2):
Most host platforms (Linux, Windows, Sun) already support IPv6.
 IPv6 sockets with Java:
 Java supports IPv6 since version 1.4.
 No difference to IPv4 sockets.
 IPv6 automatically enabled when detected.
 No source code change, no bytecode change required for IPv6 as long as the
application does not use numeric IP addresses.
 Java IPv6 API:
java.net.Inet4Address IPv4 address class
java.net.Inet6Address IPv6 address class
java.net.preferIPv4Stack Property to set preference for IPv4.
java.net.preferIPv6Addresses Property to set preference for IPv6.
N.B.: The properties are only accepted as VM arguments on startup of a program.
They can not be changed at runtime.
Example: -Djava.net.preferIPv4Stack=false -Djava.net.preferIPv6Stack=true
© Peter R. Egli 2015
28/28
Rev. 4.60
Network Sockets indigoo.com
14. IPv6 sockets (2/2):
Scenarios:
Dual stack: Separate stacks:
IPv4 IPv6
TCP
Data Link
IPv4 IPv6
TCP
Data Link
Socket IPv4 Socket
TCP
IPv6 Socket
The listening socket accepts connections
to 172.20.92.89 and
fe80::511a:886c:a8cc:dc66 on port 12345.
Windows is dual stack since Windows Vista.
172.20.92.89 fe80::511a:886c:a8cc:dc66 fe80::511a:886c:a8cc:dc66172.20.92.89
Listening
socket on
:: port
12345
Listening
socket on
0.0.0.0
port 12345
IP4 socket accepts connections only on
172.20.92.89.
IPv6 socket accepts connections only on
fe80::511a:886c:a8cc:dc66.
Listening
socket on
:: port
12345

Más contenido relacionado

La actualidad más candente

UDP - User Datagram Protocol
UDP - User Datagram ProtocolUDP - User Datagram Protocol
UDP - User Datagram ProtocolPeter R. Egli
 
Firewall and It's Types
Firewall and It's TypesFirewall and It's Types
Firewall and It's TypesHem Pokhrel
 
E mail protocol - SMTP
E mail protocol - SMTPE mail protocol - SMTP
E mail protocol - SMTPMd Syed Ahamad
 
Transport layer services
Transport layer servicesTransport layer services
Transport layer servicesMelvin Cabatuan
 
SMTP - SIMPLE MAIL TRANSFER PROTOCOL
SMTP - SIMPLE MAIL TRANSFER PROTOCOLSMTP - SIMPLE MAIL TRANSFER PROTOCOL
SMTP - SIMPLE MAIL TRANSFER PROTOCOLVidhu Arora
 
Socket programming in python
Socket programming in pythonSocket programming in python
Socket programming in pythonVignesh Suresh
 
Ch 19 Network-layer protocols Section 1
Ch 19  Network-layer protocols Section 1Ch 19  Network-layer protocols Section 1
Ch 19 Network-layer protocols Section 1Hossam El-Deen Osama
 
Address resolution protocol (ARP)
Address resolution protocol (ARP)Address resolution protocol (ARP)
Address resolution protocol (ARP)NetProtocol Xpert
 
What is Socket Programming in Python | Edureka
What is Socket Programming in Python | EdurekaWhat is Socket Programming in Python | Edureka
What is Socket Programming in Python | EdurekaEdureka!
 
TCP-IP Reference Model
TCP-IP Reference ModelTCP-IP Reference Model
TCP-IP Reference ModelMukesh Tekwani
 
Simple Mail Transfer Protocol
Simple Mail Transfer ProtocolSimple Mail Transfer Protocol
Simple Mail Transfer ProtocolUjjayanta Bhaumik
 
Socket programming in Java (PPTX)
Socket programming in Java (PPTX)Socket programming in Java (PPTX)
Socket programming in Java (PPTX)UC San Diego
 
OSI Transport Layer
OSI Transport LayerOSI Transport Layer
OSI Transport LayerSachii Dosti
 

La actualidad más candente (20)

ARP
ARPARP
ARP
 
UDP - User Datagram Protocol
UDP - User Datagram ProtocolUDP - User Datagram Protocol
UDP - User Datagram Protocol
 
Firewall and It's Types
Firewall and It's TypesFirewall and It's Types
Firewall and It's Types
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
E mail protocol - SMTP
E mail protocol - SMTPE mail protocol - SMTP
E mail protocol - SMTP
 
Transport layer services
Transport layer servicesTransport layer services
Transport layer services
 
SMTP - SIMPLE MAIL TRANSFER PROTOCOL
SMTP - SIMPLE MAIL TRANSFER PROTOCOLSMTP - SIMPLE MAIL TRANSFER PROTOCOL
SMTP - SIMPLE MAIL TRANSFER PROTOCOL
 
IPv4
IPv4IPv4
IPv4
 
Socket programming in python
Socket programming in pythonSocket programming in python
Socket programming in python
 
Ch 19 Network-layer protocols Section 1
Ch 19  Network-layer protocols Section 1Ch 19  Network-layer protocols Section 1
Ch 19 Network-layer protocols Section 1
 
Internet Protocol
Internet ProtocolInternet Protocol
Internet Protocol
 
Address resolution protocol (ARP)
Address resolution protocol (ARP)Address resolution protocol (ARP)
Address resolution protocol (ARP)
 
Socket programming
Socket programmingSocket programming
Socket programming
 
What is Socket Programming in Python | Edureka
What is Socket Programming in Python | EdurekaWhat is Socket Programming in Python | Edureka
What is Socket Programming in Python | Edureka
 
TCP-IP Reference Model
TCP-IP Reference ModelTCP-IP Reference Model
TCP-IP Reference Model
 
IPv4 Addressing
 IPv4 Addressing   IPv4 Addressing
IPv4 Addressing
 
Simple Mail Transfer Protocol
Simple Mail Transfer ProtocolSimple Mail Transfer Protocol
Simple Mail Transfer Protocol
 
Socket programming in Java (PPTX)
Socket programming in Java (PPTX)Socket programming in Java (PPTX)
Socket programming in Java (PPTX)
 
OSI Transport Layer
OSI Transport LayerOSI Transport Layer
OSI Transport Layer
 
Tcp ip
Tcp ipTcp ip
Tcp ip
 

Similar a Network Sockets

Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programmingelliando dias
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagarNitish Nagar
 
CN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjj
CN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjjCN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjj
CN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjjPRADEEPERUKULLA2
 
CN_UNIT4.ppt notre knxckvj bjbDJKVHFL jb
CN_UNIT4.ppt notre knxckvj bjbDJKVHFL jbCN_UNIT4.ppt notre knxckvj bjbDJKVHFL jb
CN_UNIT4.ppt notre knxckvj bjbDJKVHFL jbPRADEEPERUKULLA2
 
Network programming using python
Network programming using pythonNetwork programming using python
Network programming using pythonAli Nezhad
 
Raspberry pi Part 23
Raspberry pi Part 23Raspberry pi Part 23
Raspberry pi Part 23Techvilla
 
Network Programming-Python-13-8-2023.pptx
Network Programming-Python-13-8-2023.pptxNetwork Programming-Python-13-8-2023.pptx
Network Programming-Python-13-8-2023.pptxssuser23035c
 
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونیMohammad Reza Kamalifard
 
Sockets in unix
Sockets in unixSockets in unix
Sockets in unixswtjerin4u
 

Similar a Network Sockets (20)

Basics of sockets
Basics of socketsBasics of sockets
Basics of sockets
 
Os 2
Os 2Os 2
Os 2
 
Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programming
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
CN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjj
CN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjjCN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjj
CN_UNIT4.ppt ytutuim jykhjl fjghkhj gjjj
 
CN_UNIT4.ppt notre knxckvj bjbDJKVHFL jb
CN_UNIT4.ppt notre knxckvj bjbDJKVHFL jbCN_UNIT4.ppt notre knxckvj bjbDJKVHFL jb
CN_UNIT4.ppt notre knxckvj bjbDJKVHFL jb
 
Sockets
Sockets Sockets
Sockets
 
Np unit2
Np unit2Np unit2
Np unit2
 
Sockets
SocketsSockets
Sockets
 
Network programming using python
Network programming using pythonNetwork programming using python
Network programming using python
 
Raspberry pi Part 23
Raspberry pi Part 23Raspberry pi Part 23
Raspberry pi Part 23
 
Network Programming-Python-13-8-2023.pptx
Network Programming-Python-13-8-2023.pptxNetwork Programming-Python-13-8-2023.pptx
Network Programming-Python-13-8-2023.pptx
 
L5-Sockets.pptx
L5-Sockets.pptxL5-Sockets.pptx
L5-Sockets.pptx
 
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی
 
#1 (TCPvs. UDP)
#1 (TCPvs. UDP)#1 (TCPvs. UDP)
#1 (TCPvs. UDP)
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
 
Sockets in unix
Sockets in unixSockets in unix
Sockets in unix
 
Sockets intro
Sockets introSockets intro
Sockets intro
 
Tcp/ip server sockets
Tcp/ip server socketsTcp/ip server sockets
Tcp/ip server sockets
 

Más de Peter R. Egli

LPWAN Technologies for Internet of Things (IoT) and M2M Scenarios
LPWAN Technologies for Internet of Things (IoT) and M2M ScenariosLPWAN Technologies for Internet of Things (IoT) and M2M Scenarios
LPWAN Technologies for Internet of Things (IoT) and M2M ScenariosPeter R. Egli
 
Data Networking Concepts
Data Networking ConceptsData Networking Concepts
Data Networking ConceptsPeter R. Egli
 
Communication middleware
Communication middlewareCommunication middleware
Communication middlewarePeter R. Egli
 
Transaction Processing Monitors (TPM)
Transaction Processing Monitors (TPM)Transaction Processing Monitors (TPM)
Transaction Processing Monitors (TPM)Peter R. Egli
 
Business Process Model and Notation (BPMN)
Business Process Model and Notation (BPMN)Business Process Model and Notation (BPMN)
Business Process Model and Notation (BPMN)Peter R. Egli
 
Microsoft .NET Platform
Microsoft .NET PlatformMicrosoft .NET Platform
Microsoft .NET PlatformPeter R. Egli
 
Overview of Cloud Computing
Overview of Cloud ComputingOverview of Cloud Computing
Overview of Cloud ComputingPeter R. Egli
 
MQTT - MQ Telemetry Transport for Message Queueing
MQTT - MQ Telemetry Transport for Message QueueingMQTT - MQ Telemetry Transport for Message Queueing
MQTT - MQ Telemetry Transport for Message QueueingPeter R. Egli
 
Enterprise Application Integration Technologies
Enterprise Application Integration TechnologiesEnterprise Application Integration Technologies
Enterprise Application Integration TechnologiesPeter R. Egli
 
Overview of Microsoft .Net Remoting technology
Overview of Microsoft .Net Remoting technologyOverview of Microsoft .Net Remoting technology
Overview of Microsoft .Net Remoting technologyPeter R. Egli
 
Android Native Development Kit
Android Native Development KitAndroid Native Development Kit
Android Native Development KitPeter R. Egli
 
Overview of SCTP (Stream Control Transmission Protocol)
Overview of SCTP (Stream Control Transmission Protocol)Overview of SCTP (Stream Control Transmission Protocol)
Overview of SCTP (Stream Control Transmission Protocol)Peter R. Egli
 
Overview of SCTP (Stream Control Transmission Protocol)
Overview of SCTP (Stream Control Transmission Protocol)Overview of SCTP (Stream Control Transmission Protocol)
Overview of SCTP (Stream Control Transmission Protocol)Peter R. Egli
 
Overview of Spanning Tree Protocol (STP & RSTP)
Overview of Spanning Tree Protocol (STP & RSTP)Overview of Spanning Tree Protocol (STP & RSTP)
Overview of Spanning Tree Protocol (STP & RSTP)Peter R. Egli
 
MSMQ - Microsoft Message Queueing
MSMQ - Microsoft Message QueueingMSMQ - Microsoft Message Queueing
MSMQ - Microsoft Message QueueingPeter R. Egli
 
Common Object Request Broker Architecture - CORBA
Common Object Request Broker Architecture - CORBACommon Object Request Broker Architecture - CORBA
Common Object Request Broker Architecture - CORBAPeter R. Egli
 
Component Object Model (COM, DCOM, COM+)
Component Object Model (COM, DCOM, COM+)Component Object Model (COM, DCOM, COM+)
Component Object Model (COM, DCOM, COM+)Peter R. Egli
 
JMS - Java Messaging Service
JMS - Java Messaging ServiceJMS - Java Messaging Service
JMS - Java Messaging ServicePeter R. Egli
 
Web Services (SOAP, WSDL, UDDI)
Web Services (SOAP, WSDL, UDDI)Web Services (SOAP, WSDL, UDDI)
Web Services (SOAP, WSDL, UDDI)Peter R. Egli
 

Más de Peter R. Egli (20)

LPWAN Technologies for Internet of Things (IoT) and M2M Scenarios
LPWAN Technologies for Internet of Things (IoT) and M2M ScenariosLPWAN Technologies for Internet of Things (IoT) and M2M Scenarios
LPWAN Technologies for Internet of Things (IoT) and M2M Scenarios
 
Data Networking Concepts
Data Networking ConceptsData Networking Concepts
Data Networking Concepts
 
Communication middleware
Communication middlewareCommunication middleware
Communication middleware
 
Transaction Processing Monitors (TPM)
Transaction Processing Monitors (TPM)Transaction Processing Monitors (TPM)
Transaction Processing Monitors (TPM)
 
Business Process Model and Notation (BPMN)
Business Process Model and Notation (BPMN)Business Process Model and Notation (BPMN)
Business Process Model and Notation (BPMN)
 
Microsoft .NET Platform
Microsoft .NET PlatformMicrosoft .NET Platform
Microsoft .NET Platform
 
Overview of Cloud Computing
Overview of Cloud ComputingOverview of Cloud Computing
Overview of Cloud Computing
 
MQTT - MQ Telemetry Transport for Message Queueing
MQTT - MQ Telemetry Transport for Message QueueingMQTT - MQ Telemetry Transport for Message Queueing
MQTT - MQ Telemetry Transport for Message Queueing
 
Enterprise Application Integration Technologies
Enterprise Application Integration TechnologiesEnterprise Application Integration Technologies
Enterprise Application Integration Technologies
 
Overview of Microsoft .Net Remoting technology
Overview of Microsoft .Net Remoting technologyOverview of Microsoft .Net Remoting technology
Overview of Microsoft .Net Remoting technology
 
Android Native Development Kit
Android Native Development KitAndroid Native Development Kit
Android Native Development Kit
 
Overview of SCTP (Stream Control Transmission Protocol)
Overview of SCTP (Stream Control Transmission Protocol)Overview of SCTP (Stream Control Transmission Protocol)
Overview of SCTP (Stream Control Transmission Protocol)
 
Overview of SCTP (Stream Control Transmission Protocol)
Overview of SCTP (Stream Control Transmission Protocol)Overview of SCTP (Stream Control Transmission Protocol)
Overview of SCTP (Stream Control Transmission Protocol)
 
Web services
Web servicesWeb services
Web services
 
Overview of Spanning Tree Protocol (STP & RSTP)
Overview of Spanning Tree Protocol (STP & RSTP)Overview of Spanning Tree Protocol (STP & RSTP)
Overview of Spanning Tree Protocol (STP & RSTP)
 
MSMQ - Microsoft Message Queueing
MSMQ - Microsoft Message QueueingMSMQ - Microsoft Message Queueing
MSMQ - Microsoft Message Queueing
 
Common Object Request Broker Architecture - CORBA
Common Object Request Broker Architecture - CORBACommon Object Request Broker Architecture - CORBA
Common Object Request Broker Architecture - CORBA
 
Component Object Model (COM, DCOM, COM+)
Component Object Model (COM, DCOM, COM+)Component Object Model (COM, DCOM, COM+)
Component Object Model (COM, DCOM, COM+)
 
JMS - Java Messaging Service
JMS - Java Messaging ServiceJMS - Java Messaging Service
JMS - Java Messaging Service
 
Web Services (SOAP, WSDL, UDDI)
Web Services (SOAP, WSDL, UDDI)Web Services (SOAP, WSDL, UDDI)
Web Services (SOAP, WSDL, UDDI)
 

Último

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 

Último (20)

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

Network Sockets

  • 1. © Peter R. Egli 2015 1/28 Rev. 4.60 Network Sockets indigoo.com Peter R. Egli INDIGOO.COM INTRODUCTION TO NETWORK SOCKET PROGRAMMING AND CONCEPTS NETWORK SOCKETS
  • 2. © Peter R. Egli 2015 2/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
  • 3. © Peter R. Egli 2015 3/28 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. Application Application Application Socket interface IP network Network stack (OS) Network stack (OS)
  • 4. © Peter R. Egli 2015 4/28 Rev. 4.60 Network Sockets indigoo.com IP Socket TCP 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). Network Ports (e.g. Ethernet) App OSI Layer 1 (physical) OSI Layer 2 (data link) OSI Layer 3 (network) OSI Layer 4 (transport) Socket Interface TSAP (Port #) NSAP (IP Address) Socket Binding to port and specific IP address IP layer is router (between interfaces and transport layer) Binding to port and to inaddr_any TSAP: Transport Service Access Point NSAP: Network Service Access Point
  • 5. © Peter R. Egli 2015 5/28 Rev. 4.60 Network Sockets indigoo.com DL PL IP Socket TCP DL PL 3. Routing in Network Layers (1/4) The routing of packets from and to a socket depends on the bind IP address. 7 80 IP1 IP2 4567 IP3 IP4 IP24567IPn9999 App IP280IPn9999 IP17IPn9999 IP34567IPn9999 1. 1. 2. 2. Network Ports (e.g. Ethernet) Source port and IP addr. 1.1. 2. Dest. port and IP addr.
  • 6. © Peter R. Egli 2015 6/28 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.
  • 7. © Peter R. Egli 2015 7/28 Rev. 4.60 Network Sockets indigoo.com DL PL IP Socket TCP DL PL 3. Routing in Network Layers (3/4) Localhost binding and routing of outbound packets: 7 IP1 IP2 4567 IP3 IP4 IP24567IPn9999 App IP17IPn9999 IP34567IPn9999 127.0.0.1 23 App 3. Outbound packet Outbound packet Outbound packet Dest. port and IP addr. Source port and IP addr. Network Ports (e.g. Ethernet)
  • 8. © Peter R. Egli 2015 8/28 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.
  • 9. © Peter R. Egli 2015 9/28 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). Client AP (2) connect(…) TCP Socket = API TCP Socket = API Server AP (1) accept(…) TCP client socket (for sending and receiving data). (3) TCP 3-way handshake Full-duplex TCP connection between 2 TCP client sockets (4) Spawning of new TCP client socket Server AP (5) „Forking“ of new process TCP server socket (for accepting new connections).
  • 10. © Peter R. Egli 2015 10/28 Rev. 4.60 Network Sockets indigoo.com 5. Socket interface functions (1/2)  TCP Socket Interface Functions: Depending on the platform (Java, C, Python ...) client and server sockets may be implemented differently. In C (BSD sockets, Winsock) there is only 1 socket type while in Java client and server sockets are represented by different classes. Client: Server: socket() Create client socket connect() Create a connection send() Send data receive() Blocking receive data close() Close client socket serversocket() Create server socket bind() Bind server socket to socket address (IP+port) listen() Create queues for requests accept() Block on incoming requests close() Close server socket
  • 11. © Peter R. Egli 2015 11/28 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). Client & Server: socket() create client / server socket bind() bind client / server to socket address (IP+port) send() send data (client and server) receive() receive data (client and server) close() close client / server socket
  • 12. © Peter R. Egli 2015 12/28 Rev. 4.60 Network Sockets indigoo.com 6. Socket calls versus TCP segments (1/3)  Connection establishment: TCPTCPClient AP Server APClient Socket Server Socket SYN Server is blocked on incoming requests (listening). SYN ACK ACK Server directly services new socket (single thread) or starts a new thread (multithreaded). socket() bind() listen() accept() unblock return new socket handle socket() connect() socket() Function call and function return receive() Client blocked Rx Buffer Server is blocked on reading.
  • 13. © Peter R. Egli 2015 13/28 Rev. 4.60 Network Sockets indigoo.com TCPTCPClient AP Server APClient Socket Server Socket 6. Socket calls versus TCP segments (2/3)  Socket send / receive (symmetric for client and server): socket() Function call and function return send() Data Segment ACK Tx Buffer Rx Buffer receive() Server is blocked on reading. unblock receive Rx Buffer Rx Buffer Server handles the request.
  • 14. © Peter R. Egli 2015 14/28 Rev. 4.60 Network Sockets indigoo.com TCPTCPClient AP Server APClient Socket Server Socket 6. Socket calls versus TCP segments (3/3)  Socket close: socket() Function call and function return close() FIN ACK receive() Server is blocked on reading. EOF Server closes its socket. close()FIN ACK
  • 15. © Peter R. Egli 2015 15/28 Rev. 4.60 Network Sockets indigoo.com UDPUDPClient AP Server APClient Socket Server Socket 7. Socket calls versus UDP datagrams socket() socket() send() Rx Buffer Datagram Rx Buffer receive() socket() Function call and function return bind() bind() Server is blocked on reading. Server handles the request. receive() send()Datagram Rx Buffer Rx Buffer close() close() unblock read
  • 16. © Peter R. Egli 2015 16/28 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). fhdl = fopen(filename,“rw“); while not (EOF) { s = gets(fhdl); } puts(fhdl,”hello”); fclose(fhdl); Socket sock = new Socket(destHostIP,destHostPort); while not (rx = EOF) { rx = sock.read(); } sock.write(“I’m done”); sock.close Socket TCP File: Socket:
  • 17. © Peter R. Egli 2015 17/28 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. Local data structure IP Socket TCP App IP Socket TCP App Message carrying data structure Data structure may look differently when received by peer application. RPC: Remote Procedure Call
  • 18. © Peter R. Egli 2015 18/28 Rev. 4.60 Network Sockets indigoo.com 9. Parameter marshalling / RPC transparency (2/4)  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). Memory address 0 (MSByte) 0 0 14 (LSByte) n n+1 n+2 n+3 int value ‚14‘ in C/C++ on 32Bit big endian machine int value ‚14‘ in C/C++ on 32bit little endian machine0 (MSByte)0014 (LSByte) int value ‚14‘ in Java on 32Bit/64Bit big/little endian machine 0 (MSByte) 0 0 14 (LSByte) n n+1 n+2 n+3 n n+1 n+2 n+3 //the following integer is represented differently on different //processor architectures / operating systems int i = 14; int value ‚14‘ in C/C++ on 64bit big endian machine n n+1 n+2 n+3 0 (MSByte) 0 0 0 0 0 0 14 (LSByte) n+4 n+5 n+6 n+7 int value ‚14‘ in C/C++ on 8Bit big/little endian machine14 n n+1 n+2 n+3 LSByte Least Significant Byte MSByte Most Significant Byte
  • 19. © Peter R. Egli 2015 19/28 Rev. 4.60 Network Sockets indigoo.com IP Socket TCP App IP Socket TCP App 9. Parameter marshalling / RPC transparency (3/4)  E.g. complex data structures with references: Complex data structures contain references to to other data structures or objects. Such references make only sense on the local machine but not on another host. Local object with reference to other local object. Message carrying object with detached reference Reference broken (points to non-existent object).
  • 20. © Peter R. Egli 2015 20/28 Rev. 4.60 Network Sockets indigoo.com 9. Parameter marshalling / RPC transparency (4/4)  Solution: When sending parameters over the network it is mandatory to bring them into a standard ‚canonical‘ format. This is called parameter marshalling (or serialization). Stubs on the client and server marshal parameters into a standard format and vice versa. IP Socket TCP App client stub IP Socket TCP App server stub Client / server stubs are linked between app and socket and perform parameter / message marshalling.  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. Marshalled messages between client and server
  • 21. © Peter R. Egli 2015 21/28 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). Java (1.6) socket option support: socket.setSoLinger(boolean on, int linger) SO_LINGER: Define time that socket remains active to send unsent data after close() has been called (send data in transmit buffer). socket.setSoTimeout(int timeout) SO_TIMEOUT: Specify a timeout on blocking socket operations (don‘t block forever). socket.setTcpNoDelay(boolean on) SO_NODELAY: Enable/disable Nagle‘s algorithm. socket.setKeepAlive(boolean on) SO_KEEPALIVE: Enable/disable TCP keepalive timer mechanism. socket.setReceivedBufferSize(int size) SO_RCVBUF: Set the size of the receive buffer. socket.setSendBufferSize(int size) SO_SNDBUF: Set the size of the send buffer. socket.setReuseAddress(boolean on) SO_REUSEADDR: Enable reuse of port number and IP address so that after a restart an application can continue using open connections. C/C++ socket option support: In C/C++ many more socket options can be set through setsockopt() and getsockopt() socket API calls.
  • 22. © Peter R. Egli 2015 22/28 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). IP Socket Data Link Physical Link TCP / UDP / SCTP TSAP (Port #) NSAP (IP Address) Socket ICMP App Raw sockets
  • 23. © Peter R. Egli 2015 23/28 Rev. 4.60 Network Sockets indigoo.com 11. UDP multicast sockets (1/2) How does multicasting work? Stream server (e.g. audio/video) Host2 Host3 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. 1. 2. 3. 4. MOSPF or PIM Host1 if0 if1 5. 3. 4. MCGA 224.0.0.1 if0 host1 if0 host2 if1 host3
  • 24. © Peter R. Egli 2015 24/28 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  Java multicast socket class: Class MulticastSocket MulticastSocket(int port) Creates a multicast socket on specified port. joinGroup(InetAddress mcastaddr) Join a multicast group. leaveGroup(InetAddress mcastaddr) Leaves a multicast group (no IGMP report sent, only for hosts internal bookkeeping). send() and receive() Inherited methods from DatagramSocket class.
  • 25. © Peter R. Egli 2015 25/28 Rev. 4.60 Network Sockets indigoo.com 12. TCP server socket: C/C++ versus Java example #include <sys/socket.h> int main() { struct sockaddr_in serv, cli; char request[REQUEST], reply[REPLY]; int listenfd, sockfd, n, clilen; if ((listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) err_sys("socket error"); memset($serv, sizeof(serv), 0); serv.sin_family = AF_INET; serv.sin_addr.s_addr = htonl(INADDR_ANY); serv.sin_port = htons(TCP_SERV_PORT); if (bind(listenfd, (SA) &serv, sizeof(serv)) < 0) err_sys("bind error"); if (listen(listenfd, SOMAXCONN) < 0) err_sys("listen error"); for (;;) { clilen = sizeof(cli); if ((sockfd = accept(listenfd, (SA) &cli, &clilen)) < 0) err_sys("accept error"); if ((n = read_stream(sockfd, request, REQUEST)) < 0) err_sys("read error"); // n Bytes in request[] verarbeiten, reply[] erzeugen if (write(sockfd, reply, REPLY) != REPLY) err_sys("write error"); close(sockfd); } } import java.net.*; import java.io.*; public static void main(String[] args) { ServerSocket serv; Socket cli; PrintStream out; InputStream in; try { serv = new ServerSocket(33333); } catch(IOException e) { ... } while(true) { try { cli = serv.accept(); } catch(IOException e) { ... } try { out = cli.getOutputStream(); in = cli.getInputStream(); String request = in.readln(); // reply erzeugen... out.println(reply); cli.close(); } catch (IOException e) { ... } } try { serv.close(); } catch (IOException e) { ... } }
  • 26. © Peter R. Egli 2015 26/28 Rev. 4.60 Network Sockets indigoo.com 13. TCP client socket: C/C++ versus Java example #include <sys/socket.h> int main(int argc, char *argv[]) { struct sockaddr_in serv; char request[REQUEST], reply[REPLY]; int sockfd, n; // Prüfen der Parameter... memset(&serv, sizeof(serv), 0); serv.sin_family = AF_INET; serv.sin_addr.s_addr = inet_addr(argv[1]); serv.sin_port = htons(TCP_SERV_PORT); if (connect(sockfd, (SA) &serv, sizeof(serv)) < 0 err_sys("connect error"); // request[] initialisieren... if (write(sockfd, request, REQUEST) != REQUEST) err_sys("write error"); if (shutdown(sockfd, 1) < 0) err_sys("shutdown error"); if ((n = read_stream(sockfd, reply, REPLY)) < 0) err_sys("read error"); // n Bytes von reply[] verarbeiten... exit(0); } import java.net.*; import java.io.*; public static void main(String[] args) { Socket clnt; PrintStream out; InputStream in; try { clnt = new Socket("localhost", 33333); } catch(IOException e) { ... } try { out = clnt.getOutputStream(); in = clnt.getInputStream(); out.print("Hallo Server!"); String reply = in.readln(); clnt.close(); } catch (IOException e) { ... } }
  • 27. © Peter R. Egli 2015 27/28 Rev. 4.60 Network Sockets indigoo.com 14. IPv6 sockets (1/2): Most host platforms (Linux, Windows, Sun) already support IPv6.  IPv6 sockets with Java:  Java supports IPv6 since version 1.4.  No difference to IPv4 sockets.  IPv6 automatically enabled when detected.  No source code change, no bytecode change required for IPv6 as long as the application does not use numeric IP addresses.  Java IPv6 API: java.net.Inet4Address IPv4 address class java.net.Inet6Address IPv6 address class java.net.preferIPv4Stack Property to set preference for IPv4. java.net.preferIPv6Addresses Property to set preference for IPv6. N.B.: The properties are only accepted as VM arguments on startup of a program. They can not be changed at runtime. Example: -Djava.net.preferIPv4Stack=false -Djava.net.preferIPv6Stack=true
  • 28. © Peter R. Egli 2015 28/28 Rev. 4.60 Network Sockets indigoo.com 14. IPv6 sockets (2/2): Scenarios: Dual stack: Separate stacks: IPv4 IPv6 TCP Data Link IPv4 IPv6 TCP Data Link Socket IPv4 Socket TCP IPv6 Socket The listening socket accepts connections to 172.20.92.89 and fe80::511a:886c:a8cc:dc66 on port 12345. Windows is dual stack since Windows Vista. 172.20.92.89 fe80::511a:886c:a8cc:dc66 fe80::511a:886c:a8cc:dc66172.20.92.89 Listening socket on :: port 12345 Listening socket on 0.0.0.0 port 12345 IP4 socket accepts connections only on 172.20.92.89. IPv6 socket accepts connections only on fe80::511a:886c:a8cc:dc66. Listening socket on :: port 12345