Java Learning Lesson 59th-Network Programming overview

Source: Internet
Author: User

Network model

OSI (open System Interconnection) Open Systems Interconnect: Reference Model

Tcp / ip

Network communication elements

IP Address

Port number

Transport protocol

Network Reference Model




Basic concepts of the seven-layer OSI model to understand


Internetwork protocol: Includes IP protocol, ICMP protocol, ARP protocol, RARP protocol. Transport Layer Protocol: TCP protocol, UDP protocol. Application layer protocol: FTP, Telnet, SMTP, HTTP, RIP, NFS, DNS.


To really achieve network communication, first to find the IP address, IP address is a major element of network communication

IP Address: inetaddress

Device identification in the network

Hard to remember, can be used as a native name

Local loopback address: 127.0.0.1, this address, only for accessing native

An IP address is an externally exposed identity that can be accessed by obtaining an IP address


In order to distinguish the identity of the application, it provides some numbers to identify the application, such as 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) ports.

Port on the chassis: is the physical port

Port number

The logical address used to identify the process, and the identity of the different processes

A valid port, 0~65535, where the 0~1024 system uses or retains the port.

That means try to ensure ( < port number <= 65535)

and firewall, is the use of port implementation, such as QQ port number is 4000, firewall settings, port 4000 data, prohibit sending, so that QQ can not connect to the network


IP Object

IP addresses are complex and are encapsulated as objects in Java, and the java.net package provides the objects involved in network transport.

Class InetAddress: This class represents an Internet Protocol (IP) address.

Inetadress class has inet4address,inet6address two categories, so that the IP address, not easy to conflict

exists in the internetwork layer, which can identify IP addresses and encapsulate IP addresses

Inetadress object, no constructor, indicates that there must be a method to return the object, and it is a static method

Simple Demo

public static void Demo () throws Unknownhostexception {//Get local Host IP address object, to throw unknown host exception inetaddress IP = Inetaddress.getlocalhost ()///Get other host IP Address object//IP = Inetaddress.getbyname ("kevy-pc");//can also write IP address IP = Inetaddress.getbyname ("www.baidu.com");//Get Baidu's IP address System.out.println (ip.gethostaddress ());// Get the host IP address System.out.println (Ip.gethostname ());//Get the hostname, if not obtained, explain the resolution, the local area network can not find//may also exist the IP address is not unique, such as Sina, NetEase, server cluster situation// The resulting IP object is an array of//inetaddress[] IPP = Inetaddress.getallbyname ("");}

Domain Name resolution


Normal access to the host, such as access to NetEase's host, NetEase's host, is nothing more than a computer to store some data, the host has an IP address,

Suppose 127.0.0.1, store the network resources of NetEase

Although when visiting the website, is the domain name, but actually is the IP address, http://127.0.0.1, early is the IP address, but the IP address may correspond to the corresponding multiple hosts

So there is www. domain name. com (belongs to the commercial organization). CN (Country category)

IP address and domain name has a corresponding relationship, there are some hosts in the Internet, storage is the corresponding relationship, this is the domain name resolution, and these are DNS (domain name resolution Server), only DNS to record the IP address of netease, DNS IP address back to your computer, then go NetEase


Want to improve the resolution speed, you can implement local resolution, create a local domain name resolution list, the host itself is brought, just let us go to configure,

C:\Windows\System32\drivers\etc

Domain name resolution, the first to go is not the Internet, but this machine, the machine failed again to the Internet, analysis

Therefore, in this way, you can browse some websites, you can block some of the site's spam ads, the ad site's domain name records, configured in the local

127.0.0.1 XXXXX, this will be in the local with their own IP resolution, the website will not open, do not go to the Internet.

360 Blocking URLs is also the principle


Socket (socket)

To learn the transfer protocol, you must first understand the socket

To network transmission there must be two endpoints, the two endpoints want to communicate, you must connect, the endpoint can be understood as the socket

Sockets are a mechanism for network service delivery

Both ends of the communication must have a socket to communicate

Network communication is actually the communication between sockets

Data is transmitted via IO between two sockets


So network programming can also be called socket programming




Java Learning Lesson 59th-Network Programming overview

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.