Java基于Socket的简单聊天室案例

服务端TalkServer如下:

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class TalkServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8081);
        System.out.println("本地地址:" + serverSocket.getLocalSocketAddress());
        System.out.println("服务器已启动!");
        for (; ; ) {
            Socket socket = serverSocket.accept();
            System.out.println(String.format("【客户端连接已创建】,客户端地址:%s", socket.getRemoteSocketAddress()));
            Thread t = new Handler(socket);
            t.start();
        }
    }
}

class Handler extends Thread {
    Socket socket;

    public Handler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            InputStream is = this.socket.getInputStream();
            OutputStream os = this.socket.getOutputStream();
            handle(is, os);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("【客户端已断开连接】");
    }

    private void handle(InputStream inputStream, OutputStream outputStream) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));

        bw.write("连接成功\n");
        bw.flush();
        for (; ; ) {
            String message = br.readLine() + "\n";
            System.out.println(String.format("【收到消息(客户端地址:%s,线程名:%s)】:%s", socket.getRemoteSocketAddress(), Thread.currentThread().getName(),message));
            if (message.equalsIgnoreCase("end")) {
                bw.write(String.format("%s断开连接请求,再见!\n", this.socket.getRemoteSocketAddress()));
                bw.flush();
                System.out.println(String.format("【客户端已断开(客户端地址:%s,线程名:%s)】", socket.getRemoteSocketAddress(), Thread.currentThread().getName()));
                break;
            }

            bw.write("你发送的消息已收到,你发送的消息是:" + message);
            bw.flush();
        }

    }

}

客户端Client

import com.apple.laf.ScreenMenuBar;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class TalkClient {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("127.0.0.1", 8081);
        System.out.println("本地地址:" + socket.getLocalAddress());
        System.out.println("服务器地址:" + socket.getRemoteSocketAddress());
        try (InputStream inputStream = socket.getInputStream()) {
            try (OutputStream outputStream = socket.getOutputStream()) {
                handle(inputStream, outputStream);
            }
        }
        socket.close();
        System.out.println("连接断开");
    }

    private static void handle(InputStream inputStream, OutputStream outputStream) throws IOException {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
        System.out.println("来自服务器的消息:" + reader.readLine());
        Scanner scanner = new Scanner(System.in);
        for (; ; ) {
            System.out.print("请输入你要发送给服务端的消息:");
            String message = scanner.nextLine();
            writer.write(message+"\n");
            writer.flush();
            String response = reader.readLine();
            System.out.println("来自服务端的消息回复:" + response);
            if (message.equalsIgnoreCase("end")) {
                break;
            }
        }
    }
}

你可能感兴趣的:(Java基于Socket的简单聊天室案例)