TCP编程代码

1、简单的数据发送

 // 设置连接服务器和端口
        // 打开输出流
        try(Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),8899);
            OutputStream os = socket.getOutputStream();) {
            // 发送数据
            os.write("你好,我是客户端".getBytes());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test2(){
        // 创建服务器端的ServerSocket,指明自己的端口号
        // 调用accept()表示接收来自客户端的socket\
        // 获取输入流
        try(  ServerSocket ss = new ServerSocket(8899);
              Socket socket = ss.accept();
            InputStream is = socket.getInputStream()) {
            /**
             * ByteArrayOutputStream不能用于很长的字符串的缓冲,它在缓冲时会不断的增加内存的使用。
             */
            // 获取数据流的数据
            ByteArrayOutputStream  baos = new ByteArrayOutputStream();   // 起到缓冲的作用。
            byte[] buffer = new byte[5];
            int len;
            while((len = is.read(buffer)) != -1){
                baos.write(buffer,0,len);
            }
            System.out.println(baos.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

在这里插入图片描述
2、发送文件和数据

   @Test
    public void test3(){
        try(Socket socket = new Socket("127.0.0.1",8899);
            OutputStream os = socket.getOutputStream();
            InputStream is = socket.getInputStream();
            FileInputStream fis = new FileInputStream(new File("a.jpg"));) {
            byte[] buffer = new byte[1024];
            int len = -1;
            while((len = fis.read(buffer)) != -1){
                os.write(buffer,0,len);
            }
            socket.shutdownOutput();  // 关闭数据的输出。
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while((len = is.read(buffer)) != -1){
                baos.write(buffer,0,len);
            }
            System.out.println(baos.toString());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test4(){
        try(ServerSocket ss = new ServerSocket(8899);
            Socket socket = ss.accept();
            InputStream is = socket.getInputStream();
            FileOutputStream os = new FileOutputStream(new File("beauty2.jpg"));
            OutputStream oss = socket.getOutputStream();) {
            byte[] buffer = new byte[1024];
            int len = -1;
            while((len = is.read(buffer)) != -1){   // 在输入数据可用、检测到流末尾或者抛出异常前,此方法一直阻塞
                os.write(buffer,0,len);
            }

            oss.write("hello world ! 美女".getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值