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);
                }
            }




        }


    }
}
相关推荐
molaifeng10 小时前
Go 语言如何实现高性能网络 I/O:Netpoller 模型揭秘
开发语言·网络·golang
知乎的哥廷根数学学派12 小时前
基于多模态特征融合和可解释性深度学习的工业压缩机异常分类与预测性维护智能诊断(Python)
网络·人工智能·pytorch·python·深度学习·机器学习·分类
网络工程师_ling12 小时前
【 Elastiflow (ELK) 网络流量分析系统 部署教程】
网络·elk
2301_7807896612 小时前
高防 IP 的选择与配置确保业务稳定性
网络·网络协议·tcp/ip
willhuo13 小时前
基于xray的匿名、授权、IP白名单代理访问研究
服务器·网络·tcp/ip
qiuqyue13 小时前
基于虹软Linux Pro SDK的多路RTSP流并发接入、解码与帧级处理实践
linux·运维·网络
无名38713 小时前
关于 VRF
网络·通信
YounGp_oo14 小时前
一次内网开发环境访问方式的改进实践:使用 FRP 替代远程桌面
网络·ssh·frp·内网穿透·开发环境
云安全干货局14 小时前
服务器被攻击后如何快速恢复?数据备份 + 应急响应手册
网络·网络安全·云服务器·弹性云服务器
猿饵块14 小时前
tcp--抓包--wireshark
网络·测试工具·wireshark