Network Programmingin JavaInternet protocols(IP, UDP, TCP)
By Alan MOUHLIContent
1.Introduction to Java network API
2.Discovering package java.net
3.essential components
4.compatible locations
5.OSI model: it is possible to do (or not) with the standard Java API
6.internal functioning of the Network API
7.Competition
8.multi-threaded approach to blocking
9.non-blocking approach Event
10. Some generalities about IPv4 and IPv6
11. Two IP (Internet Protocol) co-existing on the net
12. IPv4
13. IPv6
14. IPv4 address blocks
15. RFC 2373 IPv6 addresses
16. Some address ranges IPv6 unicast special
17. IP addresses with the API java.net
18. java.net.InetAddress: an IP address in Java
19. What is this mysterious address?
20. Converting an octal IPv4 address text
21. lazy method
22. more laborious method
23. Converting an IPv4 address text octal
24. lazy method
25. more laborious method
26. an IP address connectivity test
27. How it works?
28. If negative responses
29. Name Resolution
30. Domain Name System (DNS)
31. DNS records
32. Name Resolution
33. Using InetAddress
34. IP address from a name
35. Implementation of a DNS resolver in Java
36. Network interfaces in Java
37. java.net.NetworkInterface
38. Get a network interface
39. Viewing the tree of network interfaces
40. UDP datagrams for communication in Java
41. UDP
42. Introducing UDP (User Datagram Protocol)
43. UDP datagram format
44. UDP sockets in Java
45. sockets
46. On POSIX systems
47. Network interfaces
48. DatagramSocket
49. Socket UDP network: DatagramSocket class
50. UDP datagram: DatagramPacket class
51. Characters and bytes
52. Characters to bytes (and vice versa)
53. Send and receive packets with DatagramSocket
54. Send UDP packets containing text
55. Java example: send a packet containing the time an address range
56. Some tips for the exchange of packets
57. Mechanism of pseudo-connection
58. Some tests with Netcat
59. Java example: receive datagrams on a port with a deadline
60. multicast
61. UDP multicast
62. multicast protocols
63. java.net.MulticastSocket
64. TCP communication in Java
65. TCP
66. TCP (Transport Control Protocol)
67. Halfway between UDP and TCP: SCTP (Stream Control Transmission Protocol)
68. TCP Operation: states
69. TCP operation: sending and acknowledgment packets
70. TCP segment format
71. TCP sockets
72. TCP client socket
73. Boot a client socket
74. Waves of Socket
75. Socket configuration
76. Closing Socket
77. Example: A TCP client that sends a file
78. TCP socket server
79. Operating principle of a server socket
80. java.net.ServerSocket
81. Construction
82. connection acceptance
83. iterative model of using ServerSocket
84. Using a service socket
85. Example: a TCP server that receives files
Introduction
The Java language largely reproduces the syntax of the C ++ language, widely usedby computer. Nevertheless, Java has been stripped of most subtle concepts of C++ and simultaneously the most confusing, such as pointers and references, ormultiple inheritance circumvented by implementing interfaces. The designershave favored the object-oriented approach so that in Java, everything is an objectexcept 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-sideJava has established itself in the middle of the company through the servlets, forthe server applets, and more recently the Java Server Pages (JSP) which cansubstitute 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 designlibrary (AWT / Swing), heavy applications (Jude, Oracle SQL Worksheet, etc.), webtechnologies (servlets, applets) and declination for the company (J2EE). Java bytecode portability is provided by the Java virtual machine, and possibly includedin a standard JRE libraries. This virtual machine can interpret the bytecode orcompile it on the fly into machine language. Portability is dependent on the JVMporting quality on each OS.
Discovering package java.net
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 locationsOpen JDK (reference implementation of JDK)Android APIs
OSI model: it is possible to do (ornot) 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)
Internal functioning of the NetworkAPI
Based on calls POSIX systems: getaddrinfo, socket, recv, send, accept ...
native compiled code in conjunction with system calls for each supportedplatform (Windows, Linux, FreeBSD ...)
Interfacing with native code with Java Native Interface (JNI)
The Java API is a wrapper for calls network systems (Java does not implement the UDP, TCP, DNS ... is the core of the system that handles)
Competition
Two approaches to manage multiple network communications.Approach blocking multi thread
A flood of communication = 1 thread
Operations blocking, I / O (hand given back to another thread while blocking)non-blocking approach Event
One threadUsing a selector indicating available communication waves
Operations I / O non-blocking: immediate return if nothing can be read or written
Approach established since Java 1.4 in java.nio
Some generalities about IPv4 andIPv6
Two IP (Internet Protocol) co-existing on the net
IPv4
Historic first version described in RFC 791 in 1981
size of address space 2 ^ {32}
IPv6
New version introduced in 1996 to solve the shortage of IPv4 addressessize of address space 2 ^ {128}
Version in adoption
IPv4 and IPv6 addresses are allocated by the Internet Assigned Numbers Authority (IANA).IPv4 address blocks
5 classes of publicly addressable historical addresses (from the 4 bits of the first byte) with some exceptional beaches:
0 ...: Class A addresses with network $$ 1 $ byte (from 0.0.0.0 to127.255.255.255 ...)
joker address 0.0.0.0: for the socket configuration
10.0.0.0/8 private beach (2 ^ {24} addressable machines)
127.0.0.0/8 beach localhost (local host)
... 10: Class B addresses with network of 2 bytes (128.0.0.0 ... to191,255,255,255)
address range 169.254.0.0/16 local link
172.16.0.0/12 private beach (2 ^ {20} addressable machines)
... 110: Class C addresses with network 3 bytes (192.0.0.0 to 223.255.255.255 to...)
192.168.0.0/16 private beach (2 ^ {16} addressable machines)
1110 ...: D class addresses used for multicast (224.0.0.0 to 239.255.255.255 to...)
... 1111: Class E addresses reserved for future use (from 240.0.0.0 to255.255.255.255 ...)
address 255.255.255.255 reserved for broadcast on a local network
Note: in practice obsolete notions of class (supplanted by CIDR).