Java如何创建客户端-服务器套接字通信?

在此示例中,您将看到如何创建客户端-服务器套接字通信。下面的示例包含两个主要类,ServerSocketExample和和ClientSocketExample。服务器应用程序侦听本地主机上的端口7777。当我们从客户端应用程序发送消息时,服务器会接收到该消息并向客户端应用程序发送回复。

在此示例中,使用TCP套接字进行通信,这意味着客户端应用程序和服务器应用程序之间存在固定的连接线。

package org.nhooo.example.network;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.ClassNotFoundException;
import java.lang.Runnable;
import java.lang.Thread;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerSocketExample {
    private ServerSocket server;
    private static final int PORT = 7777;

    private ServerSocketExample() {
        try {
            server = new ServerSocket(PORT);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        ServerSocketExample example = new ServerSocketExample();
        example.handleConnection();
    }

    private void handleConnection() {
        System.out.println("Waiting for client message...");

        // 服务器在此处进行循环以接受由服务器发起的所有连接
        // 客户应用程序。
        while (true) {
            try {
                Socket socket = server.accept();
                new ConnectionHandler(socket);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

class ConnectionHandler implements Runnable {
    private Socket socket;

    ConnectionHandler(Socket socket) {
        this.socket = socket;

        Thread t = new Thread(this);
        t.start();
    }

    public void run() {
        try {
            // 阅读客户端应用程序发送的消息
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            String message = (String) ois.readObject();
            System.out.println("Message Received: " + message);

            // 发送响应信息到客户端应用程序
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject("Hi...");

            ois.close();
            oos.close();
            socket.close();

            System.out.println("Waiting for client message...");
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
package org.nhooo.example.network;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.ClassNotFoundException;
import java.net.InetAddress;
import java.net.Socket;

public class ClientSocketExample {
    public static void main(String[] args) {
        try {
            // 在服务器应用程序上创建到服务器套接字的连接
            InetAddress host = InetAddress.getLocalHost();
            Socket socket = new Socket(host.getHostName(), 7777);

            // 向客户端应用程序发送消息
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
            oos.writeObject("Hello There...");

            // 读取并显示服务器应用程序发送的响应消息
            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            String message = (String) ois.readObject();
            System.out.println("Message: " + message);

            ois.close();
            oos.close();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

要测试该应用程序,您需要启动服务器应用程序。每次您运行客户端应用程序时,它将发送一条消息“ Hello There…”,然后服务器以一条消息“ Hi…”答复。