您的位置:首页 > 财经 > 金融 > 新开传奇手游新服网_东莞疫情最新消息今天中高风险区_国家优化防控措施_什么是网络营销平台

新开传奇手游新服网_东莞疫情最新消息今天中高风险区_国家优化防控措施_什么是网络营销平台

2025/4/16 12:43:59 来源:https://blog.csdn.net/2302_79590880/article/details/147155459  浏览:    关键词:新开传奇手游新服网_东莞疫情最新消息今天中高风险区_国家优化防控措施_什么是网络营销平台
新开传奇手游新服网_东莞疫情最新消息今天中高风险区_国家优化防控措施_什么是网络营销平台

摩西摩西~最近接单子用到了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() {@Overridepublic 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() {@Overridepublic 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);

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com