Java Network Programming One: A detailed understanding of the basics

Source: Internet
Author: User

Network Fundamentals 1, OSI layered model and TCP/IP layered model correspondence relation

In this case, the 7-tier model is not expanded, only the knowledge points related to this series of topics are selected.

2, seven layer model and the corresponding relationship between the protocol

Network layer------------IP (interconnection protocol between networks)

Transport Layer------------TCP (Transmission Control Protocol), UDP (User datagram protocols)

Application tier------------Telnet (standard protocol and primary method for Internet Telnet service), FTP (Text Transfer Protocol), HTTP (Hypertext Transfer Protocol)

3. IP address and port number

1, the IP address is used to uniquely identify a communication entity in the network, the communication entity can be a host, can be a printer, or a router port. The packets that are transmitted in the IP-protocol network must be marked with an IP address. The IP address is like writing a letter, you must specify the recipient's address. Each transmitted packet includes a source IP and a destination IP.

2, the IP address uniquely identifies the communication entity, but a communication entity can have multiple communication programs to provide network services. This is the time to distinguish between specific communication programs by port. A communication entity cannot have two communication programs that use the same port number.

IP Address and port number, like a trip to the field to stay in the hotel, the IP address indicates the location of the hotel, and the port number indicates the person's room number in the hotel.

4. TCP and UDP

1, TCP is a connection-oriented guarantee reliable transmission protocol. With the TCP protocol transmission, a sequential error-free data stream is obtained. It can provide reliable data flow between two computers, and applications such as HTTP, FTP, Telnet need this reliable communication channel.

2, UDP is a non-connected protocol, each datagram is a separate information, including the full source address or destination address, it on the network with any possible path to the destination, as to reach the destination, the time to reach the destination and the correctness of the content is not guaranteed.

Given the TCP protocol that guarantees reliable transmission, why not transmit the UDP protocol reliably? There are two reasons:

1, reliable transmission is to pay the price, the correctness of the data content of the test will inevitably occupy the computer processing time and network bandwidth. Therefore, TCP transmission efficiency is not as high as UDP.

2, many applications do not need to ensure strict transmission reliability, such as video conferencing system, does not require video audio data is absolutely correct, as long as it can be coherent. So in these scenarios, it's more appropriate to use UDP.

5. URL access to online resources

1, the URL object represents the Uniform Resource Locator, is a pointer to the Internet "resources". It is made up of protocol names, hosts, ports, and resources that meet the following formats:

Protocol://host:port/resourcename

http://www.crazyit.org/index.php

2. Some methods of URL object can access the resources of the URL:

String getFile (): Gets the resource name of the URL

String gethost (): Get host Name

String GetPath (): Get path part

int Getport (): Get port number

1  Public Static voidMain (string[] args)throwsIOException2     {3URL url =NewURL ("Https://i.cnblogs.com/EditPosts.aspx?opt=1");4String file =url.getfile ();5 System.out.println (file);6String host =url.gethost ();7 System.out.println (host);8         intPort =Url.getport ();9 System.out.println (port);TenString query =url.getquery (); One System.out.println (query); AString protocol =Url.getprotocol (); - System.out.println (protocol); -}

Execution Result:

1 /editposts.aspx?opt=12i.cnblogs.com3 -14 opt=15 HTTPS

The most common methods of URL objects are shown above, and the two most important methods of URLs are as follows:openConnection()、openStream()

1  Public Static voidMain (string[] args)throwsIOException2     {3URL url =NewURL ("http://www.baidu.com");4URLConnection connection =url.openconnection ();5InputStream is =Connection.getinputstream ();6OutputStream OS =NewFileOutputStream ("C:/data.txt");7         byte[] buffer =New byte[1024];8         intFlag = 0;9          while( -1! = (flag = is.read (buffer, 0, Buffer.length )))Ten         { OneOs.write (buffer, 0, flag); A         } - os.close (); - is.close (); the}

Execution Result:

1, the Code line 4thopenConnection()该方法用于返回URLConnection对象,表示到URL所引用的远程连接。

2, the Code line 5th getInputStream method, to return the input stream read from this open connection.

3, the following code is we are already familiar with reading from the input stream to the data, and then through the output stream to write to the file.

4, the implementation of the result is that we access through the browser http://www.baidu.com, Baidu server returned content.

5, here is a text file, we will change the suffix to HTML, and then use the browser to access, so it looks more intuitive:

6, InetAddress

Java provides the InetAddress class to represent an IP address.

1  Public Static void throws IOException 2     {3         inetaddress address = inetaddress.getlocalhost (); 4         System.out.println (address); 5         Address = Inetaddress.getbyname ("www.baidu.com"); 6         System.out.println (address); 7         8     }

Execution Result:

1 windows-ec813qq/192.168.88.12 www.baidu.com/115.239.211.112

Java Network Programming One: A detailed understanding of the basics

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.