Java supports stream-based communication and packet-based communication,and the first is universal.
Create ServerSocket
1.Create ServerSocket
ServerSocket serverSocket=new ServerSocket(port);
2.Listen to the connect
Socket socket=serverSocket.accept();
3.We need create InputStream and OutputStream to transmit data
InputStream inputFromClient=socket.getInputSream();
OutputStream outputToClient=socket.getOutputStream();
(we can also use DataInputStream and DataOutputStream ,BufferedReader and PrintWriter to get the data types are int,double and string.)
Create client Socket
1.Create client Socket
Socket socket=new Socket(serverName,port);
(the serverName can be host name or the host IP address.)
2.We need create InputStream and OutputStream to transmit data
InputSream inputFromServer=socket.getInputStream();
OutputStream outputToServer=socket.getOutputStream();
(we can also use DataInputStream and DataOutputStream ,BufferedReader and PrintWriter to get the data type are int,double or string.)
Now,there is an example that Client send radius to Server and Server compute area and return to Client.
Server.java
import java.awt.BorderLayout;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Server extends JFrame {
private JTextArea jta=new JTextArea();
public static void main(String[] args) {
new Server();
}
public Server() {
setLayout(new BorderLayout());
add(new JScrollPane(jta),BorderLayout.CENTER);
setTitle("Server");
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
try{
//Create a server socket
ServerSocket serverSocket=new ServerSocket(8000);
jta.append("Server started at "+new Date() +"\n");
//Listen for a connection request
Socket socket=serverSocket.accept();
//Create data input and output streams
DataInputStream inputFromClient=new DataInputStream(socket.getInputStream());
DataOutputStream outputToClient=new DataOutputStream(socket.getOutputStream());
while (true) {
//Receive radius from the client
double radius=inputFromClient.readDouble();
//Compute area
double area=radius*radius*Math.PI;
//Send area back to the client
outputToClient.writeDouble(area);
jta.append("Radius received from client:"+radius +‘\n‘);
jta.append("Area found:" +area+‘\n‘);
}
}
catch(IOException ex){
System.err.println(ex);
}
}
}
Client.java
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Client extends JFrame {
//Text filed for receiving radius
private JTextField jtf=new JTextField();
//Text area to display contents
private JTextArea jta=new JTextArea();
//IO streams
private DataOutputStream toServer;
private DataInputStream fromServer;
public static void main(String[] args) {
new Client();
}
public Client(){
//Panel panel to hold the label and text field
JPanel panel=new JPanel();
panel.setLayout(new BorderLayout());
panel.add(new JLabel("Enter radius"),BorderLayout.WEST);
panel.add(jtf,BorderLayout.CENTER);
jtf.setHorizontalAlignment(JTextField.RIGHT);
setLayout(new BorderLayout());
add(panel,BorderLayout.NORTH);
add(new JScrollPane(jta),BorderLayout.CENTER);
jtf.addActionListener(new TextFieldListener());
setTitle("Client");
setSize(500,300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);//It is necessary to show the frame here
try {
//Create a socket to connect to the server
Socket socket=new Socket("localhost",8000);
//Socket socket =new Socket("130.254.204.36",8000);
//Socket socket =new Socket("drake.Armstrong.edu",8000);
System.out.println("local port: "+socket.getLocalPort());
//Test InetAddress
InetAddress inetAddress=socket.getInetAddress();
System.out.println("Client‘s host name is "+inetAddress.getHostName());
System.out.println("Client‘s IP Address is "+inetAddress.getHostAddress());
//
//Create an input stream to receive data from the server
fromServer=new DataInputStream(socket.getInputStream());
//Create an output stream to send data from the server
toServer=new DataOutputStream(socket.getOutputStream());
} catch (IOException ex) {
jta.append(ex.toString()+‘\n‘);
}
}
private class TextFieldListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
try {
//get the radius from the text field
double radius=Double.parseDouble(jtf.getText().trim());
//Send the radius to the server
toServer.writeDouble(radius);
toServer.flush();
//Get area from the server
double area=fromServer.readDouble();
//display to the text server
jta.append("Radius is "+radius+"\n");
jta.append("Area received from the server is "+area +‘\n‘);
} catch (IOException ex) {
System.err.println(ex);
}
}
}
}