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




        }


    }
}
相关推荐
我科绝伦(Huanhuan Zhou)5 小时前
Linux 通用压力测试脚本合集:磁盘 + 网络
linux·网络·压力测试
聚美智数6 小时前
网站HTTP状态查询-SEO优化
网络·网络协议·http
treesforest7 小时前
数据包是如何找到回家路的?
网络·智能路由器·nat·ip归属地查询
有毒的教程8 小时前
Linux底层存储路由配
linux·运维·网络
是真的小外套8 小时前
第9章:网络层安全协议 IPSec 学习笔记
网络·网络协议·tcp/ip
云梦谭8 小时前
WebSocket 协议详解:从 HTTP 握手到全双工通信
websocket·网络协议·http
中屹指纹浏览器svip9 小时前
静态 IP和动态 IP,搭配指纹浏览器该怎么选?
网络·网络协议·tcp/ip·中屹指纹浏览器
木子n110 小时前
车载以太网与SOME/IP服务化通信实战-02.状态机与订阅:SOME/IP-SD的三态迁移、重传账与上电风暴
嵌入式硬件·网络协议·车载系统·车载以太网·some/ip
ITxiaobing202310 小时前
选型不踩坑:IPinfo 之外,IP 地址定位服务还有哪些可选项
网络·网络协议·tcp/ip
A心有千千结10 小时前
通过 SSH 反向代理让服务器借用本机网络访问 GitHub 的最佳实践
linux·服务器·网络·git·ssh·github