Client program 1:chatclient.java<pre name= "code" class= "java" >import java.io.*;import Java.net.*;import Java.applet.applet;public class Chatserver {public static void main (String[]args) {Try{serversocket Server = null;try{ Server = new ServerSocket (4000); System.out.println ("Ready. Exit input Bye ");} catch (Exception e) {System.out.println ("Can not listen to" + E);} Socket socket = Null;try{socket = Server.accept ();} catch (Exception e) {System.out.println ("Error." + e);} String Line; BufferedReader is = new BufferedReader (New InputStreamReader (Socket.getinputstream ())); PrintWriter os = new PrintWriter (Socket.getoutputstream ()); BufferedReader sin = new BufferedReader (new InputStreamReader (system.in)); System.out.println ("Client:" + is.readline ()), line = Sin.readline (), while (! Line.equals ("Bye")) {os.println (line); Os.flush (); System.out.println ("Server:" + line); System.out.println ("Client:" + is.readline ()); line = Sin.readline (); }os.close (); Is.close (); Socket.close (); Server.close ();} catch (Exception e) {System.ouT.println ("Error:" + E);}}} Client program 2:chatserver.java
Import Java.io.*;import java.net.*;p ublic class Cgatclient {public static void main (String[]args) {Try{socket Socket = new Socket ("127.0.0.1", 4000); System.out.println ("Enter what you want to say, if you want to exit input bye"); BufferedReader sin = new BufferedReader (new InputStreamReader (system.in)); PrintWriter os = new PrintWriter (Socket.getoutputstream ()); BufferedReader is = new BufferedReader (New InputStreamReader (Socket.getinputstream ())); String readline;readline = Sin.readline (); while (! readline.equals ("Bye")) {os.println (readline); Os.flush (); System.out.println ("catclient:" + readline); System.out.println ("Server:" + is.readline ()); readLine = Sin.readline ();} Os.close (); Is.close (); Socket.close ();} catch (Exception e) {System.out.println ("Error" + e);}}}
The result of the implementation is that whatever dialog box is compiled in that program, you enter what you want to say in it, and it will be displayed in another dialog box. These two small programs achieve simple dialogue and communication.
At the end of the dialog box, enter bye, which is the end of the session.
Java Third job: Java and socket implementations