Network Programming 4-video learning notes for bixiangdong java basic tutorial, 4-bixiangdong

Source: Internet
Author: User

Network Programming 4-video learning notes for bixiangdong java basic tutorial, 4-bixiangdong

Day24

06 custom browser-Tomcat server
07 custom graphic interface browser-Tomcat server
08 URL-URLConnection
09 tips
10 domain name resolution

 

06 custom browser-Tomcat server

Client: custom

Server: Tomcat

1 import java.net. *; 2 import java. io. *; 3 class MyIE 4 {5 public static void main (String [] args) throws Exception 6 {7 Socket s = new Socket ("127.0.0.1", 8080 ); 8 PrintWriter out = new PrintWriter (s, getOutputStream (), true); 9 10 out. prinln ("GET/myWeb/Demo.html HTTP/1.1"); 11 out. prinln ("Accept: */*"); 12 out. println ("Accept-Language: zh-cn"); 13 out. println ("Host: 127.0.0.1: 11000"); 14 out. println ("Connecti On: closed "); 15 16 out. println (); 17 out. println (); 18 19 BufferedReader bufr = 20 new BufferedReader (new InputStreamReader (s. getInputStreamReader); 21 String line = null; 22 while (line = bufr. readLine ())! = Null) 23 {24 System. out. println (line); 25} 26 s. close (); 27} 28}View Code

When the Tomcat server is enabled, the console displays the information sent from the server to the client.

 

 

07 custom graphic interface browser-Tomcat server

The browser code for implementing the graphical interface is as follows. The key of the program is to split the entered URL and obtain the IP address, port number, and access path.

1 import java. awt. *; 2 import java. awt. event. *; 3 import java. io. *; 4 import java.net. *; 5 class MyIEByGUI 6 {7 private Frame f; 8 private TextField tf; 9 private Button but; 10 private TextArea ta; 11 12 private Dialog d; 13 private Label lab; 14 private Button okBut; 15 16 17 MyIEByGUI () 18 {19 init (); 20} 21 public void init () 22 {23 f = new Frame ("my window "); 24 f. setBounds (300,100,600,500 ); 25 f. setLayout (new FlowLayout (); 26 27 tf = new TextField (60); 28 29 but = new Button ("go "); 30 31 ta = new TextArea (25, 70); 32 33 34 d = new Dialog (f, "prompt message-self", true); 35 d. setBounds (400,200,240,150); 36 d. setLayout (new FlowLayout (); 37 lab = new Label (); 38 okBut = new Button ("OK"); 39 40 d. add (lab); 41 d. add (okBut); 42 43 44 45 f. add (tf); 46 f. add (but); 47 f. add (ta); 48 49 50 myEvent (); 5 1 f. setVisible (true); 52} 53 private void myEvent () 54 {55 56 okBut. addActionListener (new ActionListener () 57 {58 public void actionreceivmed (ActionEvent e) 59 {60 d. setVisible (false); 61} 62}); 63 d. addWindowListener (new WindowAdapter () 64 {65 public void windowClosing (invalid wevent e) 66 {67 d. setVisible (false); 68} 69}); 70 71 tf. addKeyListener (new KeyAdapter () 72 {73 public void keyPr Essed (KeyEvent e) 74 {75 try 76 {77 if (e. getKeyCode () = KeyEvent. VK_ENTER) 78 showDir (); 79} 80 catch (Exception ex) 81 {82} 83 84} 85}); 86 87 88. addActionListener (new ActionListener () 89 {90 public void actionreceivmed (ActionEvent e) 91 {92 try 93 {94 showDir (); 95} 96 catch (Exception ex) 97 {98} 99 100 101} 102}); 103 104 f. addWindowListener (new WindowAdapter () 105 {106 public v Oid windowClosing (invalid wevent e) 107 {108 System. exit (0); 109} 110}); 111} 112 113 private void showDir () throws Exception114 {115 116 ta. setText (""); 117 String url = tf. getText (); // http: // 192.168.1.254: 8080/myweb/demo.html 118 119 int index1 = url. indexOf ("//") + 2; 120 121 int index2 = url. indexOf ("/", index1); 122 123 124 125 String str = url. substring (index1, index2); 126 String [] arr = str. split (":"); 1 27 String host = arr [0]; 128 int port = Integer. parseInt (arr [1]); 129 130 String path = url. substring (index2); 131 // ta. setText (str + ".... "+ path); 132 133 134 Socket s = new Socket (host, port); 135 136 PrintWriter out = new PrintWriter (s. getOutputStream (), true); 137 138 out. println ("GET" + path + "HTTP/1.1"); 139 out. println ("Accept: */*"); 140 out. println ("Accept-Language: zh-cn"); 141 out. println ("Host: 127. 0.0.1: 11000 "); 142 out. println ("Connection: closed"); 143 144 out. println (); 145 out. println (); 146 147 BufferedReader bufr = new BufferedReader (new InputStreamReader (s. getInputStream (); 148 149 String line = null; 150 151 while (line = bufr. readLine ())! = Null) 152 {153 ta. append (line + "\ r \ n"); 154} 155 156 s. close (); 157 158} 159 160 public static void main (String [] args) 161 {162 new MyIEByGUI (); 163} 164}View Code

When the Tomcat server is enabled, Enter http: // 127.0.0.1: 8080/myWeb/Demo.html in the input URL area at the top of the window, and then press Enter or go to. The running result is as follows:


08 URL-URLConnection

To obtain useful information in 07, It is very troublesome to split strings. We can use the URL class in the java.net package.

ClassURLRepresents a unified resource identifier, which is a pointer to the Internet "resource.

You can specify a port for the URL. It is the port number used to establish a TCP connection to the remote host.

If this port is not specified, the default port of the protocol is used. For example,httpThe default port of the Protocol is80.

Constructor:

URL (String spec ):

URL (String protocl, String host; int port, String file)

1 import java.net. *; 2 class URLDemo 3 {4 public static void main (String [] args) throws MalformedURLException 5 {6 URL = new url ("http: // 127.0.0.1: 8080/myWeb/Demo.html? Name = hahaha & name = 20 "); 7 8 System. out. println ("getFile ():" + url. getFile (); 9 System. out. println ("getHost ():" + url. getHost (); 10 System. out. println ("getPath ():" + url. getPath (); 11 System. out. println ("getPort ():" + url. getPort (); 12 System. out. println ("getProtocol ():" + url. getProtocol (); 13 System. out. println ("getQuery ():" + url. getQuery (); 14} 15} 16/* 17 String getFile () 18 get the file name of this URL. 19 String getHost () 20 get the host name of this URL (if applicable ). 21 String getPath () 22 obtain the path of this URL. 23 int getPort () 24 get the port number of this URL. 25 String getProtocol () 26 get the protocol name of this URL. 27 String getQuery () 28 obtain the query part of this URL. 29 30 running result: 31 32 D: \ abc> java URLDemo33 getFile ():/myWeb/Demo.html? Name = hahaha & name = 2034 getHost (): 127.0.0.135 getPath ():/myWeb/Demo.html 36 getPort (): 808037 getProtocol (): htthwgetquery (): name = hahaha & name = 2039 40 41 42 */View Code 1 import java. io. *; 2 import java.net. *; 3 class URLConnectionDemo 4 {5 public static void main (String [] args) throws Exception 6 {7 URL url = new URL ("http: // 127.0.0.1: 8080/myWeb/Demo.html "); 8 9 URLConnection conn = url. openConnection (); 10 System. out. println (conn); 11 12 InputStream in = conn. getInputStream (); 13 byte [] buf = new byte [1024]; 14 int len = in. read (buf); 15 System. out. println (new String (buf, 0, len); 16} 17}View Code

 

 

09 tips

1. There is a constructor without parameters in the Socket class. This constructor is generally used together with the connect method.

Void connect (SocketAddress endpoint)

The parameter type of the connect method is an abstract class. The difference between InetSocketAddress and InetAddress is:

InetAddress class encapsulates only IP addresses, while InetSocketAddress encapsulates IP addresses and port numbers.

 

2. The ServerSocket class has a constructor: ServerSocket (int port, int backlog ),

The backlog indicates the maximum queue length of the connection request, that is, the maximum number of users that the server can connect.

 

10 domain name resolution

1. Because IP addresses are hard to remember, host names are often used for accessing the Internet.

If the host name is translated into an IP address, domain name resolution (DNS) is required ). For example, if you enter www.sina.com.cn when accessing the internet, the browser first requests the DNS service from the Internet,

Translate the host name into the corresponding IP address and send it back to the browser.

 

2. http: // 127.0.0.1: 8080/

Http: // localhost: 8080/

The above two addresses are equivalent. In fact, the ing between 127 and localhost is on the local machine.

The host file in the path C: \ Windows \ System32 \ drivers \ etc contains:

# Localhost name resolution is handled within DNS itself.
#127.0.0.1 localhost
#: 1 localhost

You can also change the file name to 127.0.0.1.

When surfing the Internet, first find the ing relationship on the local machine, and then use DNS.

 

This feature can be used to prevent malicious website intrusion. In this file, you can set up a ing between some malicious websites and 127.0.0.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.