TCP字节流编程实战

8 TCP字节流编程1

编写一个服务器端和一个客户端
服务器端在9999端口监听

客户端和服务器端各有一个socket

在这里插入图片描述

服务端代码
public class SocketTCP01Server {
    public static void main(String[] args) throws IOException {

        //1 本机9999端口监听
        //细节,9999端口未被占用
        //细节:这个serverSocket可以通过accept()返回多个Scket[多个客户端连接]
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("服务端,在9999端口监听,等待连接");

        //2、如果没有客户端连接,程序阻塞,等待连接
        //如果有客户端连接,返回socket对象,程序继续
        Socket socket = serverSocket.accept();
        System.out.println("服务端,socket="+socket.getClass());

        //3 读取客户端写入到数据通道的数据,显示
        InputStream inputStream = socket.getInputStream();
        
        //4 IO读取
        byte[] buf=new byte[1024];
        int readLen=0;
        while ((readLen=inputStream.read(buf))!=-1){
            System.out.println(new String(buf,0,readLen));//根据读取到的实际长度,显示内容
        }

        //5 关闭流和socket
        inputStream.close();
        socket.close();
        serverSocket.close();//关闭
    }
}

客户端代码
public class SocketTCP01Client {
    public static void main(String[] args) throws Exception {
        //1、连接服务端(ip,端口)
        //如果连接成功,但会socket对象
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        System.out.println("客户端 socket返回"+socket.getClass());

        //2、连接上后,生成socket,通过socket.getOutputStream
        OutputStream outputStream = socket.getOutputStream();

        //3、通过输出流,写数据到数据通道
        outputStream.write("hello,server".getBytes());

        //4 关闭流对象
        outputStream.close();
        socket.close();
        System.out.println("客户端退出");
    }
}

9 TCP字节流编程2

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WLwm7gaz-1680705442598)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20230405221240527.png)]

server端代码
@SuppressWarnings({"all"})
public class SocketTCP02Server {
    public static void main(String[] args) throws IOException {

        //1 本机9999端口监听
        //细节,9999端口未被占用
        //细节:这个serverSocket可以通过accept()返回多个Scket[多个客户端连接]
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("服务端,在9999端口监听,等待连接");

        //2、如果没有客户端连接,程序阻塞,等待连接
        //如果有客户端连接,返回socket对象,程序继续
        Socket socket = serverSocket.accept();
        System.out.println("服务端,socket="+socket.getClass());

        //3 读取客户端写入到数据通道的数据,显示
        InputStream inputStream = socket.getInputStream();

        //4 IO读取
        byte[] buf=new byte[1024];
        int readLen=0;
        while ((readLen=inputStream.read(buf))!=-1){
            System.out.println(new String(buf,0,readLen));//根据读取到的实际长度,显示内容
        }

        //5 获取socket相关的输出流
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("hello,client".getBytes());
        //设置结束标记
        socket.shutdownOutput();

        //6 关闭流和socket
        outputStream.close();
        inputStream.close();
        socket.close();
        serverSocket.close();//关闭
    }
}

客户端代码
@SuppressWarnings({"all"})
public class SocketTCP02Client {
    public static void main(String[] args) throws Exception {
        //1、连接服务端(ip,端口)
        //如果连接成功,但会socket对象
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        System.out.println("客户端 socket返回"+socket.getClass());

        //2、连接上后,生成socket,通过socket.getOutputStream
        OutputStream outputStream = socket.getOutputStream();

        //3、通过输出流,写数据到数据通道
        outputStream.write("hello,server".getBytes());

        //设置结束标记
        socket.shutdownOutput();

        //4 获取和socket相关联的输入流,读取数据并显示
        InputStream inputStream = socket.getInputStream();
        byte[] buf=new byte[1024];
        int readLen=0;
        while ((readLen=inputStream.read(buf))!=-1){
            System.out.println(new String(buf,0,readLen));
        }

        //5 关闭流对象
        inputStream.close();
        outputStream.close();
        socket.close();
        System.out.println("客户端退出");
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值