经典 socket通讯 -- 已验证

[tcp通信]
来源:不详;
client:
  1. package com.defonds.socket.begin;  
  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.InputStreamReader;  
  8. import java.net.Socket;  
  9.   
  10. public class Client {  
  11.     public static final String IP_ADDR = "localhost";//服务器地址   
  12.     public static final int PORT = 12345;//服务器端口号    
  13.       
  14.     public static void main(String[] args) {    
  15.         System.out.println("客户端启动...");    
  16.         System.out.println("当接收到服务器端字符为 \"OK\" 的时候, 客户端将终止\n");   
  17.         while (true) {    
  18.             Socket socket = null;  
  19.             try {  
  20.                 //创建一个流套接字并将其连接到指定主机上的指定端口号  
  21.                 socket = new Socket(IP_ADDR, PORT);    
  22.                     
  23.                 //读取服务器端数据    
  24.                 DataInputStream input = new DataInputStream(socket.getInputStream());    
  25.                 //向服务器端发送数据    
  26.                 DataOutputStream out = new DataOutputStream(socket.getOutputStream());    
  27.                 System.out.print("请输入: \t");    
  28.                 String str = new BufferedReader(new InputStreamReader(System.in)).readLine();    
  29.                 out.writeUTF(str);    
  30.                     
  31.                 String ret = input.readUTF();     
  32.                 System.out.println("服务器端返回过来的是: " + ret);    
  33.                 // 如接收到 "OK" 则断开连接    
  34.                 if ("OK".equals(ret)) {    
  35.                     System.out.println("客户端将关闭连接");    
  36.                     Thread.sleep(500);    
  37.                     break;    
  38.                 }    
  39.                   
  40.                 out.close();  
  41.                 input.close();  
  42.             } catch (Exception e) {  
  43.                 System.out.println("客户端异常:" + e.getMessage());   
  44.             } finally {  
  45.                 if (socket != null) {  
  46.                     try {  
  47.                         socket.close();  
  48.                     } catch (IOException e) {  
  49.                         socket = null;   
  50.                         System.out.println("客户端 finally 异常:" + e.getMessage());   
  51.                     }  
  52.                 }  
  53.             }  
  54.         }    
  55.     }    
  56. }    
服务端:
  1. package com.defonds.socket.begin;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.DataInputStream;  
  5. import java.io.DataOutputStream;  
  6. import java.io.InputStreamReader;  
  7. import java.net.ServerSocket;  
  8. import java.net.Socket;  
  9.   
  10. public class Server {  
  11.     public static final int PORT = 12345;//监听的端口号     
  12.       
  13.     public static void main(String[] args) {    
  14.         System.out.println("服务器启动...\n");    
  15.         Server server = new Server();    
  16.         server.init();    
  17.     }    
  18.     
  19.     public void init() {    
  20.         try {    
  21.             ServerSocket serverSocket = new ServerSocket(PORT);    
  22.             while (true) {    
  23.                 // 一旦有堵塞, 则表示服务器与客户端获得了连接    
  24.                 Socket client = serverSocket.accept();    
  25.                 // 处理这次连接    
  26.                 new HandlerThread(client);    
  27.             }    
  28.         } catch (Exception e) {    
  29.             System.out.println("服务器异常: " + e.getMessage());    
  30.         }    
  31.     }    
  32.     
  33.     private class HandlerThread implements Runnable {    
  34.         private Socket socket;    
  35.         public HandlerThread(Socket client) {    
  36.             socket = client;    
  37.             new Thread(this).start();    
  38.         }    
  39.     
  40.         public void run() {    
  41.             try {    
  42.                 // 读取客户端数据    
  43.                 DataInputStream input = new DataInputStream(socket.getInputStream());  
  44.                 String clientInputStr = input.readUTF();//这里要注意和客户端输出流的写方法对应,否则会抛 EOFException  
  45.                 // 处理客户端数据    
  46.                 System.out.println("客户端发过来的内容:" + clientInputStr);    
  47.     
  48.                 // 向客户端回复信息    
  49.                 DataOutputStream out = new DataOutputStream(socket.getOutputStream());    
  50.                 System.out.print("请输入:\t");    
  51.                 // 发送键盘输入的一行    
  52.                 String s = new BufferedReader(new InputStreamReader(System.in)).readLine();    
  53.                 out.writeUTF(s);    
  54.                   
  55.                 out.close();    
  56.                 input.close();    
  57.             } catch (Exception e) {    
  58.                 System.out.println("服务器 run 异常: " + e.getMessage());    
  59.             } finally {    
  60.                 if (socket != null) {    
  61.                     try {    
  62.                         socket.close();    
  63.                     } catch (Exception e) {    
  64.                         socket = null;    
  65.                         System.out.println("服务端 finally 异常:" + e.getMessage());    
  66.                     }    
  67.                 }    
  68.             }   
  69.         }    
  70.     }    
  71. }    
posted @ 2016-08-05 14:20  Study_Work  阅读(449)  评论(0编辑  收藏  举报