标签:
第一:socket服务器的启动
1 package com.yeepay.sxf.testbio; 2 3 import java.io.IOException; 4 import java.net.ServerSocket; 5 import java.net.Socket; 6 7 /** 8 * 时间服务器 9 * 基于同步阻塞I/O实现的服务器模型 10 * @author sxf 11 * 12 */ 13 public class TimerServer { 14 15 /** 16 * 启动timerServer服务器 17 */ 18 public void init(){ 19 int port=8000; 20 //创建Socket服务 21 ServerSocket server=null; 22 try { 23 server=new ServerSocket(port); 24 System.out.println("TimerServer.init()===>the time server is start in port"+port); 25 Socket socket=null; 26 while(true){ 27 //获取一次socket请求 28 socket=server.accept(); 29 //启动一个新线程处理socket请求 30 new Thread(new TimerServerHandler(socket)).start(); 31 } 32 } catch (IOException e) { 33 e.printStackTrace(); 34 }finally{ 35 if(server!=null){ 36 try { 37 server.close(); 38 } catch (IOException e) { 39 // TODO Auto-generated catch block 40 e.printStackTrace(); 41 } 42 } 43 server=null; 44 } 45 46 } 47 48 49 public static void main(String[] args) { 50 //启动timerServer服务 51 TimerServer timerServer=new TimerServer(); 52 timerServer.init(); 53 } 54 }
第二:soket服务器接收到请求的处理类
1 package com.yeepay.sxf.testbio; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.io.PrintWriter; 7 import java.net.Socket; 8 import java.util.Date; 9 10 /** 11 * 时间服务器接受socket请求的处理类 12 * @author sxf 13 * 继承Runnable接口的线程类 14 * 15 */ 16 public class TimerServerHandler implements Runnable { 17 18 private Socket socket; 19 20 public TimerServerHandler(Socket socket) { 21 this.socket=socket; 22 } 23 24 /** 25 * 处理socket请求的线程体 26 */ 27 @Override 28 public void run() { 29 BufferedReader in=null; 30 PrintWriter out=null; 31 try { 32 //获取请求的输入流 33 in=new BufferedReader(new InputStreamReader(this.socket.getInputStream())); 34 //获取响应请求的输出流 35 out=new PrintWriter(this.socket.getOutputStream(),true); 36 37 String currentTime=null; 38 String body=null; 39 //读取请求输入流的内容获取请求信息 40 while(true){ 41 body=in.readLine(); 42 if(body==null){ 43 break; 44 } 45 //打印请求信息 46 System.out.println("TimerServerHandler.run()==>the time server receive order:"+body); 47 48 //处理请求信息 49 if("shangxiaofei".equals(body)){ 50 currentTime=new Date(System.currentTimeMillis()).toString(); 51 }else{ 52 currentTime="you is not get time"; 53 } 54 //响应请求信息 55 out.println(currentTime); 56 } 57 58 } catch (IOException e) { 59 e.printStackTrace(); 60 }finally{ 61 if(in!=null){ 62 try { 63 in.close(); 64 } catch (IOException e) { 65 // TODO Auto-generated catch block 66 e.printStackTrace(); 67 } 68 } 69 70 if(out!=null){ 71 out.close(); 72 } 73 74 if(this.socket!=null){ 75 try { 76 socket.close(); 77 } catch (IOException e) { 78 // TODO Auto-generated catch block 79 e.printStackTrace(); 80 } 81 } 82 83 this.socket=null; 84 } 85 86 87 88 89 } 90 91 }
第三:向socket服务器发送请求
1 package com.yeepay.sxf.testbio; 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.io.PrintWriter; 7 import java.net.Socket; 8 9 /** 10 * 创建一个客户端请求 11 * @author sxf 12 * 13 */ 14 public class TimerClient { 15 16 17 18 public static void main(String[] args) { 19 int port=8000; 20 Socket socket=null; 21 BufferedReader in=null; 22 PrintWriter out=null; 23 try { 24 socket=new Socket("127.0.0.1",port); 25 in=new BufferedReader(new InputStreamReader(socket.getInputStream())); 26 out=new PrintWriter(socket.getOutputStream(),true); 27 //发送请求 28 out.println("shangxiaofei!="); 29 System.out.println("TimerClient.main()send order to server success"); 30 31 //等待服务器响应 32 String resp=in.readLine(); 33 System.out.println("TimerClient.main(Now is:)"+resp); 34 } catch (Exception e) { 35 // TODO Auto-generated catch block 36 e.printStackTrace(); 37 }finally{ 38 if(out!=null){ 39 out.close(); 40 out=null; 41 } 42 if(in !=null){ 43 try { 44 in.close(); 45 } catch (IOException e) { 46 // TODO Auto-generated catch block 47 e.printStackTrace(); 48 } 49 in=null; 50 } 51 52 if(socket!=null){ 53 try { 54 socket.close(); 55 } catch (IOException e) { 56 // TODO Auto-generated catch block 57 e.printStackTrace(); 58 } 59 } 60 } 61 } 62 63 }
java的nio之:java的bio流下实现的socket服务器
标签:
原文地址:http://www.cnblogs.com/shangxiaofei/p/5827323.html