⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exercise20_1server.java

📁 一款用java编写的小型数据库管理系统
💻 JAVA
字号:
import java.io.*;
import java.net.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;

public class Exercise20_1Server extends JFrame {
  private JTextArea jta = new JTextArea();
  public static void main(String[] args) {
    new MultiThreadServer();
  }
public Exercise20_1Server() {
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(new JScrollPane(jta), BorderLayout.CENTER);
    setTitle("Exercise20_1Server");
    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 " + clientNo +" at " + new Date() + '\n');
        InetAddress inetAddress = socket.getInetAddress();
        jta.append("Client " + clientNo + "'s host name is " + inetAddress.getHostName() + "\n");
        jta.append("Client " + clientNo + "'s IP Address is "+ inetAddress.getHostAddress() + "\n");
        HandleAClient thread = new HandleAClient(socket);
        thread.start();
        clientNo++;
      }
    }
    catch(IOException ex) {
      System.err.println(ex);
    }
  }
  class HandleAClient extends Thread {
    private Socket socket;
    public HandleAClient(Socket socket) {
      this.socket = socket;
    }
    public void run() {
      try {
        DataInputStream inputFromClient1= new DataInputStream(socket.getInputStream());
        DataInputStream inputFromClient2= new DataInputStream(socket.getInputStream());
        DataInputStream inputFromClient3= new DataInputStream(socket.getInputStream());
        DataOutputStream outputToClient1 = new DataOutputStream(socket.getOutputStream());
        DataOutputStream outputToClient2 = new DataOutputStream(socket.getOutputStream());
        while (true) {
          double Length = inputFromClient1.readDouble();
          double Width=inputFromClient2.readDouble();
          double High=inputFromClient3.readDouble();
          double area = Length * Width ;
          double Volume=area*High;
          outputToClient1.writeDouble(area);
          outputToClient2.writeDouble(Volume);
          jta.append("Area found: " + area + '\n');
          jta.append("Volume found: " +Volume + '\n');
        }
      }
      catch(IOException e) {
        System.err.println(e);
      }
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -