Service-Side code:
PackageCom.bobohe.socket;ImportJava.io.*; Importjava.net.*; ImportJava.applet.Applet; 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); //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 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 output Line=Sin.readline (); //reads a string from a standard input while(!line.equals ("Bye")) { //If the string is "Bye", the loop is stoppedos.println (line); //output The string to the clientOs.flush (); //refreshes the output stream so that the client receives the string immediatelySystem.out.println ("Server:" +Line ); //Print the Read-in string on the system standard outputSystem.out.println ("Client:" +is.readline ()); //reads a string from the client and prints it to the standard output Line=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 Socketserver.close ();//Close ServerSocket } Catch(Exception e) {System.out.println ("Error:" +e); //error, printing error message } } }
Client code:
PackageCom.bobohe.socket;ImportJava.io.*; Importjava.net.*; Public classtalkclient { Public Static voidMain (String args[]) {Try{Socket Socket=NewSocket ("127.0.0.1", 4700); //make a customer request to port 4700 on this machineBufferedReader sin=NewBufferedReader (NewInputStreamReader (system.in)); //constructing BufferedReader objects from System standard input devicesprintwriter os=NewPrintWriter (Socket.getoutputstream ()); //The output stream is obtained by the socket object, and the PrintWriter object is constructedBufferedReader is=NewBufferedReader (NewInputStreamReader (Socket.getinputstream ())); //The input stream is obtained by the socket object and the corresponding BufferedReader object is constructedString ReadLine; ReadLine= Sin.readline ();//reads a string from the system standard input 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:" +ReadLine); //Print the Read-in string on the system standard outputSystem.out.println ("Server:" +is.readline ()); //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) {System.out.println ("Error" + e);//Error, print error message } } }
170410. Simple example of Java socket Communication (TCP)