Java socket programming, read the server a few characters, and then write to the local display.

Source: Internet
Author: User

Server-side Programs

ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public classServer {PrivateServerSocket SS; Privatesocket socket; PrivateBufferedReader in; PrivatePrintWriter out;  PublicServer () {Try{SS=NewServerSocket (10000);  while(true) {Socket=ss.accept (); String Remoteip=socket.getinetaddress (). gethostaddress (); String RemotePort=":"+Socket.getlocalport (); System.out.println ("A Client Come in! IP: "+remoteip+remoteport); Inch=NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); String Line=In.readline (); System.out.println ("Client Send is:" +Line ); out=NewPrintWriter (Socket.getoutputstream (),true); Out.println ("Your Message received!");                Out.close ();                In.close ();                                            Socket.close (); }        } Catch(IOException e) {//TODO auto-generated Catch blockOut.print ("Wrong"); }    }     Public Static voidMain (string[] args) {NewServer (); }        }

Client Terminal program:

ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.Socket;Importjava.net.UnknownHostException; Public classClient {socket socket;        BufferedReader in;        PrintWriter out;  PublicClient () {System.out.println ("Try to Connect to 127.0.0.1:10000"); Try{Socket=NewSocket ("127.0.0.1", 10000); System.out.println ("The Server connected!"); System.out.println ("Please enter some character:"); BufferedReader Line=NewBufferedReader (NewInputStreamReader (system.in)); out=NewPrintWriter (Socket.getoutputstream (),true); Out.println (Line.readline ());//// in=NewBufferedReader (NewInputStreamReader (Socket.getinputstream ()));                System.out.println (In.readline ());                Out.close ();                In.close ();                            Socket.close (); } Catch(unknownhostexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } Catch(IOException e) {//TODO auto-generated Catch blockOut.println ("Wrong"); }                    }         Public Static voidMain (string[] args) {NewClient (); }}

Java socket programming, read the server a few characters, and then write to the local display.

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.