Linux系统TCP传输文件示例

客户端代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    int sockfd, n;
    struct sockaddr_in servaddr;

    if (argc != 2)
    {
        printf("usage: ./client <ipaddress>\n");
        return 0;
    }

    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        return 0;
    }

    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(20002);
    if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
    {
        return 0;
    }
    if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
    {
        return 0;
    }
    FILE *fp;
    int j = 0;
    long length;
    char *sendData;
    if ((fp = fopen("1.exe", "rb")) != NULL)
    {
        fseek(fp, 0, SEEK_END);
        length = ftell(fp);
        sendData = (char *)malloc(length);
        fseek(fp, 0, SEEK_SET);
        for (j = 0; j < length; j++)
        {
            sendData[j] = fgetc(fp);
        }
    }

    if (send(sockfd, sendData, length, 0) < 0)
    {
        return 0;
    }
    printf("send msg length: %ld\n", length);
    close(sockfd);
    fclose(fp);
    free(sendData);
    return 0;
}

服务端代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

#define MAXLINE 1 * 1024 * 1024

int main()
{
    int listenfd, connfd;
    struct sockaddr_in servaddr;

    if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        return 0;
    }

    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(20002);

    if (bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) == -1)
    {
        return 0;
    }

    if (listen(listenfd, 10) == -1)
    {
        return 0;
    }

    printf("waiting for client's request......\n");

    while (1)
    {
        long length_1 = 0;
        int n = 0;
        int nn = 0;
        char *buff = (char *)malloc(1024 * 1024);
        if ((connfd = accept(listenfd, (struct sockaddr *)NULL, NULL)) == -1)
        {
            return 0;
        }
        FILE *fp;
        if ((fp = fopen("xilinx_pcie_3_0_7vx_ep.bit", "wb")) == NULL)
        {
            return 0;
        }
        while ((n = recv(connfd, buff, MAXLINE, 0)) > 0)
        {
            length_1 += n;
            for (size_t i = 0; i < n; i++)
            {
                fputc(buff[i], fp);
            }
        }
        fclose(fp);
        free(buff);
        printf("recv msg from client length:%ld\n", length_1);
        close(connfd);
    }
    close(listenfd);
    return 0;
}

编译运行服务端

abe@abe-virtual-machine:~$ gcc tcp_server.c -o server
abe@abe-virtual-machine:~$ ./server
waiting for client's request......
recv msg from client length:67652352

编译运行客户端

abe@abe-virtual-machine:~$ gcc demo.c -o client
abe@abe-virtual-machine:~$ ./client 127.0.0.1
send msg length: 67652352

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值