您的位置:首页 > 理论基础 > 计算机网络

网络编程4--毕向东java基础教程视频学习笔记

2017-04-06 15:50 1141 查看
Day24
06 自定义浏览器-Tomcat服务端
07 自定义图形界面浏览器-Tomcat服务端
08 URL-URLConnection
09 小知识点
10 域名解析

 

06 自定义浏览器-Tomcat服务端

客户端:自定义

服务端: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("Connection: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
在Tomcat服务端开启的情况下,运行此程序控制台便能显示服务端发给客户端的信息。

 

 

07 自定义图形界面浏览器-Tomcat服务端

实现图形化界面的浏览器代码如下,程序的关键是对输入的网址进行分割,获得ip地址,端口号和访问路径。





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("转到");
30
31         ta = new TextArea(25,70);
32
33
34         d = new Dialog(f,"提示信息-self",true);
35         d.setBounds(400,200,240,150);
36         d.setLayout(new FlowLayout());
37         lab = new Label();
38         okBut = new Button("确定");
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();
51         f.setVisible(true);
52     }
53     private void  myEvent()
54     {
55
56         okBut.addActionListener(new ActionListener()
57         {
58             public void actionPerformed(ActionEvent e)
59             {
60                 d.setVisible(false);
61             }
62         });
63         d.addWindowListener(new WindowAdapter()
64         {
65             public void windowClosing(WindowEvent e)
66             {
67                 d.setVisible(false);
68             }
69         });
70
71         tf.addKeyListener(new KeyAdapter()
72         {
73             public void keyPressed(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         but.addActionListener(new ActionListener()
89         {
90             public void actionPerformed(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 void windowClosing(WindowEvent e)
107             {
108                 System.exit(0);
109             }
110         });
111     }
112
113     private void showDir()throws Exception
114     {
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(":");
127         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
在Tomcat服务端开启的情况下,在窗口上方的输入网址区输入:http://127.0.0.1:8080/myWeb/Demo.html,再按Enter或者转到,运行显示如下:



08 URL-URLConnection

在07中为了获得有用信息,分割字符串很是麻烦。我们可以使用java.net包中的URL类。

URL
代表一个统一资源定位符,它是指向互联网“资源”的指针。

URL 可选择指定一个“端口”,它是用于建立到远程主机 TCP 连接的端口号。

如果未指定该端口号,则使用协议默认的端口。例如,
http
协议的默认端口为
80


构造方法:

 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 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           获取此 URL 的文件名。
19  String getHost()
20           获取此 URL 的主机名(如果适用)。
21  String getPath()
22           获取此 URL 的路径部分。
23  int getPort()
24           获取此 URL 的端口号。
25  String getProtocol()
26           获取此 URL 的协议名称。
27  String getQuery()
28           获取此 URL 的查询部分。
29
30  运行结果:
31
32 D:\abc>java URLDemo
33 getFile(): /myWeb/Demo.html?name=hahaha&name=20
34 getHost(): 127.0.0.1
35 getPath(): /myWeb/Demo.html
36 getPort(): 8080
37 getProtocol(): http
38 getQuery(): name=hahaha&name=20
39
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 小知识点

1.Socket类中有一个没有参数的构造方法,这个构造方法一般和connect方法一起使用。

void connect(SocketAddress endpoint)

connect方法的参数类型SocketAddress类是一个抽象类,它的直接子类InetSocketAddress和InetAddress类的区别就是:

InetAddress类中只封装ip地址,而InetSocketAddress封装了ip地址和端口号。

 

2.ServerSocket类有一个构造方法:ServerSocket(int port,int backlog),

其中backlog代表对连接请求的最大队列长度,即此服务器最多能连接的用户个数。

 

10 域名解析

1.因为ip地址不容易记忆,上网时常用主机名。

主机名翻译成ip地址,则需要域名解析(DNS)。比如上网时输入www.sina.com.cn,浏览器就先向公网请求DNS服务,

把主机名翻译成相应的ip地址,再把这个地址发回给浏览器。

 

2.http://127.0.0.1:8080/
http://localhost:8080/
上面这两个地址是等价的,其实127和localhost的映射关系就在本机。

在C:\Windows\System32\drivers\etc路径下的host文件,文件中有:

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

也可以修改文件让127.0.0.1对应别的名字。

上网时,先在本机找映射关系,找不到再用DNS。

 

可以利用这个特点避免恶意网站的侵扰。在此文件中把一些恶意网站同127.0.0.1建立映射关系就可以了。

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: