本文共 3893 字,大约阅读时间需要 12 分钟。
代码只兼容JAVA 7及以上版本。
服务器端代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | package com.stevex.app.bio; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import javax.net.ServerSocketFactory; public class XiaoNa { public static void main(String[] args){ XiaoNa xiaona = new XiaoNa(); ServerSocket serverSocket = null ; Socket socket = null ; try { //可以设置客户连接请求队列的长度,比如5,队列长度超过5后拒绝连接请求 //serverSocket = ServerSocketFactory.getDefault().createServerSocket(8383, 5); serverSocket = ServerSocketFactory.getDefault().createServerSocket( 8383 ); while ( true ){ try { //监听直到接受连接后返回一个新Socket对象 socket = serverSocket.accept(); //阻塞 //new一个线程处理连接请求 new Thread(xiaona. new Worker(socket)).start();; } catch (Throwable e) { //防止发生异常搞死服务器 e.printStackTrace(); } } } catch (IOException e) { e.printStackTrace(); } finally { try { if (socket != null ){ socket.close(); } } catch (IOException e) { e.printStackTrace(); } } } private static String getAnswer(String question){ String answer = null ; switch (question){ case "who" : answer = "我是小娜" ; break ; case "what" : answer = "我是来帮你解闷的" ; break ; case "where" : answer = "我来自外太空" ; break ; default : answer = "请输入 who, 或者what, 或者where" ; } return answer; } private class Worker implements Runnable{ private Socket socket; public Worker(Socket socket){ this .socket = socket; } @Override public void run() { BufferedReader reader = null ; PrintWriter writer = null ; try { reader = new BufferedReader( new InputStreamReader(socket.getInputStream())); writer = new PrintWriter(socket.getOutputStream()); String question = reader.readLine(); //没有内容会阻塞 while (!question.equals( "OVER" )){ String answer = getAnswer(question); writer.println(answer); question = reader.readLine(); } writer.println( "OVER" ); //OVER作为操作完成暗号 writer.flush(); if (writer != null ){ writer.close(); } if (reader != null ){ reader.close(); } } catch (IOException e) { e.printStackTrace(); } } } } |
客户端代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | package com.stevex.app.bio; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class Client { public static void main(String[] args) { Client c = new Client(); //种20个线程发起Socket客户端连接请求 for ( int i= 0 ; i< 20 ; i++){ new Thread(c. new Worker()).start(); } } private class Worker implements Runnable{ @Override public void run() { Socket socket = null ; BufferedReader reader = null ; PrintWriter writer = null ; try { //创建一个Socket并连接到指定的目标服务器 socket = new Socket( "localhost" , 8383 ); reader = new BufferedReader( new InputStreamReader( socket.getInputStream())); writer = new PrintWriter(socket.getOutputStream()); writer.println( "who" ); writer.println( "what" ); writer.println( "where" ); writer.println( "OVER" ); //OVER作为操作完成暗号 writer.flush(); String answer = reader.readLine(); //没有内容会阻塞 while (! answer.equals( "OVER" )){ System.out.println(Thread.currentThread().getId() + "---Message from server:" + answer); answer = reader.readLine(); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (writer != null ) { writer.close(); } if (reader != null ) { reader.close(); } if (socket != null ) { socket.close(); } } catch (IOException e) { e.printStackTrace(); } } } } } |