黑马程序员_Java基础_网络编程相关小项目

来源:互联网 发布:晋江腾达陶瓷销售网络 编辑:程序博客网 时间:2024/05/22 02:14

一、网络编程(TCP-上传图片)

[java] view plaincopy
  1. /* 
  2. 需求:上传图片。 
  3. */  
  4.   
  5. /* 
  6. 客户端: 
  7. 1.服务端点。 
  8. 2.读取客户端已有的图片数据。 
  9. 3.通过socket输出流将数据发给服务端。 
  10. 4.读取服务端反馈信息。 
  11. 5.关闭。 
  12. */  
  13. import java.net.*;  
  14. import java.io.*;  
  15. class PicClient  
  16. {  
  17.     public static void main(String[] args)throws Exception   
  18.     {  
  19.         Socket s=new Socket("172.16.56.237",1005);  
  20.         FileInputStream fis=new FileInputStream("e:\\1.jpg");  
  21.   
  22.         OutputStream out=s.getOutputStream();  
  23.   
  24.         byte[] buf=new byte[1024];  
  25.         int len=0;  
  26.   
  27.         while((len=fis.read(buf))!=-1)  
  28.         {  
  29.             out.write(buf,0,len);  
  30.         }  
  31.           
  32.         s.shutdownOutput();//告诉服务端数据已写完。(增加结束标记)  
  33.   
  34.         InputStream in=s.getInputStream();  
  35.         byte[] bufIn=new byte[1024];  
  36.   
  37.         int num=in.read(bufIn);  
  38.         System.out.println(new String(bufIn,0,num));  
  39.   
  40.         fis.close();  
  41.         s.close();  
  42.     }  
  43. }  



[java] view plaincopy
  1. /* 
  2. 服务端: 
  3. */  
  4. import java.net.*;  
  5. import java.io.*;  
  6. class PicServer  
  7. {  
  8.     public static void main(String[] args)throws Exception  
  9.     {  
  10.         ServerSocket ss=new ServerSocket(1005);  
  11.         Socket s=ss.accept();  
  12.   
  13.         InputStream in=s.getInputStream();  
  14.   
  15.         FileOutputStream fos=new FileOutputStream("f:\\server.jpg");  
  16.   
  17.         byte[] buf=new byte[1024];  
  18.         int len=0;  
  19.         while((len=in.read(buf))!=-1)  
  20.         {  
  21.             fos.write(buf,0,len);  
  22.         }  
  23.   
  24.         OutputStream out=s.getOutputStream();  
  25.         out.write("图片已收到".getBytes());  
  26.   
  27.         fos.close();  
  28.         s.close();  
  29.         ss.close();  
  30.     }  
  31. }  


二、网络编程(TCP-客户端并发上传图片)


[java] view plaincopy
  1. /* 
  2. 需求:上传图片。 
  3. */  
  4.   
  5. /* 
  6. 客户端: 
  7. 1.服务端点。 
  8. 2.读取客户端已有的图片数据。 
  9. 3.通过socket输出流将数据发给服务端。 
  10. 4.读取服务端反馈信息。 
  11. 5.关闭。 
  12. */  
  13. import java.net.*;  
  14. import java.io.*;  
  15. class PicClient  
  16. {  
  17.     public static void main(String[] args)throws Exception   
  18.     {     
  19.         if(args.length!=1)  
  20.         {  
  21.             System.out.println("请选择一个jpg格式的图片。");  
  22.             return;  
  23.         }  
  24.   
  25.         File file=new File(args[0]);  
  26.         if(!(file.exists()&&file.isFile()))  
  27.         {  
  28.             System.out.println("该文件有问题,要么存在,要么不是文件");  
  29.             return;  
  30.         }  
  31.         if(!file.getName().endsWith(".jpg"))  
  32.         {  
  33.             System.out.println("图片格式错误,请重新选择");  
  34.             return;  
  35.         }  
  36.         if(file.length()>1024*1024*5)  
  37.         {  
  38.             System.out.println("文件过大,禁止上传!");  
  39.             return;  
  40.         }  
  41.   
  42.   
  43.         Socket s=new Socket("172.16.56.237",1005);  
  44.         FileInputStream fis=new FileInputStream(file);  
  45.   
  46.         OutputStream out=s.getOutputStream();  
  47.   
  48.         byte[] buf=new byte[1024];  
  49.         int len=0;  
  50.   
  51.         while((len=fis.read(buf))!=-1)  
  52.         {  
  53.             out.write(buf,0,len);  
  54.         }  
  55.           
  56.         s.shutdownOutput();//告诉服务端数据已写完。(增加结束标记)  
  57.   
  58.         InputStream in=s.getInputStream();  
  59.         byte[] bufIn=new byte[1024];  
  60.   
  61.         int num=in.read(bufIn);  
  62.         System.out.println(new String(bufIn,0,num));  
  63.   
  64.         fis.close();  
  65.         s.close();  
  66.     }  
  67. }  



[java] view plaincopy
  1. /* 
  2. 服务端: 
  3. 当A客户端连接上后,被服务端获取到后,服务端就在执行具体流程。这时B客户端连接的话,只能等待。 
  4. 因为服务端还没有处理完A客户端的请求,还没有循环结束回来执行下一次accept方法,所以暂时获取不到 
  5. B客户端对象。 
  6.  
  7. 所以,为了可以让多个客户端同时并发访问服务端,那么服务端最好就是将每个客户端封装到一个 
  8. 单独的线程中,这样就可以同时处理多个客户端请求。 
  9.  
  10. 如何定义线程呢? 
  11. 只要明确了每一个客户端要在服务端执行的代码即可,将该代码存入run方法中。 
  12. */  
  13. import java.net.*;  
  14. import java.io.*;  
  15. class PicThread implements Runnable  
  16. {  
  17.     private Socket s;  
  18.     PicThread(Socket s)  
  19.     {  
  20.         this.s=s;  
  21.     }  
  22.     public void run()  
  23.     {  
  24.         int count=1;  
  25.   
  26.         String ip=s.getInetAddress().getHostAddress();  
  27.         try  
  28.         {  
  29.               
  30.             System.out.println(ip+"..........connceted");  
  31.   
  32.             InputStream in=s.getInputStream();  
  33.   
  34.   
  35.             //为了不覆盖  
  36.             File file=new File("f:\\"+ip+"("+(count)+")"+".jpg");  
  37.             while(file.exists())  
  38.                 file=new File("f:\\"+ip+"("+(count++)+")"+".jpg");  
  39.   
  40.   
  41.   
  42.             FileOutputStream fos=new FileOutputStream(file);  
  43.   
  44.             byte[] buf=new byte[1024];  
  45.             int len=0;  
  46.             while((len=in.read(buf))!=-1)  
  47.             {  
  48.                 fos.write(buf,0,len);  
  49.             }  
  50.   
  51.             OutputStream out=s.getOutputStream();  
  52.             out.write("图片已收到".getBytes());  
  53.   
  54.             fos.close();  
  55.             s.close();  
  56.               
  57.         }  
  58.         catch (Exception e)  
  59.         {  
  60.             throw new RuntimeException(ip+"上传失败!");  
  61.         }  
  62.     }  
  63. }  
  64. class PicServer  
  65. {  
  66.     public static void main(String[] args)throws Exception  
  67.     {  
  68.         ServerSocket ss=new ServerSocket(1005);  
  69.   
  70.         while(true)  
  71.         {  
  72.             Socket s=ss.accept();  
  73.             new Thread(new PicThread(s)).start();  
  74.         }  
  75.           
  76.     }  
  77. }  


三、网络编程(TCP-客户端并发登录)

[java] view plaincopy
  1. /* 
  2. 客户端通过键盘录入用户名,服务端对这个用户名进行校验。 
  3. 如果该用户存在,在服务端显示xxx,已登录,并在客户端显示xxx,欢迎登录。 
  4. 如果该用户存在,在服务端显示xxx,尝试登录。并在客户端显示xxx,该用户不存在。 
  5.  
  6. 最多登录3次。 
  7. */  
  8. import java.io.*;  
  9. import java.net.*;  
  10.   
  11. class LoginClient  
  12. {  
  13.     public static void main(String[] args)throws Exception   
  14.     {  
  15.         Socket s=new Socket("172.16.56.237",1005);  
  16.   
  17.         BufferedReader bufr=new BufferedReader(new InputStreamReader(System.in));  
  18.   
  19.         PrintWriter out=new PrintWriter(s.getOutputStream(),true);  
  20.   
  21.         BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));  
  22.   
  23.         for(int i=0;i<3;i++)  
  24.         {  
  25.             String line=bufr.readLine();  
  26.             if(line==null)  
  27.                 break;  
  28.             out.println(line);  
  29.   
  30.             String info=bufIn.readLine();  
  31.             System.out.println("info:"+info);  
  32.             if(info.contains("欢迎"))  
  33.                 break;  
  34.         }  
  35.   
  36.         bufr.close();  
  37.         s.close();  
  38.     }  
  39. }  



[java] view plaincopy
  1. /* 
  2. 服务端 
  3. */  
  4. import java.io.*;  
  5. import java.net.*;  
  6. class UserThread implements Runnable  
  7. {  
  8.     private Socket s;  
  9.     UserThread(Socket s)  
  10.     {  
  11.         this.s=s;  
  12.     }  
  13.     public void run()  
  14.     {  
  15.         String ip=s.getInetAddress().getHostAddress();  
  16.         try  
  17.         {  
  18.             for(int i=0;i<3;i++)  
  19.             {  
  20.                 BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));  
  21.                 String name=bufIn.readLine();  
  22.                 if(name==null)  
  23.                     return;  
  24.                 BufferedReader bufr=new BufferedReader(new FileReader("e:\\user.txt"));  
  25.   
  26.                 PrintWriter out=new PrintWriter(s.getOutputStream(),true);  
  27.   
  28.                 String line=null;  
  29.   
  30.                 boolean flag=false;  
  31.                 while((line=bufr.readLine())!=null)  
  32.                 {  
  33.                     if(line.equals(name))  
  34.                     {  
  35.                         flag=true;  
  36.                         break;  
  37.                     }  
  38.                 }  
  39.                 if(flag)  
  40.                 {  
  41.                     System.out.println(name+",已登录");  
  42.                     out.println(name+",欢迎光临。");  
  43.                 }  
  44.                 else  
  45.                 {  
  46.                     System.out.println(name+",尝试登录。");  
  47.                     out.println(name+",用户名不存在。");  
  48.                 }  
  49.             }  
  50.   
  51.             s.close();  
  52.         }  
  53.         catch (Exception e)  
  54.         {  
  55.             throw new RuntimeException(ip+"校验失败");  
  56.         }  
  57.     }  
  58. }  
  59.   
  60. class LoginServer    
  61. {  
  62.     public static void main(String[] args)throws Exception   
  63.     {  
  64.         ServerSocket ss=new ServerSocket(1005);  
  65.   
  66.         while(true)  
  67.         {  
  68.             Socket s=ss.accept();  
  69.   
  70.             new Thread(new UserThread(s)).start();  
  71.         }  
  72.     }  
  73. }  


四、网络编程(浏览器客户端-自定义服务端)

[java] view plaincopy
  1. /* 
  2. 演示客户端和服务端。 
  3.  
  4. 1. 
  5. 客户端:浏览器 
  6. 服务端:自定义   
  7.  
  8. */  
  9. import java.net.*;  
  10. import java.io.*;  
  11. class ServerDemo   
  12. {  
  13.     public static void main(String[] args)throws Exception   
  14.     {  
  15.         ServerSocket ss=new ServerSocket(1005);  
  16.         Socket s=ss.accept();  
  17.   
  18.         System.out.println(s.getInetAddress().getHostAddress());  
  19.   
  20.         PrintWriter out=new PrintWriter(s.getOutputStream(),true);  
  21.   
  22.         out.println("客户端你好");//输出HTML文本  
  23.   
  24.         s.close();  
  25.   
  26.         ss.close();  
  27.     }  
  28. }  


五、网络编程(浏览器客户端-Tomcat服务端)

1.下载好Tomcat服务端,解压到任意路径。

2.进入Tomcat文件夹的bin目录下,点击startup.bat文件,启动Tomcat服务器。

可以看到配置信息里面的服务器端口:8080

3.进入浏览器输入:    http://172.16.56.237:8080/ 打开了Tomcat网页。


这样的话,自己写个HMTL

[html] view plaincopy
  1. <html>  
  2.     <body>  
  3.         <h1>  
  4.         这是我的第一个网页  
  5.         <h1>  
  6.         <font size=5 color=red>欢迎光临</font>  
  7.         <div>  
  8.         1234567890</br>  
  9.         3456789090</br>  
  10.         5678987654</br>  
  11.         </div>  
  12.     </body>  
  13. </html>  


保存成index.html文件。然后在Tomcat文件夹的webapps下建立一个myweb文件夹,将index.html放入。

在浏览器中指定具体服务器要打开html文件的路径:  

http://172.16.56.237:8080//myweb/index.html

六、网络编程(自定义浏览器-Tomcat服务端)


[java] view plaincopy
  1. /* 
  2. 客户端:浏览器 
  3. 服务端:Tomcat服务器   
  4.  
  5. */  
  6. import java.net.*;  
  7. import java.io.*;  
  8. class ServerDemo   
  9. {  
  10.     public static void main(String[] args)throws Exception   
  11.     {  
  12.         ServerSocket ss=new ServerSocket(1005);  
  13.         Socket s=ss.accept();  
  14.   
  15.         System.out.println(s.getInetAddress().getHostAddress());  
  16.   
  17.         InputStream in=s.getInputStream();  
  18.         byte[] buf=new byte[1024];  
  19.         int len=in.read(buf);  
  20.   
  21.         System.out.println(new String(buf,0,len));  
  22.   
  23.         PrintWriter out=new PrintWriter(s.getOutputStream(),true);  
  24.   
  25.         out.println("<font color='red' sixe'10'>客户端你好</font>");//输出HTML文本  
  26.   
  27.         s.close();  
  28.   
  29.         ss.close();  
  30.     }  
  31. }  


运行自定义服务器后,在浏览器输入地址:http://172.16.56.237:1005/ 

172.16.56.237

GET / HTTP/1.1

Host: 172.16.56.237:1005

Connection: keep-alive

User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like G

ecko) Chrome/21.0.1180.83 Safari/537.1

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Encoding: gzip,deflate,sdch

Accept-Language: zh-CN,zh;q=0.8

Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3

这些都是浏览器向服务器发送请求的信息。包含浏览器的信息。


[java] view plaincopy
  1. /*模仿IE浏览器从Tomcat服务器读取index.html 
  2. */  
  3. import java.io.*;  
  4. import java.net.*;  
  5. class MyIE   
  6. {  
  7.     public static void main(String[] args)throws Exception  
  8.     {  
  9.         Socket s=new Socket("172.16.56.237",8080);  
  10.   
  11.         PrintWriter out=new PrintWriter(s.getOutputStream(),true);  
  12.   
  13.         out.println("GET /myweb/index.html HTTP/1.1");//浏览器请求的文件目录  
  14.         out.println("Accept: */*");//浏览器支持的文件   */*  支持所有  
  15.         out.println("Accept-Language: zh-CN");//浏览器支持的语言  
  16.         out.println("Host: 172.16.56.237:1005");//浏览器请求的地址  
  17.         out.println("Connection: closed");//连接状态  closed 数据发送完毕,结束  
  18.   
  19.         out.println();  
  20.         out.println();  
  21.       
  22.   
  23.   
  24.         BufferedReader bufr=new BufferedReader(new InputStreamReader(s.getInputStream()));  
  25.   
  26.         String line=null;  
  27.         while((line=bufr.readLine())!=null)  
  28.         {  
  29.             System.out.println(line);  
  30.         }  
  31.   
  32.         s.close();  
  33.   
  34.     }  
  35. }  




七、网络编程(自定义图形界面浏览器-Tomcat服务端)


[java] view plaincopy
  1. /* 
  2. 自定义图形界面浏览器-Tomcat服务端: 
  3. */  
  4. import java.awt.*;  
  5. import java.awt.event.*;  
  6. import java.io.*;  
  7. import java.net.*;  
  8.   
  9. class MyIEByGUI  
  10. {  
  11.     private Frame f;  
  12.     private TextField tf;  
  13.     private Button but;  
  14.     private TextArea ta;  
  15.   
  16.     private Dialog d;  
  17.     private Label lab;  
  18.     private Button okBut;  
  19.       
  20.     MyIEByGUI()  
  21.     {  
  22.         init();  
  23.     }  
  24.     public void init()  
  25.     {  
  26.         f=new Frame("my window");  
  27.         f.setBounds(300,100,600,500);  
  28.         f.setLayout(new FlowLayout());  
  29.   
  30.         tf=new TextField(60);  
  31.         but=new Button("转到");  
  32.         ta=new TextArea(25 ,70);//  行,列  
  33.   
  34.   
  35.         d=new Dialog(f,"提示信息",true);//第3个参数:指定在显示的时候是否阻止用户将内容输入到其他顶级窗口中。  
  36.         d.setBounds(400,200,240,150);  
  37.         d.setLayout(new FlowLayout());  
  38.         lab=new Label();  
  39.         okBut=new Button("确定");  
  40.         d.add(lab);  
  41.         d.add(okBut);  
  42.           
  43.         f.add(tf);  
  44.         f.add(but);  
  45.         f.add(ta);  
  46.   
  47.         myEvent();  
  48.   
  49.         f.setVisible(true);  
  50.     }  
  51.     private void myEvent()  
  52.     {  
  53.         but.addActionListener(new ActionListener()  
  54.         {  
  55.             public void actionPerformed(ActionEvent e)  
  56.             {  
  57.                 try  
  58.                 {  
  59.                     showDir();  
  60.                 }  
  61.                 catch (Exception ee)  
  62.                 {  
  63.                     String info="您输入的地址是错误的,请重新输入!";  
  64.                     lab.setText(info);  
  65.                     d.setVisible(true);  
  66.                 }  
  67.                   
  68.             }  
  69.         });  
  70.   
  71.         f.addWindowListener(new WindowAdapter()  
  72.         {  
  73.             public void windowClosing(WindowEvent e)  
  74.             {  
  75.                 System.exit(0);  
  76.             }  
  77.         });  
  78.   
  79.         tf.addKeyListener(new KeyAdapter()  
  80.         {  
  81.             public void keyPressed(KeyEvent e)  
  82.             {     
  83.                 try  
  84.                 {  
  85.                     if(e.getKeyCode()==KeyEvent.VK_ENTER)  
  86.                         showDir();  
  87.                 }  
  88.                 catch (Exception ee)  
  89.                 {  
  90.                     String info="您输入的地址是错误的,请重新输入!";  
  91.                     lab.setText(info);  
  92.                     d.setVisible(true);  
  93.                 }  
  94.                   
  95.             }  
  96.         });  
  97.         okBut.addKeyListener(new KeyAdapter()  
  98.         {  
  99.             public void keyPressed(KeyEvent e)  
  100.             {     
  101.                 if(e.getKeyCode()==KeyEvent.VK_ENTER)  
  102.                     d.setVisible(false);  
  103.             }  
  104.         });  
  105.   
  106.         d.addWindowListener(new WindowAdapter()  
  107.         {  
  108.             public void windowClosing(WindowEvent e)  
  109.             {  
  110.                 d.setVisible(false);  
  111.             }  
  112.         });  
  113.         okBut.addActionListener(new ActionListener()  
  114.         {  
  115.             public void actionPerformed(ActionEvent e)  
  116.             {  
  117.                 d.setVisible(false);  
  118.             }  
  119.   
  120.         });  
  121.     }  
  122.     private void showDir()throws Exception  
  123.     {     
  124.         ta.setText("");//清空  
  125.   
  126.         String url=tf.getText();//    http://172.16.56.237:8080/myweb/index.html  
  127.           
  128.         int index1=url.indexOf("//")+2;  
  129.         int index2=url.indexOf("/",index1);  
  130.   
  131.         String str=url.substring(index1,index2);  
  132.         String[] arr=str.split(":");  
  133.         String host=arr[0];  
  134.         int port=Integer.parseInt(arr[1]);  
  135.   
  136.   
  137.         String path=url.substring(index2);  
  138.   
  139.         //ta.setText(str+"..."+path);//测试  
  140.   
  141.           
  142.         Socket s=new Socket(host,port);  
  143.   
  144.         PrintWriter out=new PrintWriter(s.getOutputStream(),true);  
  145.   
  146.         out.println("GET "+path+" HTTP/1.1");//浏览器请求的文件目录  
  147.         out.println("Accept: */*");//浏览器支持的文件   */*  支持所有  
  148.         out.println("Accept-Language: zh-CN");//浏览器支持的语言  
  149.         out.println("Host: 172.16.56.237:1005");//浏览器请求的地址  
  150.         out.println("Connection: closed");//连接状态  closed 数据发送完毕,结束  
  151.   
  152.         out.println();  
  153.         out.println();  
  154.       
  155.   
  156.   
  157.         BufferedReader bufr=new BufferedReader(new InputStreamReader(s.getInputStream()));  
  158.   
  159.         String line=null;  
  160.         while((line=bufr.readLine())!=null)  
  161.         {  
  162.             ta.append(line+"\r\n");  
  163.         }  
  164.   
  165.         s.close();  
  166.     }  
  167.   
  168.     public static void main(String[] args)   
  169.     {  
  170.         new MyIEByGUI();  
  171.     }  
  172. }  


八、网络编程(URL-URLConnection)


[java] view plaincopy
  1. /* 
  2.  
  3.  String getFile()  
  4.           获取此 URL 的文件名。  
  5.  String getHost()  
  6.           获取此 URL 的主机名(如果适用)。  
  7.  String getPath()  
  8.           获取此 URL 的路径部分。  
  9.  int getPort()  
  10.           获取此 URL 的端口号。  
  11.  String getProtocol()  
  12.           获取此 URL 的协议名称。  
  13.  String getQuery()  
  14.           获取此 URL 的查询部分。  
  15. */  
  16. import java.net.*;  
  17. import java.io.*;  
  18. class URLDemo   
  19. {  
  20.     public static void main(String[] args)throws MalformedURLException   
  21.     {  
  22.         URL url=new URL("http://172.16.56.237/myweb/index.html?name=yangcheng&age=21");  
  23.   
  24.         System.out.println("getProtocol:"+url.getProtocol());  
  25.         System.out.println("getHost:"+url.getHost());  
  26.         System.out.println("getPort:"+url.getPort());  
  27.         System.out.println("getPath:"+url.getPath());  
  28.         System.out.println("getFile:"+url.getFile());  
  29.         System.out.println("getQuery:"+url.getQuery());  
  30.   
  31.         /* 
  32.         int port=getPort(); 
  33.         if(port==-1) 
  34.             port=80; 
  35.         */  
  36.           
  37.     }  
  38. }  



[java] view plaincopy
  1. /* 
  2. URLConnection: 
  3.  
  4. */  
  5. import java.net.*;  
  6. import java.io.*;  
  7. class URLConnectionDemo   
  8. {  
  9.     public static void main(String[] args)throws Exception  
  10.     {  
  11.         URL url=new URL("http://172.16.56.237:8080/myweb/index.html");  
  12.   
  13.         URLConnection conn=url.openConnection();  
  14.   
  15.         InputStream in=conn.getInputStream();  
  16.         byte[] buf=new byte[1024];  
  17.         int len=in.read(buf);  
  18.         System.out.println(new String(buf,0,len));  
  19.           
  20.     }  
  21. }  


既然如此,那么:


[java] view plaincopy
  1. /* 
  2. 通过URLConnection拆封: 
  3. */  
  4. import java.awt.*;  
  5. import java.awt.event.*;  
  6. import java.io.*;  
  7. import java.net.*;  
  8.   
  9. class MyIEByGUI2  
  10. {  
  11.     private Frame f;  
  12.     private TextField tf;  
  13.     private Button but;  
  14.     private TextArea ta;  
  15.   
  16.     private Dialog d;  
  17.     private Label lab;  
  18.     private Button okBut;  
  19.       
  20.     MyIEByGUI2()  
  21.     {  
  22.         init();  
  23.     }  
  24.     public void init()  
  25.     {  
  26.         f=new Frame("my window");  
  27.         f.setBounds(300,100,600,500);  
  28.         f.setLayout(new FlowLayout());  
  29.   
  30.         tf=new TextField(60);  
  31.         but=new Button("转到");  
  32.         ta=new TextArea(25 ,70);//  行,列  
  33.   
  34.   
  35.         d=new Dialog(f,"提示信息",true);//第3个参数:指定在显示的时候是否阻止用户将内容输入到其他顶级窗口中。  
  36.         d.setBounds(400,200,240,150);  
  37.         d.setLayout(new FlowLayout());  
  38.         lab=new Label();  
  39.         okBut=new Button("确定");  
  40.         d.add(lab);  
  41.         d.add(okBut);  
  42.           
  43.         f.add(tf);  
  44.         f.add(but);  
  45.         f.add(ta);  
  46.   
  47.         myEvent();  
  48.   
  49.         f.setVisible(true);  
  50.     }  
  51.     private void myEvent()  
  52.     {  
  53.         but.addActionListener(new ActionListener()  
  54.         {  
  55.             public void actionPerformed(ActionEvent e)  
  56.             {  
  57.                 try  
  58.                 {  
  59.                     showDir();  
  60.                 }  
  61.                 catch (Exception ee)  
  62.                 {  
  63.                     String info="您输入的地址是错误的,请重新输入!";  
  64.                     lab.setText(info);  
  65.                     d.setVisible(true);  
  66.                 }  
  67.                   
  68.             }  
  69.         });  
  70.   
  71.         f.addWindowListener(new WindowAdapter()  
  72.         {  
  73.             public void windowClosing(WindowEvent e)  
  74.             {  
  75.                 System.exit(0);  
  76.             }  
  77.         });  
  78.   
  79.         tf.addKeyListener(new KeyAdapter()  
  80.         {  
  81.             public void keyPressed(KeyEvent e)  
  82.             {     
  83.                 try  
  84.                 {  
  85.                     if(e.getKeyCode()==KeyEvent.VK_ENTER)  
  86.                         showDir();  
  87.                 }  
  88.                 catch (Exception ee)  
  89.                 {  
  90.                     String info="您输入的地址是错误的,请重新输入!";  
  91.                     lab.setText(info);  
  92.                     d.setVisible(true);  
  93.                 }  
  94.                   
  95.             }  
  96.         });  
  97.         okBut.addKeyListener(new KeyAdapter()  
  98.         {  
  99.             public void keyPressed(KeyEvent e)  
  100.             {     
  101.                 if(e.getKeyCode()==KeyEvent.VK_ENTER)  
  102.                     d.setVisible(false);  
  103.             }  
  104.         });  
  105.   
  106.         d.addWindowListener(new WindowAdapter()  
  107.         {  
  108.             public void windowClosing(WindowEvent e)  
  109.             {  
  110.                 d.setVisible(false);  
  111.             }  
  112.         });  
  113.         okBut.addActionListener(new ActionListener()  
  114.         {  
  115.             public void actionPerformed(ActionEvent e)  
  116.             {  
  117.                 d.setVisible(false);  
  118.             }  
  119.   
  120.         });  
  121.     }  
  122.     private void showDir()throws Exception  
  123.     {     
  124.         ta.setText("");//清空  
  125.   
  126.         String urlPath=tf.getText();//    http://172.16.56.237:8080/myweb/index.html  
  127.           
  128.         URL url=new URL(urlPath);  
  129.   
  130.         URLConnection conn=url.openConnection();  
  131.   
  132.         InputStream in=conn.getInputStream();  
  133.         byte[] buf=new byte[1024];  
  134.         int len=in.read(buf);  
  135.         ta.setText(new String(buf,0,len));  
  136.     }  
  137.   
  138.     public static void main(String[] args)   
  139.     {  
  140.         new MyIEByGUI2();  
  141.     }  
  142. }  


九、网络编程(小知识点)

Socket类:

Socket() :通过系统默认类型的 SocketImpl 创建未连接套接字

void connect(SocketAddress endpoint) :将此套接字连接到服务器。

InetSocketAddress类:

而:InetSocketAddress继承自SocketAddress

InetSocketAddress类实现 IP 套接字地址(IP 地址 端口号)。它还可以是一个对(主机名 端口号),在此情况下,将尝试解析主机名。如果解析失败,则该地址将被视为未解析 地址,但是其在某些情形下仍然可以使用,比如通过代理连接。

InetAddress类实现 IP地址。

ServerSocket类:

public ServerSocket(int port,int backlog)throws IOException

利用指定的 backlog 创建服务器套接字并将其绑定到指定的本地端口号。

端口号 在所有空闲端口上创建套接字。 

port - 指定的端口;或者为 0,表示使用任何空闲端口。

backlog - 队列的最大长度。也就是联机数。 

十、网络编程(域名解析)


例如:

要访问www.baidu.com

先查看本地的hosts文件(域名和对应的IP缓存)有没有www.baidu.comIP。有的话,直接到hosts文件www.baidu.com对应的IP地址去访问。如果没有则会去DNS服务器找www.baidu.com的对应IP

hosts文件最常用来屏蔽危险连接。例如将不安全的域名直接在hosts文件中和127.0.0.1回环地址绑定。这个危险的网站就不会去DNS服务器找IP了,网站也就无法打开。

0 0
原创粉丝点击