Java------Socket Series (i)

Source: Internet
Author: User

Network programming


related Basic Concepts :

1. Computer Network and Internet
2. TCP/IP protocol
3. Internet address
IP address, shaped like xxx.xxx.xxx.xxx
Domain Name System. such as www.edu.cn
URL (Uniform Resource Locator)
Protocol://HOST [: Port] [/file] [# Reference]
Client-Server (client-server) mode

network model and communication elements :

Network model
OSI Reference Model
TCP/IP Reference Model
Network communication elements
IP Address
Port number
Transport protocol


☆ Seven Floor Brief:

1. Physical layer: The main definition of physical equipment standards, such as the interface type of network cable, the interface type of optical fiber, the transmission rate of various transmission media. Its main role is to transmit the bitstream (that is, 1, 0 is converted to the current strength to carry out transmission, to the destination and then converted to 1, 0, that is, we often say the digital-to-analog conversion and A/D conversion). This layer of data is called a bit
2. Data Link layer: The main data received from the physical layer to the MAC address (network card address) encapsulation and unpacking. This layer of data is often called a frame. The device that works on this layer is the switch, which transmits the data through the switch.
3. Network layer: The main data received by the lower layers of IP address (example 192.168.0.1) encapsulation and solution encapsulation. The device that works on this layer is the router, which is often called the data packet.
4. Transport Layer: Defines some transfer data protocol and port number (WWW port number 80, etc.), such as: TCP (Transmission Control Protocol, transmission efficiency is low, reliable, for transmission reliability requirements, high data volume data), UDP (User Datagram Protocol, and TCP characteristics, the opposite, For transmission reliability requirements are not high, data volume of small data, such as QQ chat data is transmitted in this way. The main point is to segment and transmit the data received from the lower layer, and then reorganize after reaching the destination address. This layer is often called a paragraph.
5. Session Layer: The path of data transmission is established through the Transport Layer (port number: Transport port and receive port). Primarily initiates a session between your systems or receives a session request (the device needs to know each other can be either IP or Mac or host name)
6. Presentation layer: The main is to carry out the interpretation of the received data, encryption and decryption, compression and decompression, and so on (that is, the computer can recognize something that the adult can recognize things (tablets, sounds, etc.).
7. Application layer: Mainly some terminal applications, such as FTP (various file download), WEB (ie browsing), QQ and so on (can be interpreted as we can see on the computer screen, is the terminal application).

☆ Network communication elements:

IP Address: inetaddress
Identification of devices in the network
Hard to remember, host name available
Local loopback address: 127.0.0.1 host name: localhost
Port number
The logical address used to identify the process, and the identity of the different processes
Valid ports: 0~65535, where the 0~1024 system uses or retains ports.
Transport protocol
Rules of Communication
Common protocols: TCP,UDP

☆ Network resource positioning pointer--url class:

Public final class URL implements serializable{public  URL (string protocol, string host, int port, string file)                                             th Rows malformedurlexception public string  toString ()  //Return full URL address string public string  Getprotocol ()//Return protocol name public  int Getport ()        //return port public  int Getdefaultport ()//return default port public  String gethost ()     // Return host name public  String getFile ()     //Return full file name  //use stream to get URL resource contents Public  final InputStream OpenStream () Throws IOException}
<span style= "font-family:arial, Helvetica, Sans-serif;" > Create:  url url2 = new URL ("http://www.edu.cn");</span>
☆url Communication Link--urlconnection class:
Public abstract class urlconnection{public  URL GetURL ()            //Return the currently connected URL object public  int Getcontentlength ()  //Returns the length of the resource file public  String getContentType ()//returns the type of the resource file public  long getlastmodified ()  // Returns the last modified date of the resource file}
The OpenConnection () method of the URL class can create a URLConnection object

Public URLConnection OpenConnection () throws IOException

☆ Internet Protocol (IP) address--inetaddress class

public class InetAddress implements serializable{public  static inetaddress Getbyname (String host)  public Static InetAddress getbyaddress (String host, byte[] addr) public  static inetaddress getlocalhost ()//return to localhost  public string gethostaddress ()   //Return IP address string public string  gethostname ()     //Return host name}


code Example:
Package Cn.hncu.url;import Java.io.bufferedreader;import Java.io.inputstream;import java.io.InputStreamReader; Import Java.net.inetaddress;import java.net.url;import Java.net.urlconnection;import Java.util.Date;import Org.junit.test;public class Urldemo {@Testpublic void UrlDemo1 () {try {URL url=new url ("http://www.sina.com"); Nputstream In=url.openstream (); BufferedReader br=new BufferedReader (New InputStreamReader (in)); String Line=null;while ((Line=br.readline ())!=null) {System.out.println (line);}} catch (Exception e) {e.printstacktrace ();}} @Testpublic void UrlDemo2 () {try {URL url=new url ("http://www.sina.com:80"); System.out.println (Url.getprotocol ()); System.out.println (Url.gethost ()); System.out.println (Url.getpath ()); System.out.println (Url.getport ());} catch (Exception e) {e.printstacktrace ();}} @Testpublic void Urlconnectiondemo () {try {URL url=new url ("http://www.hncu.net:80"); URLConnection con=url.openconnection (); System.out.println (Con.geturl ()); System.out.println (con.getcontentlength()); System.out.println (Con.getcontenttype ()); System.out.println (New Date (Con.getlastmodified ()));//Here you can output the latest update time for this site} catch (Exception e) {e.printstacktrace ();}} @Testpublic void Inetaddressdemo () {try {inetaddress ip=inetaddress.getbyname ("www.sina.com.cn");//inetaddress ip= Inetaddress.getbyname ("121.14.1.189"); System.out.println (Ip.gethostaddress ()); System.out.println (Ip.gethostname ()); SYSTEM.OUT.PRINTLN (IP);} catch (Exception e) {e.printstacktrace ();}} @Testpublic void InetAddressDemo2 () {try {URL url=new url ("http://www.hncu.net"); InetAddress Ip=inetaddress.getbyname (Url.gethost ()); System.out.println (Ip.gethostaddress ()); System.out.println (Ip.gethostname ());} catch (Exception e) {e.printstacktrace ();}}}


Java------Socket Series (i)

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.