TCP实现聊天

客户端:

1.连接服务器 Socket

2.发送信息

复制代码
//客户端
public class TcpClientDemo01 {
    public static void main(String[] args) {
        Socket socket = null;
        OutputStream os = null;
        try {
            //1.要知道服务器的地址和端口
            InetAddress serverIP = InetAddress.getByName("127.0.0.1");
            int port = 9999;
            //2.创建一个 socket连接
            socket = new Socket(serverIP,port);
            //3.发送消息 IO流
             os = socket.getOutputStream();
            os.write("你好,欢迎学习Java哟".getBytes());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            if (os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}

服务端:

1.建立服务的端口 ServerSocket

2.等待用户的连接 accept

3.接收用户的信息

复制代码
//服务端
public class TcpServerDemo01 {
    public static void main(String[] args) {
        ServerSocket serverSocket =null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        try {
            //1.得有个地址
            serverSocket = new ServerSocket(9999);
            //2.等待客户端连接
             socket = serverSocket.accept();
            //3.读取客户端的消息
             is = socket.getInputStream();
            baos = new ByteArrayOutputStream();
            byte[] bytes = new byte[1024];
            int len;
            while ((len = is.read(bytes)) != -1){
                baos.write(bytes,0,len);
            }
            System.out.println(baos.toString());

        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if (baos != null){
                try {
                    baos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (is != null){
                try {
                    is.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (serverSocket != null) {
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }




        }


    }
}
相关推荐
wanhengidc39 分钟前
私有云的作用都有哪些?
运维·服务器·网络·游戏·智能手机
花阴偷移41 分钟前
Ubuntu 22.04版本下配置静态IP
linux·运维·服务器·tcp/ip·ubuntu
CTO Plus技术服务中1 小时前
71款企业级自研产品,线上演示环境
网络
Bruce_Liuxiaowei1 小时前
2026年5月第4周网络安全形势周报
网络·人工智能·安全·web安全·网络安全·系统安全
HMS工业网络2 小时前
边缘网关网络安全
网络·安全·web安全
AI科技星3 小时前
全域数学·第三部·数术几何部·平行网格卷 完整专著目录(含拓扑发展史+学科定位·终稿)
c语言·开发语言·网络·量子计算·agi
Tassel_YUE3 小时前
超节点技术深度篇三:大模型并行通信拆解:DP、TP、PP、EP、CP 到底在网络里发生了什么
网络·人工智能·数据中心·超节点
xiaoshuaishuai84 小时前
C# 签名异常与Gas预估失败调试方案
开发语言·网络·tcp/ip·c#
其实防守也摸鱼5 小时前
软件安全与漏洞--软件安全编码
java·前端·网络·安全·网络安全·web·工具
XiaoLin laile5 小时前
数据自主可控时代,企业私有化通讯部署价值解析
网络