Client Package test;import java.io.*;import java.net.*;import java.applet.applet;public class talkserver{public static void main (string args[]) {try{ServerSocket server=null;try{server=new serversocket (4700);//Create a serversocket on port 4700 to listen for client requests}catch (exception e) {system.out.println ("Can not listen to:" +e);//error, print error message}socket socket=null;try{ Socket=server.accept ();//Use the Accept () block to wait for a customer request, a client//request arrives to produce a socket object, and continue to execute}catch (exception e) { System.out.println ("Error.") +E);//error, print error message}string line; Bufferedreader is=new bufferedreader (New inputstreamreader (Socket.getinputstream ()));// The input stream is obtained by the socket object and the corresponding BufferedReader object Printwriter os=new printwriter (Socket.getoutputstream ()) is constructed; The output stream is obtained by the socket object and constructs the PrintWriter object Bufferedreader sin=new bufferedreader (New inputstreamreader ( system.in));//BufferedReader Object System.out.println ("Client:" +is.readline ()) constructed by the system standard input device;//in standard outputPrint the string read-in from the client Line=sin.readline ();//Read a string from the standard input while (!line.equals ("Bye")) {//If the string is "Bye", Stop the Loop os.println ( line);//Output the string Os.flush () to the client,//refresh the output stream so that the client immediately receives the string System.out.println ("Server:" +line);// Print the read-in string System.out.println ("Client:" +is.readline ()) on the system standard output;//Read a string from the Client and print to the standard output line=sin.readline ();// Reads a string from the system standard input}//continues the loop os.close (); //closes the socket output stream is.close (); //closes the socket input stream socket.close (); // Close Socketserver.close (); //close Serversocket}catch (exception e) {System.out.println ("error:" +e);//Error, Print error message}}}} //server package test;import java.io.*;import java.net.*;p ublic class Talkclient {public static void main (string args[]) {try{socket socket=new socket ("127.0.0.1", 4700);//Shanben 4700 Port issuing customer request Bufferedreader sin=new bufferedreader (new InputStreamReader (system.in));//The BufferedReader object is constructed by the system standard input device Printwriter os=new printwriter ( Socket.getoutputstream ());//The output stream is obtained by the socket object, and the PrintWriter object is constructed BuffeRedreader is=new bufferedreader (New inputstreamreader (Socket.getinputstream ()));// The input stream is obtained by the socket object, and the corresponding BufferedReader object is constructed String readline;readline=sin.readline (); //reads a string from the system standard input while (!). Readline.equals ("Bye")) {//If the string read in from the standard input is "Bye" then stop the Loop os.println (readline);//Output The string read from the system standard input to Serveros.flush ( );//flushes the output stream so that the Server receives the string System.out.println ("Client:" +readline);//Prints the read-in string System.out.println on the system standard output ("Server:" +is.readline ());//reads a string from the server and prints it to the standard output readline=sin.readline (); //reads a string from the system standard input} //continues the loop os.close (); //close the socket output stream is.close (); //close the socket input stream socket.close (); //close Socket}catch (exception e) { system.out.println (e);//error, print error message}}}
Java Socket Programming Simple sample (client + server)