I. Overview of network programming
Overview:
Java is a language on the Internet that provides support for Web applications from a language level, and programmers can easily develop common Web applications.
Java provides the Network class library, can achieve painless network connectivity, the underlying details of the network is hidden in Java's native installation system, controlled by the JVM. And Java implements a cross-platform network library, the programmer is faced with a unified network programming environment.
Basis:
The purpose of network programming:
The purpose of network programming is to communicate with other computers directly or indirectly through network protocols.
The main problems:
How to accurately locate one or more hosts on a network
How to reliably and efficiently transfer data after locating the host.
Transmission of data requires compliance with certain protocols and is widely used in TCP/IP protocol
Protocol Model:
Communication elements:
IP Address: inetaddress
The unique identity of the device in the network; local loopback address (localhost): 127.0.0.1; It is not easy to remember, easily remember is the domain name:
The domain name is easy to remember, and when you enter a host domain name when you connect to the network, the Domain name server (DNS) is responsible for converting the domain name to an IP address so that you can connect with the host.
Ports: Port
In order to differentiate the identity of the application, some numbers are provided to identify the application. For example, QQ with 4000来 logo, my Computer QQ message, sent to the IP address is: 192.168.0.1 number 4000 on the application. And these numbers are the (logical) port.
The port number is limited to 0-65536, for example the common MySQL (3306), HTTP (80), generally we should use the port communication after 1024 to avoid communication port conflict;
A network socket is derived from the combination of the port number and IP address.
Second, the creation and use of InetAddress class
inetaddress class is used to encapsulate the digital IP address and the domain name of the address we discussed earlier.
1. Instantiation
Access via Getlocalhost ()-native, Getbyname (), and Getallbyname () three static methods
2. Two methods:
String gethostname (): Gets the domain name of the InetAddress object;
String gethostaddress (): Gets the IP address of the InetAddress object;
Instance:
Public Static void throws unknownhostexception { = inetaddress.getbyname ("www.atguigu.com"); System.out.println ("address =" + address); System.out.println (Address.gethostname ()); System.out.println (Address.gethostaddress ()); }
Third, network communication protocol
1. Network Communication Protocol
Specified criteria for rate, transmission code, code structure, transfer control steps, error control, etc.
Java Basics-Network programming