Java---Network programming (1)

Source: Internet
Author: User

Basic concepts related to network programming

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

1. Network model
---osi Reference Model
---TCP/IP Reference Model
2. Network communication elements
---ip Address
---Port number
---Transfer Protocol

☆ Network Model

☆ 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 primary data received by the lower layer is encapsulated and unpacked by IP address (example 192.168.0.1). The device that works on this layer is the router, which is often called the data packet.

4. Transport Layer:
Defines a number of protocols and port numbers (WWW port number 80, etc.) that transmit data, such as TCP (Transmission Control Protocol, low transmission efficiency, strong reliability, high transmission reliability requirements, data volume data), UDP (User Datagram Protocol, contrary to TCP characteristics, for transmission reliability is not high, Data that is small in volume, 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:
A path for 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 interpret the received data, encryption and decryption, compression and decompression, etc. (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 (all kinds of file download), the WEB (ie browsing), QQ and the like (it can be understood 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, available host name
---Local loopback address: 127.0.0.1 host name: localhost
* Port number
---to identify the logical address of the process, the identity of the different processes
---Valid port: 0~65535, where the 0~1024 system uses or retains the port.
* Transfer Protocol
---Rules of communication
---Common protocol: TCP,UDP

☆ Network resource positioning pointer--url class
 Public Final  class URL implements Serializable{   Public URL(String protocol, string host,intPort, String file)throwsMalformedurlexception PublicStringtoString()//Return full URL address string PublicStringGetprotocol()//Return protocol name Public int Getport()//Return port Public int Getdefaultport()//return to the default port PublicStringGetHost()//Return host name PublicStringGetFile()//return full file name//use stream to get URL resource content Public FinalInputStreamOpenStream()throwsIOException}

Test Example:
Created: url url2 = new URL ("http://www.edu.cn");

 PackageCn.hncu.url;ImportJava.io.BufferedReader;ImportJava.io.IOException;ImportJava.io.InputStream;ImportJava.io.InputStreamReader;ImportJava.net.MalformedURLException;ImportJava.net.URL;ImportOrg.junit.Test;/** * URL Simple demo * @author Chen Haoxiang * 2016-5-7 * * Public  class urldemo {    @Test     Public void UrlDemo1(){Try{URL url =NewURL ("Https://chenhaoxiang.github.io"); InputStream in = Url.openstream ();//byte stream to convert to character stream output----encoding is Utf-8BufferedReader br =NewBufferedReader (NewInputStreamReader (In,"Utf-8")); String line=""; while((Line=br.readline ())! =NULL) {System.out.println (line); }        }Catch(Malformedurlexception e)        {E.printstacktrace (); }Catch(IOException e)        {E.printstacktrace (); }    }@Test     Public void UrlDemo2(){Try{URL url =NewURL ("http://www.sina.com:80"); System.out.println (Url.getprotocol ());//Network protocolSystem.out.println (Url.getpath ());//Gets the path portion of this URL (requires the other server permission to access)System.out.println (Url.getport ());//Get Port}Catch(Malformedurlexception e)        {E.printstacktrace (); }    }}
☆url Communication Link--urlconnection class
publicabstractclass URLConnection{  publicgetURL()            //返回当前连接的URL对象  publicintgetContentLength()  //返回资源文件的长度  publicgetContentType() //返回资源文件的类型  publiclonggetLastModified()  //返回资源文件的最后修改日期}

The OpenConnection () method of the URL class can create a URLConnection object
Public URLConnection OpenConnection () throws IOException

@Test Public void URLConnection(){Try{URL url =NewURL ("Http://blog.csdn.net/qq_26525215?viewmode=contents");            URLConnection URLC = Url.openconnection (); System. out. println (Urlc.geturl ());//Returns the URL object for the current connectionSystem. out. println (Urlc.getcontentlength ());//Returns the length of the resource fileSystem. out. println (Urlc.getcontenttype ());//Return resource file typeSystem. out. println (Urlc.getlastmodified ());//Returns the last modified date of the resource file}Catch(Malformedurlexception e)        {E.printstacktrace (); }Catch(IOException e)        {E.printstacktrace (); }    }
☆ 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 local host public  string gethostaddress  ()//Return IP address string public  String gethostname  ()//return host name}  
 @Test public  void  inetaddressdemo  () {try             {inetaddress IP = inetaddress.getbyname ();            //inetaddress IP = inetaddress.getbyname ("111.13.129.32");  System. out . println (Ip.getlocalhost ());            //return to localhost  System. out . println (Ip.gethostaddress ());            //returns the IP address string  System. out . println (Ip.gethostname ()); //return host name www.sina.com/111.13.129.32 } catch  (Unknownhostexception e)        {E.printstacktrace (); }    }
@Test    publicvoidinetAddressDemo2(){        try {            new URL("http://www.hncu.net:80");            InetAddress ip = InetAddress.getByName(url.getHost());            System.out.println(ip.getHostAddress());            System.out.println(ip.getHostName());        catch (Exception e) {            e.printStackTrace();        }    }

Java---Network programming (1)

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.