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

Socket--Java

时间:2016-12-18 01:44:16      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:rate   api   tin   closed   .com   equal   tor   soc   except   

Server.java

技术分享
 1 package socket;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.DataInputStream;
 5 import java.io.DataOutputStream;
 6 import java.io.IOException;
 7 import java.io.InputStream;
 8 import java.io.InputStreamReader;
 9 import java.io.OutputStream;
10 import java.net.ServerSocket;
11 import java.net.Socket;
12 
13 public class Server {
14     //监听的端口
15     public static final int PORT = 54039;
16     
17     public static void main(String[] args)
18     {
19         new Server().start();
20     }
21     
22     //启动监听线程的方法
23     public void start()
24     {
25         ServerSocket serverSocket = null;
26         try {
27             //新建一个ServerSocket对象,监听PORT端口,可能会抛出IOException
28             serverSocket  = new ServerSocket(PORT);
29             System.out.println("serverScoket:" + serverSocket);
30             //循环监听
31             while(true)
32             {
33                 //获取客户端连接,如果没有则阻塞
34                 Socket clientSocket = serverSocket.accept();
35                 //启动线程处理监听到的数据并启动线程
36                 new Thread(new DataRunnable(clientSocket)).start();
37                 
38             }
39         } catch (IOException e) {
40             e.printStackTrace();
41         } finally{
42             if (null != serverSocket)
43             {
44                 try {
45                     serverSocket.close();
46                     System.out.println("close serverSocket success!");
47                 } catch (IOException e) {
48                     System.out.println("close serverSocket failed!");
49                 }
50             }
51         }
52         
53     }
54     
55     /**
56      * 处理监听信息的job
57      */
58     private class DataRunnable implements Runnable
59     {
60         Socket client;
61         
62         public DataRunnable(Socket client) {
63             super();
64             this.client = client;
65         }
66 
67         public void run() {
68             try {
69                 //读取客户端socket取输入流,包装成DataInputStream
70                 DataInputStream dataIn = new DataInputStream(client.getInputStream());
71                 DataOutputStream dataOut = new DataOutputStream(client.getOutputStream());
72                 String content = dataIn.readUTF();//有read其他格式的方法,具体见api
73                 //打印到控制台
74                 System.out.println("content read in from client:" + content);
75                 
76                 //向客户端回复信息
77                 System.out.println("请输入要回复的内容:");
78                 String reply = new BufferedReader(new InputStreamReader(System.in)).readLine();
79                 dataOut.writeUTF(reply);
80                 
81                 if("Close".equals(content))
82                 {
83                     System.out.println("client has been closed ,i want to be closed too");
84                 }
85                 dataIn.close();
86                 dataOut.close();
87             } catch (IOException e) {
88                 e.printStackTrace();
89             }
90             
91         }
92         
93     }
94 }
View Code

Client.java

技术分享
 1 package socket;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.DataInputStream;
 5 import java.io.DataOutputStream;
 6 import java.io.IOException;
 7 import java.io.InputStream;
 8 import java.io.InputStreamReader;
 9 import java.net.Socket;
10 import java.net.UnknownHostException;
11 
12 public class Client {
13     public static final String ADDR = "localhost";//监听本机
14     public static final int PORT = 54039;//端口
15     
16     public static void main(String[] args)
17     {    
18         while(true)
19         {
20             Socket client = null;
21             DataInputStream dataIn = null;
22             DataOutputStream dataout = null;
23             try {
24                 //创建客户端socket连接到主机服务器端
25                 client = new Socket(ADDR, PORT);
26                 
27                 dataIn = new DataInputStream(client.getInputStream());
28                 dataout = new DataOutputStream(client.getOutputStream());
29                 
30                 //向服务器发送数据
31                 System.out.println("请输入数据:");
32                 String content = new BufferedReader(new InputStreamReader(System.in)).readLine();
33                 dataout.writeUTF(content);
34                 
35                 //读取服务器端返回的数据
36                 String reply = dataIn.readUTF();
37                 System.out.println("server reply:" + reply);
38                 
39             } catch (UnknownHostException e) {
40                 // TODO Auto-generated catch block
41                 e.printStackTrace();
42             } catch (IOException e) {
43                 // TODO Auto-generated catch block
44                 e.printStackTrace();
45             }
46             finally{
47                 try {
48                     if(null!=dataIn) dataIn.close();
49                     if(null!=dataout) dataout.close();
50                     if(null!=client) client.close();
51                 } catch (IOException e) {
52                     // TODO Auto-generated catch block
53                     e.printStackTrace();
54                 }
55             }
56         }
57     }
58 }
View Code

 

Socket--Java

标签:rate   api   tin   closed   .com   equal   tor   soc   except   

原文地址:http://www.cnblogs.com/cici20166/p/6193365.html

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