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