Java Socket编程从零到实战详解

摩西摩西~最近接单子用到了Java的socket编程,顺手给整理下来咯!

各个语言的socket编程除了语法之外几乎思路都是一样的。

所以这些思路都是可以直接移植到其他语言实现的!

话不多说上车!


一、Socket基础概念与工作流程(图解)

(先理解“打电话”模型,再写代码)

1. Socket通信核心模型

 

  • 关键角色
    • 客户端:主动发起连接(类似拨打电话)
    • 服务端:监听端口,等待连接(类似待机电话)
    • Socket对象:连接建立后的数据传输通道(通话线路)

2. 核心流程分解
  1. 服务端:创建ServerSocket → 绑定端口 → 阻塞等待连接(accept()
  2. 客户端:创建Socket → 指定服务端IP和端口 → 发起连接
  3. 双向通信:通过输入流(InputStream)和输出流(OutputStream)收发数据
  4. 关闭连接:调用close()释放资源

二、服务端与客户端基础代码分步解析

(每行代码加注释,新手必看)

1. 服务端基础代码(单线程版)
// 步骤1:创建ServerSocket,绑定端口8080
ServerSocket serverSocket = new ServerSocket(8080);
System.out.println("服务端启动,等待连接...");

// 步骤2:等待客户端连接(阻塞方法,直到有客户端连接)
Socket clientSocket = serverSocket.accept(); 
System.out.println("客户端接入:" + clientSocket.getRemoteSocketAddress());

// 步骤3:获取输入流(接收客户端数据)
InputStream input = clientSocket.getInputStream();
byte[] buffer = new byte[1024];
int len = input.read(buffer);  // 读取数据到buffer数组
String receivedData = new String(buffer, 0, len);
System.out.println("收到消息:" + receivedData);

// 步骤4:发送响应数据
OutputStream output = clientSocket.getOutputStream();
output.write("已收到!".getBytes());

// 步骤5:关闭连接(实际开发中需在finally块处理)
clientSocket.close();
serverSocket.close();
2. 客户端基础代码
// 步骤1:连接服务端(IP+端口)
Socket socket = new Socket("127.0.0.1", 8080);
System.out.println("连接服务端成功!");

// 步骤2:发送数据
OutputStream output = socket.getOutputStream();
output.write("你好,服务端!".getBytes());

// 步骤3:接收响应
InputStream input = socket.getInputStream();
byte[] buffer = new byte[1024];
int len = input.read(buffer);
String response = new String(buffer, 0, len);
System.out.println("服务端响应:" + response);

// 步骤4:关闭连接
socket.close();

三、超时设置详解(解决卡死问题)

(必学技能,避免程序无限等待)

1. 连接超时(防止无法连接时卡死)
Socket socket = new Socket();
// 设置连接超时为5秒(单位:毫秒)
socket.connect(new InetSocketAddress("127.0.0.1", 8080), 5000);  // 
  • 触发场景:服务端未启动或网络不通
  • 异常处理:捕获SocketTimeoutException提示用户检查网络
2. 读取超时(防止数据未到达时阻塞)
socket.setSoTimeout(3000);  // 设置读取超时3秒 
  • 作用范围InputStream.read()操作
  • 异常处理:超时后抛出SocketTimeoutException,可重试或终止
3. 完整超时处理示例
try (Socket socket = new Socket()) {
    // 连接超时5秒
    socket.connect(new InetSocketAddress("127.0.0.1", 8080), 5000);
    
    // 读取超时3秒
    socket.setSoTimeout(3000);
    
    InputStream input = socket.getInputStream();
    // 读取数据...
} catch (SocketTimeoutException e) {
    System.err.println("操作超时:" + e.getMessage());
} catch (IOException e) {
    System.err.println("连接失败:" + e.getMessage());
}

四、心跳机制实现(维持长连接)

(防止长时间无数据导致连接断开)

1. 心跳包原理
  • 作用:定时发送空数据包,告知对方连接存活
  • 实现方式:客户端定时任务 + 服务端超时检测
2. 客户端心跳代码
Timer timer = new Timer();
timer.schedule(new TimerTask() {
    @Override
    public void run() {
        try {
            OutputStream out = socket.getOutputStream();
            out.write(0);  // 发送心跳包(内容可为任意约定标识)
            out.flush();
            System.out.println("心跳发送成功");
        } catch (IOException e) {
            System.err.println("心跳发送失败,连接已断开");
            timer.cancel();  // 停止定时任务
        }
    }
}, 0, 30000);  // 立即启动,每30秒执行一次 
3. 服务端检测心跳
socket.setSoTimeout(45000);  // 超时时间略大于心跳间隔
try {
    InputStream in = socket.getInputStream();
    while (true) {
        int data = in.read();  // 阻塞等待数据
        if (data == 0) {
            System.out.println("收到心跳包");
        }
    }
} catch (SocketTimeoutException e) {
    System.err.println("心跳超时,连接断开");
    socket.close();
}

五、完整实战案例:带超时与心跳的Echo服务

服务端代码(多线程版)
public class EchoServer {
    public static void main(String[] args) throws IOException {
        ExecutorService pool = Executors.newCachedThreadPool();  // 线程池处理并发
        try (ServerSocket server = new ServerSocket(8080)) {
            System.out.println("服务端启动,端口8080");
            while (true) {
                Socket client = server.accept();
                client.setSoTimeout(45000);  // 设置读取超时45秒 
                pool.submit(() -> handleClient(client));
            }
        }
    }

    private static void handleClient(Socket client) {
        try (BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
             PrintWriter out = new PrintWriter(client.getOutputStream(), true)) {
            
            String input;
            while ((input = in.readLine()) != null) {
                if ("HEARTBEAT".equals(input)) {  // 识别心跳包
                    System.out.println("收到心跳包");
                    continue;
                }
                out.println("Echo: " + input);  // 回显消息
            }
        } catch (SocketTimeoutException e) {
            System.err.println("客户端超时未响应,连接关闭");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try { client.close(); } catch (IOException e) {}
        }
    }
}
客户端代码(带心跳与超时)
public class EchoClient {
    public static void main(String[] args) {
        try (Socket socket = new Socket()) {
            // 连接超时5秒
            socket.connect(new InetSocketAddress("127.0.0.1", 8080), 5000);
            // 读取超时3秒
            socket.setSoTimeout(3000);

            // 启动心跳线程(每30秒一次)
            startHeartbeat(socket.getOutputStream());

            Scanner scanner = new Scanner(System.in);
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            while (true) {
                System.out.print("输入消息:");
                String msg = scanner.nextLine();
                out.println(msg);  // 发送消息
                System.out.println("服务端响应:" + in.readLine());
            }
        } catch (SocketTimeoutException e) {
            System.err.println("操作超时:" + e.getMessage());
        } catch (IOException e) {
            System.err.println("连接异常:" + e.getMessage());
        }
    }

    private static void startHeartbeat(OutputStream out) {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    out.write("HEARTBEAT\n".getBytes());  // 发送心跳标识
                    out.flush();
                } catch (IOException e) {
                    timer.cancel();
                }
            }
        }, 0, 30000);
    }
}

六、常见问题与解决方案速查表

问题现象可能原因解决方案
Connection refused服务端未启动或端口错误检查服务端代码是否运行,确认端口一致
Read timed out网络延迟或服务端未及时响应增加超时时间或优化服务端代码
Broken pipe连接已关闭仍尝试写数据发送前检查socket.isClosed(),捕获异常后重连
内存泄漏未关闭Socket或流使用try-with-resources自动关闭资源

七、Java Socket核心方法速查表

方法名所属类功能描述参数说明返回值常见异常使用示例
ServerSocket(int port)ServerSocket创建服务端Socket并绑定指定端口port:监听的端口号(0-65535)BindException(端口被占用)new ServerSocket(8080);
accept()ServerSocket阻塞等待客户端连接,返回通信用的Socket对象Socket(客户端连接对象)IOExceptionSocket client = serverSocket.accept();
close()ServerSocket关闭服务端Socket,释放端口资源IOExceptionserverSocket.close();
Socket(String host, int port)Socket客户端主动连接服务端(构造函数隐式调用connect()host:服务端IP;port:服务端端口UnknownHostException, IOExceptionSocket socket = new Socket("127.0.0.1", 8080);
connect(SocketAddress addr, int timeout)Socket显式连接服务端,可设置超时时间addr:服务端地址;timeout:超时毫秒SocketTimeoutExceptionsocket.connect(new InetSocketAddress("127.0.0.1", 8080), 5000);
getInputStream()Socket获取输入流,用于接收数据InputStreamIOExceptionInputStream in = socket.getInputStream();
getOutputStream()Socket获取输出流,用于发送数据OutputStreamIOExceptionOutputStream out = socket.getOutputStream();
setSoTimeout(int timeout)Socket设置读取超时时间(单位:毫秒),超时后抛出SocketTimeoutExceptiontimeout:超时时间(0表示无限等待)SocketExceptionsocket.setSoTimeout(3000);
setKeepAlive(boolean on)Socket启用/禁用TCP保活机制(默认关闭),自动检测连接是否存活on:true启用,false禁用SocketExceptionsocket.setKeepAlive(true);
shutdownOutput()Socket关闭输出流(发送FIN包),通知对方数据发送完毕,但不关闭SocketIOExceptionsocket.shutdownOutput();
close()Socket关闭Socket连接,释放资源IOExceptionsocket.close();
readInt()DataInputStream从输入流读取4字节的int值(常用于解析长度头)intEOFException, IOExceptionint length = new DataInputStream(in).readInt();
writeInt(int v)DataOutputStream向输出流写入4字节的int值(常用于发送长度头)v:要写入的整数值IOExceptionnew DataOutputStream(out).writeInt(1024);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北岭敲键盘的荒漠猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值