Java socket Application (vi)--Server response client

Source: Internet
Author: User

Reprint Please specify : Http://blog.csdn.net/uniquewonderq

Through the input stream to read the client information, the corresponding time through the output stream to achieve.

The code for the service-side class:

Package Com.test;import Java.io.bufferedreader;import Java.io.ioexception;import java.io.inputstream;import Java.io.inputstreamreader;import Java.io.outputstream;import Java.io.printwriter;import Java.net.ServerSocket; Import Java.net.socket;import java.util.logging.level;import java.util.logging.logger;/** * * @author WONDERQ */public Class Server {//server-side public static void main (string[] args) {try {//1. Create a server-side socket, or serversocket,            Specifies the port that is bound and listens for this port.            ServerSocket serversocket=new ServerSocket (8888);            2. Call ServerSocket's Accept () method, waiting for the client's connection System.out.println ("= = Server is about to start, waiting for client connection = =");            Socket socket=serversocket.accept (); 3. Gets the input stream used to read the login information issued by the client InputStream Is=socket.getinputstream ();//Byte input stream InputStreamReader isr=new in            Putstreamreader (IS);//convert bytes into a character stream//add buffer to a character stream BufferedReader bufferedreader=new BufferedReader (ISR);            String Info=null;   Looping through client-submitted information         while ((Info=bufferedreader.readline ())!=null) {System.out.println ("I am the server, the client submits the information is:" +info);            } socket.shutdowninput ();                         4. Get the output stream, respond to client requests OutputStream os= Socket.getoutputstream (); PrintWriter pw=new printwriter (OS);//convert to print stream Pw.write ("Welcome!               ");            Pw.flush ();//flush cache//5. Close the associated resource bufferedreader.close ();            Is.close ();            Isr.close ();            Socket.close ();            Serversocket.close ();            Os.close ();        Pw.close ();        } catch (IOException ex) {Logger.getlogger (Server.class.getName ()). log (Level.severe, NULL, ex); }    }}

Code for the client class:

Package Com.test;import Java.io.bufferedreader;import Java.io.ioexception;import java.io.inputstream;import Java.io.inputstreamreader;import Java.io.outputstream;import Java.io.printwriter;import Java.net.Socket;import Java.util.logging.level;import java.util.logging.logger;/** * * @author WONDERQ */public class Client {//Client public STA tic void Main (string[] args) {try {//1. Create client socket, specify server address and port number socket socket=new socket ("12            7.0.0.1 ", 8888); 2. Gets the output stream used to send information to the server OutputStream os=socket.getoutputstream ();//Byte output stream//convert to print stream Prin              Twriter pw=new printwriter (OS);              Pw.write ("User name: Wonderq; Password: root");              Pw.flush ();//flush cache, output information to server//close output stream socket.shutdownoutput ();              3. Get the input stream to read the server-side response information InputStream is=socket.getinputstream ();              BufferedReader br=new BufferedReader (New InputStreamReader (IS));           String Info=null; while ((Info=br.readline ())!=null) {System.out.println ("I am the client, the server side returns information is:" +info);              }//4. Close resource Br.close ();              Is.close ();              Pw.close ();              Os.close ();        Socket.close ();        } catch (IOException ex) {Logger.getlogger (Client.class.getName ()). log (Level.severe, NULL, ex); }    }}


or run the server-side class before running the client class:

Server-side class output results:


Output from the client class:

















Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java socket Application (vi)--Server response client

Related Article

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.