The code is compatible with Java version 7 and above only.
Server-side code:
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 {//can set the length of the Client connection request queue, such as 5, which rejects the connection request after the queue length exceeds 5//serversocket = Serversocketfactory.getdefault (). Creates Erversocket (8383, 5); ServerSocket = Serversocketfactory.getdefault (). Createserversocket (8383); while (true) {try{//listens until a connection is accepted to return a new socket object socket = Serversocket.acce PT ();//block//new a thread handles the connection request new thread (Xiaona.new Worker (socket)). Start (); } catch (Throwable e) {//Prevent an exception from killing the server E.printstacktrace (); }}} catch (IOException e) {e.printstacktrace (); } finally{try {if (socket! = NULL) {s Ocket.close (); }} catch (IOException e) {e.printstacktrace (); }}} private static string Getanswer (String question) {string answer = NULL; Switch (question) {case ' who ': answer = "I am Cortana"; Break Case ' What ': Answer = "I'm here to help you with your sorrows"; Break Case "where": Answer = "I come from outer space"; Break Default:answer = "Please enter who, or what, or 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 ();//no content blocks while (!question.equals ("over")) { String answer = getanswer (question); Writer.println (answer); Question = Reader.readline (); } writer.println ("over");//over as Operation complete code writer.flush (); if (writer! = null) {writer.close (); } if (reader! = null) {reader.close (); }} catch (IOException e) {e.printstacktrace (); } } }}
Customer Segment Code:
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 (); Type 20 threads to initiate a socket client connection request for (int i=0; i<20; i++) {new Thread (C.new Worker ()). Start (); }} Private class Worker implements runnable{@Override public void Run () {Soc KET socket = NULL; BufferedReader reader = null; PrintWriter writer = null; try {//Create a socket and connect to the specified target server 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 as Operation complete code writer.flush (); String answer = Reader.readline (); No content blocks 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 (); } } } }}
Welcome everyone to visit my personal website Meng Meng's IT People
JAVA BIO Server and client implementation examples