JAVA BIO Server and client implementation examples

Source: Internet
Author: User

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, and reject the connection request after the queue length exceeds 5//serversocket =  Serversocketfactory.getdefault (). Createserversocket (8383, 5);serversocket =  Serversocketfactory.getdefault (). Createserversocket (8383); while (true) {try{//listens until a connection is accepted and returns a new socket object socket  = serversocket.accept ();//block//new a thread handles connection request New thread (Xiaona.new worker (socket)). Start ();;} catch  (throwable e)  {//prevents an exception from being killed by the server 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 =  "I'm Cortana";break;case  "what":answer =  "I'm here to help you with your sorrows. ";break;case " where ":answer = " I'm 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;} @Overridepublic  void run ()  {BufferedReader reader = null; Printwriter writer = null;try {reader = new bufferedreader (new  InputStreamReader (Socket.getinputstream ())); Writer = new printwriter (Socket.getOutputStream ()); String question&nBsp;= 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 password writer.flush (); if (writer != null) {writer.close ();} if (reader != null) {reader.close ();}}  catch  (ioexception e)  {e.printstacktrace ();}}}


Client 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 the socket client connection request for (int i=0; i<20; i++) {New thread (c.new  Worker ()). Start ();}} private class worker implements runnable{@Overridepublic  void run ()  {Socket  socket = null; bufferedreader reader = null; printwriter writer = null;try {//creates a Socket and connects to the specified target server socket = new socket ( "localhost",  8383); Reader = new bufferedreader (New inputstreamreader ( Socket.getinputstream ())); Writer = new printwriter (Socket.getoutputstream ()); Writer.println (" Writer.println ("what"); Writer.println ("whereWriter.println ("over");//over as the operation complete the 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 ();}}}}


This article is from "Power from absolute love!" "Blog, be sure to keep this provenance http://stevex.blog.51cto.com/4300375/1581093

JAVA BIO Server and client implementation examples

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.