Java学习笔记之网络编程

1、要想实现网络传输,需要考虑哪些问题?

1.1 如何才能准确的定位网络上的一台主机?

1.2 如何才能进行可靠的、高效的数据传输?

 

 

2、Java如何实现的网络通信

2.1 使用IP地址定位一台主机 使用端口号定位一个应用-------->IPAddress类。

>如何创建一个InetAddress的对象?getByName("");比如InetAddress inet = InetAddress.getByName("127.0.0.1");

>如何获取本机的一个InetAddress的对象?getLocalHost()

>域名:getHostName();   IP:getHostAddress();

2.2 对应有协议

对于传输层而言,分为TCP、UDP

TCP编程例子: Socket、ServerSocket

 

[java] view plain copy
 
 print?
  1. /** 
  2.  * TCP编程例一: 
  3.  * 客户端给服务端发送信息,服务端输出此信息到控制台上 
  4.  * 网络编程实际上就是Socket编程 
  5.  */  
  6. public class TestTCP1 {  
  7.     @Test  
  8.     public void client() {  
  9.         Socket socket = null;  
  10.         OutputStream os = null;  
  11.         try {  
  12.             // 1.创建一个Socket对象,通过构造器指明服务端的IP地址,以及其接收程序的端口号。  
  13.             socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);  
  14.             // 2.getOutputStream():发送数据,方法返回OutputStream对象。  
  15.             os = socket.getOutputStream();  
  16.             // 3.具体的输出过程。  
  17.             os.write("我是客户端,请多关照!".getBytes());  
  18.         } catch (IOException e) {  
  19.             e.printStackTrace();  
  20.         } finally {  
  21.             // 4.关闭相应的流和Socket对象。  
  22.             if (os != null) {  
  23.                 try {  
  24.                     os.close();  
  25.                 } catch (IOException e) {  
  26.                     e.printStackTrace();  
  27.                 }  
  28.             }  
  29.             if (socket != null) {  
  30.                 try {  
  31.                     socket.close();  
  32.                 } catch (IOException e) {  
  33.                     e.printStackTrace();  
  34.                 }  
  35.             }  
  36.         }  
  37.   
  38.     }  
  39.   
  40.     @Test  
  41.     public void server() {  
  42.         ServerSocket ss = null;  
  43.         Socket s = null;  
  44.         InputStream is = null;  
  45.         try {  
  46.             // 1.创建ServerSocket对象,通过构造器指明自身的端口号。  
  47.             ss = new ServerSocket(9090);  
  48.             // 2.调用其accept()方法,返回一个socket的对象  
  49.             s = ss.accept();  
  50.             // 3. 调用Socket对象的getInputStream()获取一个从客户端发送来的输入流。  
  51.             is = s.getInputStream();  
  52.             // 4.对获取的输入流进行操作  
  53.             byte[] b = new byte[20];  
  54.             int len;  
  55.             while ((len = is.read(b)) != -1) {  
  56.                 String str = new String(b, 0, len);  
  57.                 System.out.println(str);  
  58.             }  
  59.         } catch (IOException e) {  
  60.             e.printStackTrace();  
  61.         } finally {  
  62.             // 5.关闭相应的流以及Socket、ServerSocket对象。  
  63.             if (is != null) {  
  64.                 try {  
  65.                     is.close();  
  66.                 } catch (IOException e) {  
  67.                     e.printStackTrace();  
  68.                 }  
  69.             }  
  70.             if (s != null) {  
  71.                 try {  
  72.                     s.close();  
  73.                 } catch (IOException e) {  
  74.                     e.printStackTrace();  
  75.                 }  
  76.             }  
  77.   
  78.             if (ss != null) {  
  79.                 try {  
  80.                     ss.close();  
  81.                 } catch (IOException e) {  
  82.                     e.printStackTrace();  
  83.                 }  
  84.             }  
  85.         }  
  86.     }  
  87. }  

 

UDP编程例子:DatagramSocket、DatagramPacket

 

[java] view plain copy
 
 print?
  1. public class TestUDP1 {  
  2.     /** 
  3.      * 发送端 
  4.      */  
  5.     @Test  
  6.     public void send() {  
  7.         DatagramSocket ds = null;  
  8.         try {  
  9.             ds = new DatagramSocket();  
  10.             byte[] b = "你好,我是要发送的数据!".getBytes();  
  11.             // 创建一个数据报:每一个数据报不能大于64K,都记录着数据信息以及发送端的IP、端口号以及要发送到的  
  12.             // 接收端的IP、端口号。  
  13.             DatagramPacket packet = new DatagramPacket(b, 0, b.length, InetAddress.getByName("127.0.0.1"), 9090);  
  14.             ds.send(packet);  
  15.         } catch (IOException e) {  
  16.             e.printStackTrace();  
  17.         } finally {  
  18.             if (ds != null) {  
  19.                 ds.close();  
  20.             }  
  21.         }  
  22.   
  23.     }  
  24.   
  25.     /** 
  26.      * 接收端 
  27.      */  
  28.     @Test  
  29.     public void receive() {  
  30.         DatagramSocket ds = null;  
  31.         try {  
  32.             ds = new DatagramSocket(9090);  
  33.             byte[] b = new byte[1024];  
  34.             DatagramPacket packet = new DatagramPacket(b, 0, b.length);  
  35.             ds.receive(packet);  
  36.             String str = new String(packet.getData(), 0, packet.getLength());  
  37.             System.out.print(str);  
  38.         } catch (IOException e) {  
  39.             e.printStackTrace();  
  40.         } finally {  
  41.             if (ds != null) {  
  42.                 ds.close();  
  43.             }  
  44.         }  
  45.     }  
  46. }  
URL编程:统一资源定位符 Uniform Resource Locator

 

例子:

[java] view plain copy
 
 print?
  1. /** 
  2.  * URL:统一资源定位符,一个URL对象对应着互联网上一个资源。 
  3.  * 我们可以通过URL的对象调用其相应的方法,将此资源读取(“下载”) 
  4.  */  
  5. public class TestURL {  
  6.     public static void main(String[] args) throws Exception {  
  7.         // 1.创建一个URL对象  
  8.         URL url = new URL("http://127.0.0.1:8080/examples/helloworld.txt");  
  9.         // 2.常用的方法:  
  10. //        System.out.println(url.getProtocol());  
  11. //        System.out.println(url.getHost());  
  12. //        System.out.println(url.getPort());  
  13. //        System.out.println(url.getFile());  
  14. //        System.out.println(url.getRef());  
  15. //        System.out.println(url.getQuery());  
  16.         // 如何将服务端的资源读取进来?  
  17.         InputStream is = url.openStream();  
  18.         byte[] b = new byte[20];  
  19.         int len;  
  20.         while ((len = is.read(b)) != -1) {  
  21.             String str = new String(b, 0, len);  
  22.             System.out.println(str);  
  23.         }  
  24.         //如果既有数据的输入,也有输出,则须使用URLConnection  
  25.         URLConnection urlConn = url.openConnection();  
  26.         InputStream is1 = urlConn.getInputStream();  
  27.         FileOutputStream fos = new FileOutputStream(new File("abc.txt"));  
  28.         byte[] b1 = new byte[20];  
  29.         int len1;  
  30.         while ((len1 = is1.read(b1)) != -1) {  
  31.             fos.write(b1, 0, len1);  
  32.         }  
  33.         is.close();  
  34.         is1.close();  
  35.         fos.close();  
  36.     }  
  37. }  
posted @ 2016-12-05 15:05  天涯海角路  阅读(96)  评论(0)    收藏  举报