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




        }


    }
}
相关推荐
今儿敲了吗8 小时前
CN——数据链路层(下)
网络·笔记
你怎么知道我是队长8 小时前
计算机网络入门指南:从OSI模型到网络设备
网络·计算机网络
K成长日志8 小时前
BLE链路层--比特流处理
网络·物联网·网络协议·蓝牙·iot·ble·无线
怪奇云呼军9 小时前
闪电智能VoiceAgent 如何管理呼入、接听、桥接和挂断状态?
java·前端·网络·数据库·人工智能
x_x-F9 小时前
DMA技术详解:解放CPU的数据搬运工
网络
xiaoxiangsiyan9 小时前
RHCE之2026年全套考点详解
运维·前端·网络·chrome·学习·rhce
秋田君9 小时前
QT_实战TCP 聊天程序说明文档以及实现
数据库·qt·tcp/ip
腾科IT教育9 小时前
网络安全与网络空间安全:概念、产业与防护建议
网络·安全·web安全·网络安全·网络空间安全
0Kilobyte9 小时前
自建拨号服务器上网
网络
不悔哥10 小时前
Tinyftp 源码学习笔记:3000 行 C 实现的 FTP 服务器
服务器·c语言·网络·tcp/ip