Java Learning Lesson 59th-Network Programming overview

Source: Internet
Author: User
Tags get ip

Network model

OSI (open System Interconnection) Open Systems Interconnection: 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, just for visiting the machine

An IP address is an externally exposed identity that can be obtained by acquiring an IP address. Interview


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.

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

Valid port,0~65535, in which the 0~1024 system uses or retains the port.

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

And the firewall, is the use of port implementation, for example, QQ port number is 4000, firewall settings. port4000 data, prohibit sending, so that QQ can not connect to the network


IP Object

The IP address is more complex. It is also encapsulated as an object in Java, and the java.net package provides the objects involved in the network transport.

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

There are inet4address under the Inetadress class. Inet6address two major categories. Make the IP address. Conflict-prone

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

The Inetadress object. No constructors, stating that there must be a method to return an object, and that 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 ());// Gets the host IP address System.out.println (Ip.gethostname ());//Gets the hostname, assuming it is not obtained, when the description is resolved. LAN can not find//may also exist IP address is not unique, such as Sina, NetEase, server cluster situation//Get IP object is array//inetaddress[] IPP = Inetaddress.getallbyname ("");}

Domain Name resolution


Normally when visiting the host, for example, visit NetEase's host. NetEase's host. It's just a computer that stores some data. Host has an IP address,

If 127.0.0.1, store the network resources of NetEase

While visiting the site, it is a domain name. But it's actually an IP address. http://127.0.0.1, early IP address, but the IP address may be corresponding to the corresponding multiple hosts

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

The IP address and domain name have a corresponding relationship. There are some hosts in the Internet, which is the corresponding relationship, this is the domain name resolution, and these hosts are DNS (domain name resolution Server). Only DNS to record the IP address of NetEase, DNS to return IP address to your computer, then go NetEase


Want to improve the parsing speed. Local parsing is possible. Create a local domain name resolution list, the host itself comes with, just need us to configure it,

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

So. As a result, you will be able to browse some sites. Be able to block some sites of spam ads, the ad site's domain name records, configured in the local

127.0.0.1 XXXXX. This will be the first in the local with their own IP resolution, the site will not open, do not take 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.