Java--tcp

Source: Internet
Author: User

Import Java.io.printstream;import Java.net.serversocket;import java.net.socket;//================================ =================//File Name       : helloserver_demo//----------------------------------------------------------- -------------------//Author          : common//main class//function        : Helloserver_demopublic class Helloserver_demo {public static void Main (string[] args) throws exception{//TODO auto-generated method stub serversocket server = NULL;//Declaration ServerSocket Object Socket CLI ent = null;//A Socket object represents a client printstream out = null;//declares a print stream object to output server = new ServerSocket (8888) to the client;// Indicates that the server waits on port 8888 for client Access System.out.println ("Server is running, waiting for client Connection"), client = Server.accept ();//program blocked, waiting for client to connect string str = " Helloword ";//To output information to the client out = new PrintStream (Client.getoutputstream ());//instantiate the Print stream object, output information out.println (str); Out.close () ; Client.close (); Server.close ();}}

Import Java.io.bufferedreader;import Java.io.inputstreamreader;import java.net.socket;//========================= ========================//File Name       : helloclient_demo//---------------------------------------------------- --------------------------//Author          : common//main class//function        : Helloclient_demopublic class Helloclient_demo { public static void Main (string[] args) throws exception{//TODO auto-generated method stub socket client = null;//Declaration Socket Object client = new Soc Ket ("localhost", 8888);//Specify the connected host and port BufferedReader BUF = null;//Declare BufferedReader object, accept the message//Specify the client's input stream buf = new BufferedReader (New InputStreamReader (Client.getinputstream ())); String str = buf.readline (); SYSTEM.OUT.PRINTLN ("Server output content is" +str); Client.close (); Buf.close ();}}

Java--tcp

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.