Linux Linux程序练习十(网络编程大文件发送)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux Linux程序练习十(网络编程大文件发送)相关的知识,希望对你有一定的参考价值。

//网络编程客户端--大文件传输
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int arg,char *args[])
{
    if(arg<4)
    {
        printf("please print three param !\n");
        return -1;
    }
    int port=atoi(args[2]);
    //create socket
    int st=socket(AF_INET,SOCK_STREAM,0);
    if(st==-1)
    {
        printf("create socket failed ! error message :%s\n",strerror(errno));
        return -1;
    }
    //defien IP address
    struct sockaddr_in addr;
    //init addr
    memset(&addr,0,sizeof(addr));
    addr.sin_family=AF_INET;
    addr.sin_port=htons(port);
    addr.sin_addr.s_addr=inet_addr(args[1]);
    if(connect(st,(struct sockaddr *)&addr,sizeof(addr))==-1)
    {
        printf("connect failed ! error message :%s\n",strerror(errno));
        goto END;
    }
    //send file

    //define file stream
    FILE * pfr=NULL;
    //open the file in read mode
    pfr=fopen(args[3],"r");
    if(pfr==NULL)
    {
        printf("open the file failed !error message :%s\n",strerror(errno));
        goto END;
    }
    char buf[1024]={0};
    size_t num=0;
    while((num=fread(buf,sizeof(char),sizeof(buf),pfr))>0)
    {
        //send part of the file
        if(send(st,buf,sizeof(char)*num,0)==-1)
        {
            printf("send failed !error message :%s\n",strerror(errno));
            break;
        }
        memset(buf,0,sizeof(buf));
    }
    fclose(pfr);
    END:close(st);
    return 0;
}
//网络编程服务端--发送大文件
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int arg, char *args[])
{
    if (arg < 2)
    {
        printf("please print two param !\n");
        return -1;
    }
    int port = atoi(args[1]);
    int st = socket(AF_INET, SOCK_STREAM, 0);
    if (st == -1)
    {
        printf("create socket failed ! error message :%s\n", strerror(errno));
        return -1;
    }
    //defien IP address
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
    //bind port
    if (bind(st, (struct sockaddr *) &addr, sizeof(addr)) == -1)
    {
        printf("bind failed ! error message :%s\n", strerror(errno));
        goto END;
    }
    //listen
    if (listen(st, 20) == -1)
    {
        printf("listen failed ! error message :%s\n", strerror(errno));
        goto END;
    }
    //只接收一个用户连接
    int clientst = 0;
    struct sockaddr_in client_addr;
    memset(&client_addr, 0, sizeof(client_addr));
    size_t client_addrlen = sizeof(client_addr);
    clientst = accept(st, (struct sockaddr *) &client_addr, &client_addrlen);
    if (clientst == -1)
    {
        printf("accept failed ! error message :%s\n", strerror(errno));
        goto END;
    }
    //recv message
    char buf[1024] = { 0 };
    int mflag = 0;
    //open the file stream
    FILE * pfa = NULL;
    pfa = fopen("/home/test/2/1.dat", "a");
    if (pfa == NULL)
    {
        printf("open the file failed ! error message :%s\n", strerror(errno));
        goto END;
    }
    while (1)
    {
        mflag = recv(clientst, buf, sizeof(buf), 0);
        if (mflag == 0)
        {
            printf("client is closed!\n");
            break;
        } else if (mflag == -1)
        {
            printf("recv message is failed ! error message :%s\n",
                    strerror(errno));
            break;
        }
        //将文件写到当前程序目录下
        fwrite(buf, sizeof(char), mflag, pfa);
        memset(buf, 0, sizeof(buf));
    }
    fclose(pfa);
    END: close(st);
    return 0;
}
.SUFFIXES:.c .o
CC=gcc
SRCS1=mclient.c
SRCS2=mserver.c
OBJS1=$(SRCS1:.c=.o)
OBJS2=$(SRCS2:.c=.o)
EXEC1=mcl
EXEC2=mser

start:$(OBJS1) $(OBJS2)
    $(CC) -o $(EXEC1) $(OBJS1)
    $(CC) -o $(EXEC2) $(OBJS2)
    @echo "-------ok-----------"
.c.o:
    $(CC) -Wall -g -o [email protected] -c $<
clean:
    rm -f $(OBJS1)
    rm -f $(EXEC1)
    rm -f $(OBJS2)
    rm -f $(EXEC2)

 

以上是关于Linux Linux程序练习十(网络编程大文件发送)的主要内容,如果未能解决你的问题,请参考以下文章

Linux Linux程序练习十三(信号阻塞,捕获)

Linux Linux程序练习十七

Linux Linux程序练习十四(多进程压力测试)

Linux Linux程序练习十二(select实现QQ群聊)

Linux Linux程序练习十九

Linux Linux程序练习十五(进程间的通信共享内存版)