Dark Horse programmer ---- java basics-network programming (1)

Source: Internet
Author: User
Tags ftp file

Dark Horse programmer ---- java basics-network programming (1)

I. Network Reference Model

1. OSI reference model and TCP/IP Reference Model

Layer-7 description:

(1). Physical Layer: mainly defines physical device standards, such as the interface type of the network cable, the interface type of the optical fiber, and the transmission rate of various transmission media. It is mainly used to transmit bit streams.

(2). Data Link Layer: encapsulate and unencapsulate the data received from the physical layer to the IP address of the MAC address Nic. The working device of this layer is a switch, and data is transmitted through the switch.

(3). Network Layer: encapsulate and unencapsulate the data received in the lower layer, for example, 192.168.0.1. This layer works on a router ..

(4 ). transmission Layer: defines some data transmission protocols and port numbers, such as WWW port 80), mainly refers to the data received from the lower layer for segmentation and transmission, to the destination address before restructuring.

(5). Session Layer: Build a data transmission path through the transport layer port number: Transmission port and receiving port. It is mainly used to initiate a session between your systems or receive a session request between devices that need to know each other, either an IP address, a MAC address, or a host name ).

(6 ). presentation Layer: mainly refers to interpreting the received data, encryption and decryption, compression and decompression, that is, converting things that can be recognized by computers into things and sounds that can be recognized by adults ).

(7 ). application Layer: Mainly some terminal applications, such as FTP file downloads), WEBIE browsing), QQ, and so on can be understood as what we can see on the computer screen, is the terminal application ).

Note:

(1). the MAC address of each Nic is globally unique.

(2) The router sends data packets to the specified location.

(3) The process of communication between applications is the process of packaging and unpacking between layers.

(4) the OSI reference model, although well-designed, is too cumbersome and inefficient. Therefore, a simplified TCP/IP reference model is generated.

2. Three elements of network communication: IP address, port number, and transmission protocol.

(1) IP Address: InetAdderss class

1. It is the identifier of the device in the network.

2. It is hard to remember and can be expressed by the host name. There is a ing between the two.

3. The number of IPV4 addresses is not enough, so IPV6 is generated.

4. local loopback address: 127.0.0.1 Host Name: localhost

5. In the absence of an Internet connection, a default IP address, that is, the local loopback address, is assigned to access the local computer for convenience.

(2) Port Number

1. The logical address used to identify the process application) and the identifier of different processes

2. Valid ports: 0 ~ 65535, 0 ~ 1024 the system uses or retains the port.

3. No program has a port number for setting it as an application.

(3) transmission protocol

This is the communication rule. Common protocols include UDP and TCP.

UDP

1. encapsulate data and source and target data packets without establishing a connection

2. The size of each data packet is limited to 64 KB.

3. Because there is no connection, It is an unreliable protocol.

4. No need to establish a connection, fast

Application Case: QQ chat, video chat, walkie talkie, FeiQ, video teaching, and other online videos are all UDP

TCP

1. Establish a connection to form a channel for data transmission

2. transfer large amounts of data in the connection

3. It is a reliable protocol that completes the connection through three handshakes. Three-way handshake: the first request sent by the local party, the second request confirmed by the other party, and the third request confirmed by the other party that the connection was successful.

4. You must establish a connection to reduce the efficiency.

Application Cases: FTP file transmission, thunder download, and telephone

3. Common network methods of java.net. InetAdderss

No constructor. Only methods can be used to return objects of this class.

 
 
  1. Static InetAddress getLocalHost (); // returns an InetAdderss Class Object
  2. Static String getHostName (); // returns the Host Name of the IP address.
  3. Static String getHostAddress (); // returns the IP address String
  4. String toString (); // returns the String representation of this IP address.
  5. Static InetAddress getByName (String host); // obtain the IP address of the specified host name
  6. Static InetAddress [] getAllByName (String host); // obtain all IP addresses of the specified host name

Code Demonstration:

 
 
  1. Import java.net .*;
  2. Class InetAddressDemo
  3. {
  4. Public static void main (String [] args) throws Exception
  5. {
  6. InetAddress ia = InetAddress. getLocalHost (); // returns the InetAddress object.
  7. String name = ia. getHostName (); // obtain the Host name of the IP address
  8. String ip = ia. getHostAddress (); // obtain the local ip Address
  9. System. out. println ("name =" + name );
  10. System. out. println ("ip =" + ip );
  11.  
  12. InetAddress I = InetAddress. getByName ("192.168.1.1"); // obtain the IP address of the specified host.
  13. System. out. println (I. getHostAddress ());
  14.  
  15. InetAddress [] ina = InetAddress. getAllByName ("www.sina.com.cn"); // obtain all IP addresses of the specified host name
  16. For (InetAddress a: ina)
  17. {
  18. System. out. println (a. getHostAddress ());
  19. System. out. println (a. getHostName ());
  20. }
  21. }
  22. }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.