码迷,mamicode.com
首页 > 编程语言 > 详细

java高薪之路__009_网络

时间:2015-10-05 07:00:52      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:

1. InetAddress类
2. Socket: IP地址和端口号的结合,socket允许程序把网络连接当成一个流,数据在两个socket间通过IO传输, 通信的两端都要有socket. 主动发起通信请求的是客户端,等待通信请求的是服务端
3. DatagramSocket 和 DatagramPacket实现了UDP协议网络程序, 使用DatagramSocket发送和接收,系统不保证UDP数据报一定能够安全送到目的地,也不能确定什么时候可以抵达
  DatagramPacket对象封装了UDP数据报,在数据报中包含了发送端的IP地址和端口号以及接收端的IP地址和端口号,给出了完整的地址信息,因此无须建立发送方和接收方的连接
4. URL类
  URL u = new URL("http://www.baidu.com");
  InputStream in = u.openStream();

  URLConnection uc = u.openConnection();
  uc.getInputStream();
  uc.getOutputSteam();
技术分享
  1 package learn.JavaBasics.Class;
  2 
  3 import java.io.*;
  4 import java.net.InetAddress;
  5 import java.net.ServerSocket;
  6 import java.net.Socket;
  7 import java.net.UnknownHostException;
  8 
  9 public class SocketDemo {
 10     public void server() {
 11         ServerSocket ss = null;
 12         Socket s = null;
 13         InputStream in = null;
 14         OutputStream out = null;
 15 
 16         try {
 17             //开始在端口监听
 18             ss = new ServerSocket(9999);
 19             
 20             //当有连接进来时,会新建一个socket
 21             s = ss.accept();
 22             
 23             //获取输入流
 24             in = s.getInputStream();
 25             
 26             //对输入流进行处理
 27             byte[] b = new byte[1024];
 28             int len;
 29             String msg = "";
 30             
 31             while((len=in.read(b)) != -1) {
 32                 msg = new String(b, 0, len);
 33             }
 34             
 35             System.out.println("The message from "+ s.getInetAddress().getHostName()+
 36                     "(ip:"+ s.getInetAddress().getHostAddress()+") is: \""+msg+"\"");
 37             
 38             
 39             //获取输出流,反馈信息
 40             out = s.getOutputStream();
 41             out.write("The msg from client has been received".getBytes());
 42         } catch (IOException e) {
 43             // TODO Auto-generated catch block
 44             e.printStackTrace();
 45         } finally {
 46             if (in != null) {
 47                 try {
 48                     in.close();
 49                 } catch (IOException e) {
 50                     // TODO Auto-generated catch block
 51                     e.printStackTrace();
 52                 }
 53             }
 54             
 55             if (s != null) {
 56                 try {
 57                     s.close();
 58                 } catch (IOException e) {
 59                     // TODO Auto-generated catch block
 60                     e.printStackTrace();
 61                 }
 62             }
 63             
 64             if (ss != null) {
 65                 try {
 66                     ss.close();
 67                 } catch (IOException e) {
 68                     // TODO Auto-generated catch block
 69                     e.printStackTrace();
 70                 }
 71             }
 72             
 73             if (out != null) {
 74                 try {
 75                     out.close();
 76                 } catch (IOException e) {
 77                     // TODO Auto-generated catch block
 78                     e.printStackTrace();
 79                 }
 80             }
 81         }
 82     }
 83     
 84     public void client() {
 85         Socket s = null;
 86         OutputStream out = null;
 87         InputStream in = null;
 88         
 89         try {
 90             //新建一个Socket
 91             s = new Socket(InetAddress.getLocalHost(), 9999);
 92             
 93             //获取输出流
 94             out = s.getOutputStream();
 95             
 96             //发送信息, 并提示
 97             out.write("This is client".getBytes());
 98             System.out.println("client message has sent");
 99             
100             //这句话存在的原因是,Server端的getInputSream流的read方法是阻塞, 它在接收信息,但不知道信息是否接收完全,所以一直在等待,
101             //所以Server端的反馈信息无发发送过来,程序死掉,
102             //这里显式的通知Server,发送结束
103             //FileInputSteam里的read方法与这个一样,但BufferInputSteam里的read方法是非阻塞的
104             s.shutdownOutput();
105             
106             //获取输入流,接收服务端的反馈信息
107             in = s.getInputStream();
108             
109             byte[] b = new byte[1024];
110             int len;
111             String msg = "";
112             
113             while((len=in.read(b)) != -1) {
114                 msg = new String(b, 0, len);
115             }
116             System.out.println("Response from Server: "+msg);
117             System.out.println("connection over");
118             
119             //这里的后边没有了,所以服务端可以不显式的提示,不影响功能
120         } catch (IOException e) {
121             // TODO Auto-generated catch block
122             e.printStackTrace();
123         } finally {
124             if (out != null) {
125                 try {
126                     out.close();
127                 } catch (IOException e) {
128                     // TODO Auto-generated catch block
129                     e.printStackTrace();
130                 }
131             }
132             if (s != null) {
133                 try {
134                     s.close();
135                 } catch (IOException e) {
136                     // TODO Auto-generated catch block
137                     e.printStackTrace();
138                 }
139             }
140             
141             if (in != null) {
142                 try {
143                     in.close();
144                 } catch (IOException e) {
145                     // TODO Auto-generated catch block
146                     e.printStackTrace();
147                 }
148             }
149         }
150     }
151     
152     public static void main(String[] args) {
153         new Thread(new Runnable(){
154             @Override
155             public void run() {
156                 // TODO Auto-generated method stub
157                 new SocketDemo().server();
158             }            
159         }).start();
160         
161         new Thread(new Runnable() {
162             @Override
163             public void run() {
164                 // TODO Auto-generated method stub
165                 new SocketDemo().client();
166             }            
167         }).start();
168     }
169 
170 }
View Code
技术分享
 1 package learn.JavaBasics.Class;
 2 
 3 import java.io.IOException;
 4 import java.net.DatagramPacket;
 5 import java.net.DatagramSocket;
 6 import java.net.InetAddress;
 7 import java.net.SocketException;
 8 import java.net.UnknownHostException;
 9 
10 public class TestUDP {
11     public void receive(){
12         DatagramSocket ds = null;
13         
14         try {
15             //定义接收的数据报大小
16             byte[] b = new byte[1024];
17             DatagramPacket packet = new DatagramPacket(b, 0, b.length);
18             
19             //定义一个接收的Socket,用于在特定的端口接收指定大小的数据报
20             ds = new DatagramSocket(9999);
21             ds.receive(packet);
22             
23             String str = new String(packet.getData(), 0, packet.getLength());
24             
25             System.out.println(str);
26         } catch (SocketException e) {
27             // TODO Auto-generated catch block
28             e.printStackTrace();
29         } catch (IOException e) {
30             // TODO Auto-generated catch block
31             e.printStackTrace();
32         } finally {
33             if (ds != null) {
34                 ds.close();
35             }
36         }
37     }
38     
39     public void send() {
40         DatagramSocket ds = null;
41         
42         try {
43             //新建一个用于发送的Socket
44             ds = new DatagramSocket();
45             
46             //数据
47             byte[] b = "你好, 这里是发送端的数据, UDP sender".getBytes();
48             
49             //封装数据到数据报中, 一个数据报不能大于64K, 记录着数据信息, 两端的IP和端口号
50             DatagramPacket packet = new DatagramPacket(b, 0, b.length, InetAddress.getLocalHost(), 9999);
51             
52             //使用Socket把数据报发走
53             ds.send(packet);
54         } catch (SocketException e) {
55             // TODO Auto-generated catch block
56             e.printStackTrace();
57         } catch (UnknownHostException e) {
58             // TODO Auto-generated catch block
59             e.printStackTrace();
60         } catch (IOException e) {
61             // TODO Auto-generated catch block
62             e.printStackTrace();
63         } finally {
64             if (ds != null) {
65                 ds.close();
66             }
67         }
68     }
69     
70     public static void main(String[] args) {
71         new Thread(new Runnable(){
72             @Override
73             public void run() {
74                 new TestUDP().receive();
75             }
76         }).start();
77 
78         new Thread(new Runnable(){
79             @Override
80             public void run() {
81                 new TestUDP().send();
82             }
83         }).start();
84     }
85 }
View Code

 

java高薪之路__009_网络

标签:

原文地址:http://www.cnblogs.com/hangtt/p/4855290.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!