Java basics-network programming, java basics Network Programming

Source: Internet
Author: User

Java basics-network programming, java basics Network Programming

I. IP and InetAddress

Java supports the development of network communication programs. It mainly provides two communication protocols: TCP and UDP.

  • Reliable connection and transmission, and third-party handshake
  • Unreliable connection transmission, which is not necessarily acceptable to the receiver during transmission

All network program development classes in Java exist in the java.net package.

IP address Overview

  • IP Address refers to the Internet Protocol Address (English: Internet Protocol Address, also translated as Internet Protocol Address), is the abbreviation of IP Address. An IP address is a Unified IP address format provided by the IP protocol. It allocates a logical address to each network and each host on the Internet to shield the differences between physical addresses.

IP address Representation

  • The IP address is a 32-bit binary number, which is usually divided into four "8-bit binary numbers" (4 bytes ). IP addresses are usually expressed in the form of (a. B. c. d), where, a, B, c, and d are all 0 ~ A decimal integer between 255. For example, enter the IP address 100.4.5.6)

IP address format

  • IP address = network address + host address
  • Network number: used to identify the network where the host is located
  • Host number: used to identify hosts in the Network

The mask function in the IP address mainly distinguishes between the network number and the host number.

IP address category

To facilitate addressing and hierarchical network construction, each IP address consists of two ID codes (ID), namely, the network ID and host ID. All hosts on the same physical network use the same network ID. A host on the network (including Workstation, server, and router) has a host ID corresponding to it. The Internet Committee defines five IP address types to suit networks of different capacities, that is, Class ~ Class E. The, B, and C3 categories (as shown in the following table) are allocated by InternetNIC globally, and Class D and Class E are special addresses.

 

Category Maximum number of networks IP address range Maximum number of hosts Private IP address range
A 126 (2 ^ 7-2) 0.0.0.0-127.255.255.255 16777214 10.0.0.0-10.20.255.255
B 16384 (2 ^ 14) 128.0.0.0-191.20.255 65534 172.16.0.0-172.31.255.255
C 2097152 (2 ^ 21) 192.0.0.0-223.20.255 254 192.168.0.0-192.168.255.255

Special website
  1. The address ("0.0.0.0") with each byte 0 corresponds to the current host;
  2. Each byte in the IP address is 1 IP address ("255.255.255.255") is the broadcast address of the current subnet;
  3. All Class e ip addresses starting with "11110" are reserved for future and lab use.
  4. The IP address cannot start with "127". The numbers 127.0.0.1 to 127.255.255.255 in this type of address are used for loop testing. For example, 127.0.0.1 can represent the IP address of the Local Machine and use "http: // 127.0.0.1 "to test the Web server configured on the local machine.
  5. The first eight-bit Group of the network ID cannot be set to "0", and "0" indicates the local network.

InetAddress

The InetAddress class mainly represents the IP address. This class has two sub-classes: Inet4Address and Inet6Address. One Class is used to represent IPV4, And the other class is used to represent IPV6.

As there are more and more hosts on the network, the IPV4 address is not enough to indicate, and then IPV6 is generated.

The specific method of the InetAddress class can be queried by the API

Local IP address query

Package com. oumyye. network Programming; import java.net. inetAddress; public class InetAddressDemo {public static void main (String args []) throws Exception {// All exceptions throw InetAddress locAdd = null; locAdd = InetAddress. getLocalHost (); // obtain the local System. out. println ("local IP Address:" + locAdd. getHostAddress (); System. out. println ("local availability:" + locAdd. isReachable (5000 ));}};

URL

A unified resource locator is a concise representation of the locations and access methods of resources that can be obtained from the Internet. It is the address of a standard resource on the Internet. Each file on the Internet has a unique URL. It contains information that specifies the location of the file and how the browser should handle it ., You can directly use this class to find resources on the Internet.

Package com. oumyye. network Programming; import java.net. URL; import java. io. inputStream; import java. util. failed; public class URLDemo {public static void main (String args []) throws Exception {// All exceptions throw URL url = new URL ("http", "www.baidu.com", 80, "/"); InputStream input = url. openStream (); // open the input stream consumer scan = new consumer (input); // instantiate consumer class scan. useDelimiter ("\ n"); // you can specify the read separator while (scan. hasNext () {System. out. println (scan. next ());}}};

URLConnection is a general method class for encapsulating remote network resources. It can be used to connect to a remote server and check the attributes of remote resources.

Package com. oumyye. network Programming; import java.net. URL; import java.net. URLConnection; import java. io. inputStream; import java. util. failed; public class URLConnectionDemo {public static void main (String args []) throws Exception {// All exceptions throw URL url = new URL ("http://www.oumyye.com"); URLConnection urlCon = url. openConnection (); // establish the connection System. out. println ("content size:" + urlCon. getContentLength (); System. out. println ("content type:" + urlCon. getContentType ());}};

 

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.