客户端发送圆的半径给服务器,服务器生成面积,再发送给客户端
先启动Server,再启动Client
1.Server.java
import java.io.*;
import java.net.*;
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class Server extends JFrame{
private JTextArea jta=new JTextArea();
public Server(){
setLayout(new BorderLayout());
add(new JScrollPane(jta),BorderLayout.CENTER);
setTitle("Server");
setSize(500,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
try {
ServerSocket serverSocket=new ServerSocket(8000);
jta.append("Server started at"+new Date()+'\n');
Socket socket=serverSocket.accept();
DataInputStream inputFromClient=new DataInputStream(socket.getInputStream());
DataOutputStream outputToClient=new DataOutputStream(socket.getOutputStream());
while(true){
double radius=inputFromClient.readDouble();
double area=radius*radius*Math.PI;
outputToClient.writeDouble(area);
jta.append("Radius received from client:"+radius+'\n');
jta.append("Area found:"+area+'\n');
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Server();
}
}
2.Client.java
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
public class Client extends JFrame {
private JTextField jtf = new JTextField();
private JTextArea jta = new JTextArea();
private DataOutputStream toServer;
private DataInputStream fromServer;
public Client(){
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);
try{Socket socket=new Socket("localhost",8000);
System.out.println(socket.getLocalPort());
fromServer=new DataInputStream(socket.getInputStream());
toServer=new DataOutputStream(socket.getOutputStream());
}
catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new Client();
}
private class TextFieldListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try{
double radius=Double.parseDouble(jtf.getText().trim());
toServer.writeDouble(radius);
toServer.flush();
double area=fromServer.readDouble();
jta.append("Radius is "+radius+"\n");
jta.append("Area received from the server is "+area+"\n");
}
catch(IOException ex){
ex.printStackTrace();
}
}
}
}
3.修改server,应用多线程以服务多个客户
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
public class MultiThreadServer extends JFrame {
private JTextArea jta = new JTextArea();
public MultiThreadServer() {
setLayout(new BorderLayout());
add(new JScrollPane(jta), BorderLayout.CENTER);
setTitle("MultiThreadServer");
setSize(500, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
try {
ServerSocket serverSocket = new ServerSocket(8000);
jta.append("MultiThreadServer started at" + new Date() + "\n");
int clientNo = 1;
while (true) {
Socket socket = serverSocket.accept();
jta.append("Starting thread for client " + new Date() + "\n");
InetAddress inetAddress = socket.getInetAddress();
jta.append("Client " + clientNo + "'s host name is"
+ inetAddress.getHostName() + "\n");
jta.append("Client " + clientNo + "'s host address is "
+ inetAddress.getHostAddress() + '\n');
HandleAClient task=new HandleAClient(socket);
new Thread(task).start();
clientNo++;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MultiThreadServer();
}
class HandleAClient implements Runnable {
private Socket socket;
public HandleAClient(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
// TODO Auto-generated method stub
try{
DataInputStream inputFromClient =new DataInputStream(socket.getInputStream());
DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream());
while(true){
double radius=inputFromClient.readDouble();
double area=radius*radius*Math.PI;
outputToClient.writeDouble(area);
jta.append("radius received from client: "+radius+'\n');
jta.append("Area found: "+area +'\n');
}
}
catch(IOException e){
e.printStackTrace();
}
}
}
}
笔记:关键代码
while(true){
Socket socket=serverSocket.accept();
Thread thread =new ThreadClass(socket);
thread.start();
}