Network Programming in Java Internet Protocols (IP, UDP, TCP), B01DY9BNOA, Apr. 6, 2016, by Alan MOUHLI
Network Programming in Java Internet Protocols (IP, UDP, TCP), B01DY9BNOA, Apr. 6, 2016, by Alan MOUHLI
The Java language largely reproduces the syntax of the C ++ language, widely used by
computer. Nevertheless, Java has been stripped of most subtle concepts of C ++ and
simultaneously the most confusing, such as pointers and references, or multiple
inheritance circumvented by implementing interfaces. The designers have favored the
object-oriented approach so that in Java, everything is an object except primitive types
(integer, floating point numbers, etc.).
Java enables the development of client-server applications. On the client side, applets are
responsible for awareness of language. This is especially server-side Java has established
itself in the middle of the company through the servlets, for the server applets, and more
recently the Java Server Pages (JSP) which can substitute for PHP, ASP and ASP.NET.
Java has created an operating system (Java OS) in development environments (Eclipse /
JDK) Virtual Machine (MSJVM (in) JRE) application cross-platform (JVM), a variation
for mobile devices / embedded (J2ME), a graphical interface design library (AWT /
Swing), heavy applications (Jude, Oracle SQL Worksheet, etc.), web technologies
(servlets, applets) and declination for the company (J2EE). Java bytecode portability is
provided by the Java virtual machine, and possibly included in a standard JRE libraries.
This virtual machine can interpret the bytecode or compile it on the fly into machine
language. Portability is dependent on the JVM porting quality on each OS.
Essential components
IP addresses: Inet Address class
Network interfaces: Network Interface class
UDP sockets: Datagram Socket classes and Multicast Socket (UDP packet:
Datagram Packet class)
TCP sockets: Server Socket and Socket classes
application level connections: URL Connection class (to manage such an HTTP
connection)
Compatible locations
Open JDK (reference implementation of JDK) Android APIs
OSI model: it is possible to do (or not) with the standard Java API
Physical
Link: send raw Ethernet frames possible
Network: ICMP connectivity test possible, sending raw IP packets cannot (no
support raw sockets)
Transport: support UDP, TCP, SCTP (unofficial)
Session, presentation: TLS support TCP (SSL Server Socket, SSL Socket) Application:
DNS resolution, HTTP connections …
Note: the model of the last OSI layers is no longer relevant now (tendency to
create new protocols managing both transportation issues, session, presentation and
application such as HTTP / 2.0)
Competition
Lazy method
Lazy method
Name Resolution
DNS records
Key / value pairs for a resource on a DNS server:
1. A: IPv4 address
2. AAAA: IPv6 address
3. CNAME: canonical name (the requested resource is an alias)
4. MX: mail server managing incoming messages
5. SRV: server handling a specific service area
6. NS: Delegation server a DNS zone
7. SOA: information on the authority managing the DNS zone
8. PTR: pointer field to a canonical name used for reverse resolution
9. TXT: various text fields
10. Some fields may exist in multiple copies (multiple IP addresses for load balancing,
mail servers for redundancy)
11. There are other specialized fields used by DNS Extensions (DNSSEC) or other related
protocols (with SPF email authentication, SSH footprint with SSHFP …).
Name Resolution
host name: displays the IP addresses related to name using the system server and DNS
cache
dig @dnsserver name: retrieves the DNS entries from the name server dnsserver
POSIX system call} getaddrinfo () (netdb.h)
Using InetAddress
java.net.NetworkInterface
Sockets
IP stacks implant a socket mechanism (IP address association and communication port
opened on a machine)
Making a connection by sending a packet to a socket to another
On POSIX systems
to open a socket
to bind a socket to an address and port
to send a datagram
to extract a datagram receiving queue
, find out more
Network interfaces
For each active network interface of a machine: at least one @IP
A socket must bind to one or more network interfaces (to focus only on the data from this
interface):
Choosing a network interface by its @IP (e.g. lo 127.0.0.1 for IPv4)
Choice of all network interfaces with the wildcard address (no address)
DatagramSocket
Multicast
Multicast UDP
Some Background on multicast addresses:
224.0.0.0/4 IPv4 (prefix 1110)
FF00 :: 0/8 IPv6 (\ rfc {4291})
Some predefined use addresses (host group, routers, uPnP …)
principle:
Guests subscribe / unsubscribe to a multicast address A. A frame sent to A is addressed
to all members. Advantages:
In theory, pools the packets exchanged on network segments unlike unicast. Avoids
sending packets to all hosts as broadcast (broadcast incompatible with an extensive
network).
multicast protocols
IGMP (Internet Group Management Protocol) to report to the local network gateway
members of each group. Some Layer 2 switches use.
On the Internet, a multicast distribution tree is built with PIM (Protocol Independent
Multicast) in Sparse Mode, Dense Mode, Mixed Sparse-Dense Mode or Source Specific.
A sending window n segments is used: the peer should send at least one acknowledgment
every n segments received
Acknowledgement sent to segment i: i until all segments have been received
Count-down for each sent segment: the expiration, the packet is returned
If too many packets lost: probable congestion and reducing the send window
Problem: a lost packet is not necessarily linked to the congestion links (without son
networks)
Sockets TCP
TCP client socket
Boot a client socket
In Java, TCP socket represented by an instance of We want to listen on the
wildcard address and let the system choose a free port of:
Socket connects to the remote socket addr: port
Socket equivalent to socket
the address of the interface is desired and specify the local port attachment: Socket
We wish not to connect the socket for now:
Then, we must bind the socket with locally bind (its and then connect (with a
time limit if timeout ≠ 0) to the remote socket to
connect ( remote, ) Waves of Socket
To communicate among TCP sockets (local and remote), using binary streams.
stream which read binary data from the corresponding
stream where write data to the corresponding
Some notes (and reminders)
The I / O operations on these waves are blocking.
IOException should be managed.
The scriptures do not necessarily cause the immediate sending of TCP packet
(can buffering)
Flush () is used to request the immediate dispatch (use TCP No Delay)) Close firm
afloat the underlying socket
Socket configuration
regularly sends packets to keep alive the connection
integrates urgent data OutOf-Band in the incoming stream
(otherwise they are ignored)
): Sends an urgent byte (8 bits of low weight)
Still the size of reception buffer
): Sets up a closing blocking confirmation of receipt of the
last data sent or timeout reached
) sets a time limit for the readings exercisable
to disable the Nagle algorithm (useful for interactive protocols:
immediate sending small segments)
): Sets the byte Type-Of-Service for packages (useful for QoS)
allows to reuse a local socket address recently closed (TIME_WAIT
state); must be called before bind.
Note: existence of getters for these methods (for the current configuration)
Closing Socket
allows you to ignore all data subsequently entering the socket $ \
Longrightarrow $ the remote socket is not informed and can continue to send data
closes the outgoing stream of the socket frees resources associated
with the socket
Example: A TCP client that sends a file