Simulating Java Scoket Server and client programming
ImportJava.io.BufferedReader;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.Socket; Public classtalkclient { Public Static voidMain (String args[]) {Try { //make a customer request to port 8000 on this machineSocket socket =NewSocket ("127.0.0.1", 8000); //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 ()); String readline,receive; ReadLine= Sin.readline ();//reads a string from the system standard inputBufferedReader is =NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); while(!readline.equals ("Bye")) { //stop Looping if the string read in from standard input is "bye"os.println (ReadLine); //output A string read from the system standard input to the serverOs.flush (); //refreshes the output stream so that the server receives the string immediatelySYSTEM.OUT.PRINTLN ("Client send:" +ReadLine); //Thread.Sleep (+);Receive =Is.readline (); System.out.println (receive); //reads a string from the server and prints it to the standard outputReadLine = Sin.readline ();//reads a string from the system standard input}//Continue loopingOs.close ();//close the socket output streamIs.close ();//close the socket input streamSocket.close ();//Close Socket}Catch(Exception e) {} }}
ImportJava.io.BufferedReader;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public classTalkserver { Public Static voidMain (String args[]) {Try{serversocket server=NULL; Try{Server=NewServerSocket (8000); //Create a ServerSocket to listen for customer requests on port 8000}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); //error, printing error message} 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 constructedline =Is.readline (); //reads a string from a standard input while(!line.equals ("Bye")) { //If the string is "Bye", the loop is stoppedOS.PRINTLN ("Server Send:yeah," +Line ); //output The string to the clientOs.flush (); //refreshes the output stream so that the client receives the string immediatelySystem.out.println ("Server Receive:" +Line ); //reads a string from the client and prints it to the standard outputline =Is.readline (); //reads a string from the system standard input}//Continue loopingOs.close ();//close the socket output streamIs.close ();//close the socket input streamSocket.close ();//Close SocketServer.close ();//Close ServerSocket}Catch(Exception e) {} }}
Java Socket Programming