首页 > 代码库 > java的nio之:java的bio流下实现的socket服务器
java的nio之:java的bio流下实现的socket服务器
第一: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 sxf11 *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 block40 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 sxf13 * 继承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 @Override28 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 block66 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 block79 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 sxf12 *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 block36 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 block47 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 block57 e.printStackTrace();58 }59 }60 }61 }62 63 }
java的nio之:java的bio流下实现的socket服务器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。