CLIENTAPP.JAVA original file with the client function:
Import java.net. *; import java.io. *; import java.lang. *;
Public class clientApp {public static void main (string args []) {Try {// Create communication and host Rock connection socket csocket = new socket ("192.168.100.188", 8018); // Open this socket input / output OutputStream OS = CSocket.getOutputStream (); DataInputStream IS = New DataInputStream (csocket.getinputStream ());
INT C; Boolean flag = true;
String responseline;
While (FLAG) {// accepts characters from standard input and writing, such as system while ((c = system.in.read ())! = - 1) {os.write ((byte) c); if (c = = '/ n') {os.flush (); // block the program until the answer information is received after receiving it, it is displayed on the standard output = Is.Readline (); system.out.println ("Message IS: " response);}}} os.close (); is.close (); csocket.close ();
} Catch (exception e) {system.out.println ("Exception:" E.getMessage ());}}}
ServerApp.java original file for server-side features:
Import java.net. *; import java.io. *;
Public class serverapp {public static void main (string args []) {Try {boolean flag = true; socket clientsocket = null; string infutline; int C;
Serversocket SSocket = New Serversocket (8018); System.out.Println ("Server Listen on:" SSOCKET.GETLOCALPORT ());
while (flag) {clientSocket = sSocket.accept (); DataInputStream is = new DataInputStream (new BufferedInputStream (clientSocket.getInputStream ())); OutputStream os = clientSocket.getOutputStream ();
While ((InputLine = Is.Readline ())! = null) {// When the client enters STOP, the server program is running! IF (InputLine.equals ("STOP")) {flag = false; break;} else {system.out.println (InputLine); while ((c = system.in.read ())! = - 1) {OS .write (Byte) C); if (c == '/ n') {os.flush (); // Send the information to the client Break;}}}
} Is.close (); os.close (); clientsocket.close ();
} SSOCKET.CLOSE ();} catch (exception e) {system.out.println ("exception:" E.getMessage ());}}}