Java Socket Application (v)--programming for TCP-based socket communication

Source: Internet
Author: User

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

Ideas:


The test code for the server segment is as follows:

Package Com.test;import Java.io.bufferedreader;import Java.io.ioexception;import java.io.inputstream;import Java.io.inputstreamreader;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, that is, ServerSocket, specify the bound port, and listen 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; Iterates through the client-submitted information while (Info=<span style= "font-family:arial, Helvetica, Sans-serif; "            >bufferedreader.readline () </span>)!=null) {System.out.println ("I am the server, the client submits the information is:" +info);            } socket.shutdowninput ();            4. Close the relevant resource bufferedreader.close ();            Is.close ();            Isr.close ();            Socket.close ();        Serversocket.close ();        } catch (IOException ex) {Logger.getlogger (Server.class.getName ()). log (Level.severe, NULL, ex); }    }}



The client is as follows:

Package Com.test;import Java.io.ioexception;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 {//clients public    static void Main (string[] args) {        try {            //1. Create client socket, specify server address and port number            socket socket =new Socket ("127.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              printwriter pw=new PrintWriter (OS);              Pw.write ("User name: Wonderq; Password: root");              Pw.flush ();//flush cache, output information to server side              //Turn off output stream              socket.shutdownoutput ();              3. Close Resource              pw.close ();              Os.close ();              Socket.close ();        } catch (IOException ex) {            Logger.getlogger (Client.class.getName ()). log (Level.severe, NULL, ex);}}}    



After that, run the server side and run the results:

= = Server is about to start, waiting for client connection = =


Then run the client, no output. At this point, to see the output of the server-side class, the discovery has changed:

This is:
























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

Java Socket Application (v)--programming for TCP-based socket communication

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.