java 网络(socket)

本文梳理一个基础的java TCP消息通信,构造一个简单的Packet进行传输,代码如下:

  • Packet
复制代码
public class Packet {
    private String attribute;
    
    public Packet(String attr){
        this.attribute = attr;
    }
    
    public String getAttribute() {
        return attribute;
    }
    public void setAttribute(String attribute) {
        this.attribute = attribute;
    }
}
复制代码
  • 服务器端代码
复制代码
public class App {
    
    private static final int PORT = 4000;

    public static void main(String[] args) throws IOException {
        
        ServerSocket listen = new ServerSocket(PORT);
        Socket          client = null;
        while(true){
            client = listen.accept();
            new Thread(new ServerThread(client)).start();
        }
    }
}
复制代码
复制代码
public class ServerThread implements Runnable{
    
    private static final int BUFSIZE  =1024;
    private Socket client = null;
    
    public ServerThread(Socket client){
        this.client = client;
    }
    
    @Override
    public void run() {
        try {
            InputStream in      = client.getInputStream();
            OutputStream out = client.getOutputStream();
             
            byte[] buffer = new byte[BUFSIZE];
                   
            while (in.read(buffer) != -1) {
                //判断包头
                if(buffer[0] == 0x01 && buffer[1] == 0x02){
                    byte[] tmp = new byte[BUFSIZE];
                    int index = 0;
                    for(int i = 2; i < buffer.length; i++){
                        tmp[index++] = buffer[i];
                    }
                    
                    Packet packet = new Packet(new String(tmp,"GB2312"));
                    
                    System.out.println(packet.getAttribute());
                }else{
                    System.out.println("消息格式不正确");
                }
            }
            
            out.close();
            in.close();
            
        } catch (IOException e) {
            e.printStackTrace();
        }    
    }
}
复制代码
  • 客户端代码
复制代码
public class App {
    
    private static final int PORT = 4000;
    private static final int BUFSIZE  =1024;

    public static void main(String[] args) throws IOException {
        
        Socket client = new Socket("127.0.0.1", PORT);
        
        InputStream in      = client.getInputStream();
        OutputStream out = client.getOutputStream();
        
        Packet packet = new Packet("类型参数2345");
            
        byte[] output = new byte[BUFSIZE];
        output[0] = 0x01;
        output[1] = 0x02;
        
        int index = 2;
        //若增加包长度字段,则可实现packet的分包、组包
        byte[] tmp1 = packet.getAttribute().getBytes();
        for(int i= 0; i < tmp1.length; i++){
            output[index++] = tmp1[i];
        }
                                    
        out.write(output);
        
        byte[] buffer = new byte[BUFSIZE];
        while (in.read(buffer) != -1) {  
            System.out.println(new String(buffer, "GB2312"));
        }
        
        out.close();
        in.close();
        client.close();
            
    }
}
复制代码
posted @   Fredric_2013  阅读(151)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示