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




        }


    }
}
相关推荐
CHINA红旗下19 分钟前
固定虚拟机的IP地址
运维·服务器·网络
网络研究院19 分钟前
美国网络安全趋势与发展
网络·安全·美国·趋势·发展
着迷不白24 分钟前
十、网络客户端工具curl, wget, ssh, scp, sftp, rsync
运维·网络·ssh
袁小皮皮不皮35 分钟前
6.HCIP OSPF域间防环机制与虚链路
服务器·网络·笔记·网络协议·学习·智能路由器
AI784039 分钟前
安全左移:网络安全从“亡羊补牢”走向“未雨绸缪”
网络·安全·web安全
caimouse1 小时前
Reactos 第 10 章 网络操作 — 10.2 NDIS及其实现
服务器·网络
zhangfeng11331 小时前
国家超算中心 昆山站 异构加速卡1 显存16GB详细配置, 海光 Z100SM HCU
linux·网络·深度学习·c#
青瓦梦滋1 小时前
Linux:TCP协议的socket套接字
网络·网络协议·tcp/ip
烂白菜1 小时前
码道启辰:定时任务自由编排
运维·服务器·网络
梁辰兴1 小时前
计算机网络基础:对称加密密码体制
网络·计算机网络·计算机·对称加密·计算机网络基础·梁辰兴