This example gives a client program and a server program. The client sends data to the server. The server receives the data, uses it to generate a result, and then returns the result to the client. The client displays the results on the console. In this case, the data sent by the client is the radius of the circle, and the result of the server is the area of the circle.
The client sends a RADIUS through the dataouputstream of the output stream socket, and the server receives the RADIUS through the datainputstream of the input stream socket. The server calculates the area and then sends it to the client by dataoutputstream the output stream socket, and the client receives the area by the datainputstream of the input stream socket. The server and client programs are given in the following list of programs.
Server.java
Packagesrc;Importjava.awt.BorderLayout;ImportJava.io.DataInputStream;ImportJava.io.DataOutputStream;Importjava.io.IOException;ImportJava.net.ServerSocket;ImportJava.net.Socket;Importjava.util.Date;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;ImportJavax.swing.JFrame;ImportJavax.swing.JScrollPane;ImportJavax.swing.JTextArea; Public classServerextendsJFrame {PrivateJTextArea Jta=NewJTextArea (); PrivateExecutorservice executor=Executors.newcachedthreadpool (); Public Static voidMain (string[] args) {NewServer (); } PublicServer () {setlayout (NewBorderLayout ()); Add (NewJScrollPane (JTA), borderlayout.center); Settitle ("Server"); SetSize (500, 300); Setdefaultcloseoperation (Jframe.exit_on_close); SetVisible (true); Try{serversocket ServerSocket=NewServerSocket (8000); Jta.append ("Server started at" +NewDate () + ' \ n '); while(true) {Socket Socket=serversocket.accept (); Executor.execute (NewTask (socket));//docking A new connection received, starting a new thread } } Catch(IOException ex) {System.err.println (ex); } finally{executor.shutdown (); } } Private classTaskImplementsrunnable{Privatesocket socket; PublicTask (socket socket) { This. socket=socket; } @Override Public voidRun () {//perform a response to the client in the thread//TODO auto-generated Method Stub Try{datainputstream inputfromclient=NewDataInputStream (Socket.getinputstream ()); DataOutputStream outputtoclient=NewDataOutputStream (Socket.getoutputstream ()); while(true){ Doubleradius=inputfromclient.readdouble (); Doublearea=radius*radius*Math.PI; Outputtoclient.writedouble (area); Jta.append ("######## from client [" +socket.getinetaddress () + ":" +socket.getport () + "]-radius =" +radius+ "\ n"); Jta.append ("******** area is----" +area+ ' \ n '); } } Catch(IOException ex) {System.err.println (ex); } } }}
Client.java
Packagesrc;Importjava.awt.BorderLayout;Importjava.awt.event.ActionEvent;ImportJava.awt.event.ActionListener;ImportJava.io.DataInputStream;ImportJava.io.DataOutputStream;Importjava.io.IOException;ImportJava.net.Socket;ImportJavax.swing.JFrame;ImportJavax.swing.JLabel;ImportJavax.swing.JPanel;ImportJavax.swing.JScrollPane;ImportJavax.swing.JTextArea;ImportJavax.swing.JTextField; Public classClientextendsJFrame {PrivateJTextField jtf=NewJTextField (); PrivateJTextArea Jta=NewJTextArea (); PrivateDataOutputStream Toserver; PrivateDataInputStream fromserver; Privatesocket socket; Public Static voidMain (String args[]) {NewClient (); } PublicClient () {JPanel panel=NewJPanel (); Panel.setlayout (NewBorderLayout ()); Panel.add (NewJLabel ("Enter radius"), borderlayout.west); Panel.add (JTF, Borderlayout.center); Jtf.sethorizontalalignment (Jtextfield.right); SetLayout (NewBorderLayout ()); Add (Panel,borderlayout.north); Add (NewJScrollPane (JTA), borderlayout.center); Jtf.addactionlistener (NewTextfieldlistener ());//some thing wrongSettitle ("Client"); SetSize (500, 300); Setdefaultcloseoperation (Jframe.exit_on_close); SetVisible (true); Try{Socket=NewSocket ("localhost", 8000); Fromserver=NewDataInputStream (Socket.getinputstream ()); Toserver=NewDataOutputStream (Socket.getoutputstream ()); } Catch(IOException ex) {Jta.append (ex.tostring ()+ ' \ n '); } } Private classTextfieldlistenerImplementsactionlistener{@Override Public voidactionperformed (ActionEvent e) {//TODO auto-generated Method Stub Try{ Doubleradius=double.parsedouble (Jtf.gettext (). Trim ()); Toserver.writedouble (RADIUS); Toserver.flush (); DoubleArea=fromserver.readdouble (); Jta.append ("-----------Radius is" +radius+ ' \ n '); Jta.append ("$$$$$$$$$$$ from Server [" +socket.getinetaddress () + ":" +socket.getport () + "] area=" +area+ ' \ n '); } Catch(IOException ex) {System.err.println (ex); } } }}
Operating effect:
Java Learning Note (3)----Network socket server multithreaded version