Javase basic review: Network Programming

Source: Internet
Author: User
Tags url example fully qualified domain name

------- Android training, Java training, and hope to communicate with you! ----------

Network Model: OSI reference model, TCP/IP Reference Model; TCP/IP Reference Model:

Application Layer
Transport Layer (TCP/UDP)
Network Layer (IP layer)
Physical Layer and data link layer

Inetaddress class
Public class inetaddresstest {public static void main (string [] ARGs) throws exception {// obtain the corresponding inetaddress instance inetaddress IP = inetaddress Based on the host name. getbyname ("www.baidu.com"); // determines whether the system is accessible. out. println ("Baidu:" + IP. tostring () + ":" + IP. isreachable (5000); // obtain the inetaddress instance's IP string system. out. println (IP. getcanonicalhostname (); system. out. println (IP. gethostname (); system. out. println (IP. gethostaddress (); // obtain the corresponding inetaddress instance based on the original IP address // inetaddress local = inetaddress. getbyaddress (New byte [] {127,0, 0, 1}); inetaddress local = inetaddress. getlocalhost (); system. out. println ("local accessibility:" + local. isreachable (2000) + ":" + local. gethostaddress (); // obtain the Fully Qualified Domain Name System for the inetaddress instance. out. println (local. getcanonicalhostname ());}}

The running result is:
Baidu: www.baidu.com/61.135.169.125: false
61.135.169.125
Www.baidu.com
61.135.169.125
Whether the local machine can be reached: true: 192.168.1.66
Acercn --- // --- whether the local machine can be reached: true: 127.0.0.1 ------- localhost
UDP is not connection-oriented. 1. Sending end

Byte [] Buf = "UDP sender ". getbytes (); datagrampacket dpsent = new datagrampacket (BUF, Buf. length, inetaddress. getbyname ("127.0.0.1"), 30000); datagramsocket DS = new datagramsocket (); DS. sent (dpsent); DS. close ();
2. Acceptor
Datagramsocket DS = new datagramsocket (30000); // the receiver must specify the port byte [] Buf = new byte [4*1024]; datagrampacket dprece = new datagrampacket (BUF, Buf. length); DS. receive (dprece); byte [] bufrece = dprece. getdata (); system. out. println (new string (bufrece ));

Broadcast address: 192.168.1.255
Tcp client and server socket

Client code:

Socket s = new Socket("192.168.1.66",9999);OutputStream out = s.getOutputStream();
Serversocket

Server code:

ServerSocket ss = new ServerSocket(9999);Socket s = ss.accept();InputStream in = s.getInputStream();OutputStream out = s.getOutputStream();
Urldecoder and urlencoder

Application/X-WWW-form-urlencoded MIME format conversion

String key = urlencoder. encode (" Java programmer", "GBK"); // --- % Ba % da % C2 % edjava % B3 % CC % D0 % F2 % D4 % b1string keyword = urldecoder. decode ("% Ba % da % C2 % edjava % B3 % CC % D0 % F2 % D4 % B1", "GBK"); // --- dark horse Java programmer
UEL and Uri

URL: Uniform Resource Identifier
Uri: Uniform Resource Identifier
The URL format consists of the following three parts:

The first part is the protocol or service method httpshttpftp
The second part is that the Host IP address and port number containing the resource are omitted as 80
The third part is the specific address of the host resource. Such as directories and file names.
Separate the first part and the second part with the "//" symbol.
The second part and the third part are separated by the "/" symbol.
The first and second parts are indispensable. The third part can be omitted sometimes.

URL and urlconnection

The URL and Uri can be converted to each other through URL. touri () and Uri. tourl.
The URL class can get a URL instance through the constructor, and provides a series of getxxx methods to obtain information about each part of the resource.
A URL instance can use the URL. openstream () method to open a connection to the URL and return an inputstream used to read from the connection.
The urlconnection instance can be obtained through the URL. openconnection () method, and the urlconnection instance can be used to obtain the input and output of the associated URL.

Urlconnection conn = URL. openconnection (); Conn. setrequestproperty (Key, value); Conn. getinputstream (); Conn. getoutputstream (); Map <string, list <string> map = Conn. getrequestproperties (); // returns an unchangeable map consisting of the general request attributes connected to it. Map <string, list <string> map = conn. getheaderfields (); // The unmodifiable map of the returned header field.
URL example:
Public class urldemo {public static void main (string [] ARGs) throws ioexception {string str_url = "http: // 192.168.1.66: 8080/myweb/1.html "; URL url = new URL (str_url); // system. out. println ("getprotocol:" + URL. getprotocol (); // system. out. println ("gethost:" + URL. gethost (); // system. out. println ("getport:" + URL. getport (); // system. out. println ("GetFile:" + URL. getFile (); // system. out. println ("getpath:" + URL. getp Ath (); // system. out. println ("getquery:" + URL. getquery (); // inputstream in = URL. openstream (); // obtain the URL connector object of the URL object. Urlconnection conn = URL. openconnection (); // string value = Conn. getheaderfield ("Content-Type"); // system. out. println (value); // system. out. println (conn); // output sun.net. www. protocol. HTTP. httpurlconnection: http: // 192.168.1.100: 8080/myweb/1.html inputstream in = Conn. getinputstream (); byte [] Buf = new byte [1024]; int Len = in. read (BUF); string text = new string (BUF, 0, Len); system. out. println (text); In. close ();}}
Client and server principles

The most common client:
Browser: IE.
The most common Server:
Server: tomcat.

The custom server uses the existing client IE to find out what requests the client sends to the server? The sent request is:

The resource path HTTP Protocol version of The get/myweb/1.html HTTP/1.1 request.
Request Message Header. attribute name: Attribute Value
Accept :*/*
Accept-language: ZH-CN, zu; q = 0.5
Accept-encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; sv1; infopath.2)
HOST: 192.168.1.66: 9090
Connection: keep-alive
// Empty rows
// Request body.

// The server sends back the Response Message.

HTTP/1.1 200 OK // response line, HTTP Protocol version response status code response status description
Response Message attributes. Property name: Property Value
Server: APACHE-Coyote/1.1
Etag: W/"199-1323480176984"
Last-modified: sat, 10 Dec 2011 01:22:56 GMT
Content-Type: text/html
Content-Length: 199
Date: Fri, 11 May 2012 07:51:39 GMT
Connection: Close
// Empty rows
// Response body.

Network structure, 1, C/S Client/Server

Features:
The software of this structure must be written by both the client and the server.
High deployment costs and complicated maintenance.
Benefits:
The client can share part of the operation locally.
2, B/S Browser/Server

Features:
The software of this structure is only developed on the server side, but not on the client side, because the client side is directly replaced by the browser.
The development cost is relatively low, and the maintenance is simpler.
Disadvantage: All operations must be completed on the server.

------- Android training, Java training, and hope to communicate with you! ----------

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.