linux socket 传输大文件解决方案

思路: 1  服务器端 把大文件分包 每一个包大小建立一个socket(对应一个线程)进行传输

           2 客户端 对每一个包对应一个线程(同时对应一个socket)进行接收

      3 在发送每一个包时 包前要发送这个包的大小 和对应的偏移量

      4  在32位系统中要开启大文件的宏开关

      5 利用pread pwrite 可以保证原子操作

废话不多说 直接附上源码 测试结果 在局域网中 能达到10Mb/s 若有问题 欢迎直接交流 qq:294178101

/*
*******************
server.cpp 源代码 by 海南之恋
说明: 在32系统中运行  
*******************

*/
//#define _XOPEN_SOURCE 500  
#define _FILE_OFFSET_BITS 64  //开启大文件支持宏开关
//#define _LARGEFILE64_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>



#define BUFFER_SIZE 4096
#define MAX_LISTEN 2048

typedef struct
{
	long long cur;
	int size;
	int sockfd;
}thread_data;
int filefd;
int file_block_size = 0;

void* sender(void* s)
{
    thread_data tdata = *((thread_data*)s);
    long long cur = tdata.cur;
    int size = tdata.size;
    int connfd = tdata.sockfd;
    char buf[BUFFER_SIZE] = {0};
    int read_count;
    char head_buf[29] = {0};
	snprintf(head_buf,29,"%016lld:%011d",cur,size);
    send(connfd, &head_buf, strlen(head_buf), 0);
	long long  read_size = 0;
    while (size)
    {    // (sizeof(buf) < size)?sizeof(buf):size 最后一次读取的时候 千万不要多读哦
        read_count = pread(filefd, buf,(sizeof(buf) < size)?sizeof(buf):size, cur + read_size);
        if (read_count < 0 && errno == EINTR)
        {                           //pread 是原子操作 这样的话可以多个线程同时读写一个文件
            puts("break by signal");
			continue;
        }
        else if (read_count == 0)
        {
            break;
        }
		else if(read_count < 0)
		{
			perror("pread"); exit(1);
		}
        send(connfd, buf, read_count, 0);
        size -= read_count;
        read_size += read_count;
    }
    close(connfd);
	printf("cur = %lld, end\n",tdata.cur);
    free(s); //要释放掉哦
	
    pthread_exit((void*)connfd);
}

int main(int a
  • 2
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值