使用posix_quic-master传输文件

本文详细介绍了如何利用posix_quic-master库进行文件传输。客户端通过修改原函数结合fopen和fread函数实现文件操作,而服务器端则负责读取和存放文件。博客提供了相关函数的参考资料和链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先修改原函数的客户端

#include "quic_socket.h"
#include "debug.h"
#include <string.h>
#include <stdio.h>
#include <string>
#include <sys/epoll.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>

using namespace posix_quic;

#define CHECK_RES(res, api) \
    do {\
        if (res < 0) {\
            perror(api " error");\
            return 1;\
        }\
    } while (0)

bool gIsTestReset = false;
bool gIsTestSocketReset = false;
bool gIsTestSocketResetWithStream = false;

QuicSocket conn = -1;

void onStreamClose() {
    if (gIsTestSocketReset) {
        QuicCloseSocket(conn);
    }
}

int doLoop(QuicEpoller ep) {
    struct epoll_event evs[1024];//
    //收集epoll事件中已经发送的事件
    int n = QuicEpollWait(ep, evs, sizeof(evs)/sizeof(struct epoll_event), 6000);
    //检查n的返回值
    CHECK_RES(n, "epoll_wait");

    int res;
    for (int i = 0; i < n; i++) {
        struct epoll_event & ev = evs[i];
        int fd = ev.data.fd;
        EntryCategory category = GetCategory(fd);//判断监听事件种类

        UserLog("QuicEpoller trigger: fd = %d, category = %s, events = %s",
                fd, EntryCategory2Str((int)category), EpollEvent2Str(ev.events));

        if (ev.events & EPOLLOUT) {
            // connected
            UserLog("Connected.\n");

            if (category == EntryCategory::Socket) {
                QuicStream stream = QuicCreateStream(fd);//创建stream流
                assert(stream > 0);

                struct epoll_event ev;
                ev.data.fd = stream;
                ev.events = EPOLLIN;
                //将stream加入到ep中去
                res = QuicEpollCtl(ep, EPOLL_CTL_ADD, stream, &ev);
                CHECK_RES(res, "epoll_ctl");
              //将hello quic这几个字符串写入到sstream流中去
             //  std::string s = "Hello quic!"; 
        char r_file[]="/home/IPcom/Downloads/posix_quic-master/test/client/test.txt";
        FILE *r_ptr=NULL;
        r_ptr= fopen(r_file,"rb");
        if(r_ptr == NULL)
        {
            printf("error");
            return 0;
        }
        char buffer[1024];
        int length=0;
        while(!feof(r_ptr))
        {
            l
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值