ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter;ImportJava.io.PrintWriter;ImportJava.net.ServerSocket;ImportJava.net.Socket;//Main class Talkclient.class Public classtalkclient{ Public Static voidMain (String args[]) {Try{ //make a request to port 4700 of 127.0.0.1Socket socket =NewSocket ("127.0.0.1", 4700);// //constructing BufferedReader objects from System standard input devicesBufferedReader sin =NewBufferedReader (NewInputStreamReader (system.in)); //The output stream is obtained by the socket object, and the PrintWriter object is constructedPrintWriter os=NewPrintWriter (Socket.getoutputstream ()); //The input stream is obtained by the socket object and the corresponding BufferedReader object is constructedBufferedReader is=NewBufferedReader (NewInputStreamReader (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); } }}
ImportJava.io.BufferedReader;ImportJava.io.BufferedWriter;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.OutputStreamWriter;ImportJava.io.PrintWriter;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public classtalkserver{ Public Static voidMain (String args[]) {Try{serversocket server=NULL; Try{Server=NewServerSocket (4700); //Create a ServerSocket to listen for customer requests on port 4700}Catch(Exception e) {System.out.println ("Can not listen to:" +e); //error, printing error message} socket Socket=NULL; Try{Socket=server.accept (); //use the Accept () block to wait for a customer request, a customer//When the request arrives, a socket object is generated and the execution continues}Catch(Exception e) {System.out.println ("Error." +e); } String Line; BufferedReader is=NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); //The input stream is obtained by the socket object and the corresponding BufferedReader object is constructedPrintWriter os=NewPrintWriter (Socket.getoutputstream ()); //The output stream is obtained by the socket object, and the PrintWriter object is constructedBufferedReader sin=NewBufferedReader (NewInputStreamReader (system.in)); //constructing BufferedReader objects from System standard input devicesSystem.out.println ("Client:" +is.readline ()); //print a string read from the client on standard outputLine=Sin.readline (); //reads a string from a standard input while(!line.equals ("Bye") {os.println (line); //output The string to the clientOs.flush (); System.out.println ("Server:" +Line ); System.out.println ("Client:" +is.readline ()); Line=Sin.readline (); }//Continue loopingOs.close ();//close the socket output streamIs.close ();//close the socket input streamSocket.close ();//Close SocketServer.close ();//Close ServerSocket}Catch(Exception e) {System.out.println ("Error:" +e); } }}
Use of sockets