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

📄 server.java

📁 Java程序设计实用教程源代码 本书源代码按章分别放置在不同的文件夹中,所有程序均在JDK1.6环境下编译运行正常,除了第13章需要建立ODBC数据源之外,其他程序只要有Java运行环境即可直接运行
💻 JAVA
字号:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Server extends JFrame {
  private JTextField jtf = new JTextField();
  private JTextArea jta = new JTextArea();
  private ObjectOutputStream outstream;
  private ObjectInputStream instream;
  private ServerSocket server;
  private Socket conn;
  private int u = 1; //记录连接客户端数目
  public Server() { //构造方法
    super("服务端");
    Container container = getContentPane();
    jtf.setEnabled(false);
    jtf.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event) {
        sendData(event.getActionCommand());
      }
    });
    container.add(jtf, BorderLayout.NORTH);
    container.add(new JScrollPane(jta), BorderLayout.CENTER);
    setSize(300, 150);
    setVisible(true);
  }
  public void runServer() { //服务端主程序
    try {
      server = new ServerSocket(5000, 100);
      while (true) {
        waitForConnection();
        getStreams();
        processConnection();
        closeConnection();
        ++u;
      }
    }
    catch (EOFException eofException) {
      System.out.println("客户端已中止连接");
    }
    catch (IOException ioException) {
      ioException.printStackTrace();
    }
  }
  private void waitForConnection() throws IOException { //等待客户端的连接
    jta.setText("等待客户端的连接...\n");
    conn = server.accept();
    jta.append("连接" + u + "来自: " + conn.getInetAddress().getHostName());
  }
  private void getStreams() throws IOException { //获取数据流
    outstream = new ObjectOutputStream(conn.getOutputStream());//获取输出流
    outstream.flush();
    instream = new ObjectInputStream(conn.getInputStream());//获取输入流
    jta.append("\n获取I/O流\n");
  }
  private void processConnection() throws IOException { //连接客户端
    String message = "服务端:连接成功!";
    outstream.writeObject(message);
    outstream.flush();
    jtf.setEnabled(true);
    do {
      try {
        message = (String) instream.readObject();
        jta.append("\n" + message);
        jta.setCaretPosition(jta.getText().length());
      }
      catch (ClassNotFoundException classNotFoundException) {
        jta.append("\n接收到未知数据");
      }
    }
    while (!message.equals("客户端:中止连接!"));
  }
  private void closeConnection() throws IOException { //中止连接
    jta.append("\n用户中止连接!");
    jtf.setEnabled(false);
    outstream.close();
    instream.close();
    conn.close();
  }
  private void sendData(String message) { //向客户端发送信息
    try {
      outstream.writeObject("服务端:" + message);
      outstream.flush();
      jta.append("\n服务端:" + message);
    }
    catch (IOException ioException) {
      jta.append("\n输入非法内容");
    }
  }
  public static void main(String args[]) {
    Server server = new Server();
    server.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    server.runServer();
  }
}

⌨️ 快捷键说明

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