target of the job
Target 1:socket simple application. Input the textbook p238-230 sample 11-5 code into our Code editor, Debug and run (80 points).
P238 client program
Import Java.io.*;import java.net.*;p ublic class ChatClient {public static void main (String args[]) {Try{socket socket=new Socket ("127.0.0.1", 4001); 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 ("Client:" +readline); System.out.println ("Server:" +is.readline ()); Readline=sin.readline ();} Os.close (); Is.close (); Socket.close ();} catch (Exception e) {System.out.println ("Error" +e);}}
p238-230 Server-side programs
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 (4001); 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.print (line); Os.flush (); 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);}}
The third assignment of Java programming: Network programming