0314网络编程

头文件

#ifndef __HEAD_H__
#define __HEAD_H__
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

#define ERR_MSG(msg) do{\
    fprintf(stderr,"line:%d",__LINE__);\
    perror(msg);\
}while(0)

#define PORT 69
#define IP "192.168.31.154"

int do_download(int sfd,struct sockaddr_in sin);

#endif

功能函数

#include "head.c"
int do_download(int sfd,struct sockaddr_in sin)
{
    int ret_val=0;
    char buf[516]="";
    char filename[20]="";
    printf("请输入下载文件名>>>");
    scanf("%s",filename);
    while(getchar()!=10);

    int size =sprintf(buf,"%c%c%s%c%s%c",0,1,filename,0,"octet",0);
    if(sendto(sfd,buf,size,0,(struct sockaddr*)&sin,sizeof(sin))<0)
    {
        ERR_MSG("sendto");
        return -1;
    }

    int fd =-1;        
    socklen_t addrlen =sizeof(sin);
    ssize_t res=0;
    unsigned short num=0;

    while(1)
    {
        bzero(buf,sizeof(buf));
        res=recvfrom(sfd,buf,sizeof(buf),0,(struct sockaddr*)&sin,&addrlen);
        if(res<0)
        {
            ERR_MSG("recvfrom");
            ret_val= -2;
            break;
        }


        if(buf[1]==3)
        {
            if((num+1)==htons(*(unsigned short*)(buf+2)))
            {
                num++;
                if(-1==fd)
                {
                    fd=open(filename,O_WRONLY|O_CREAT|O_TRUNC,0664);
                    if(fd<0)
                    {
                        ERR_MSG("open");
                        ret_val= -3;
                        break;
                    }
                }

                if(write(fd,buf+4,res-4)<0)
                {
                    ERR_MSG("write");
                    ret_val= -4;
                    break;
                }

                buf[1]=4;
                if(sendto(sfd,buf,4,0,(struct sockaddr*)&sin,sizeof(sin))<0)
                {
                    ERR_MSG("sendto");
                    ret_val= -5;
                    break;
                }

                if(res-4<512)
                {
                    printf("*****文件下载完毕******\n");
                    break;
                }
            }
        }
        else if(buf[1]==5)
        {
            printf("MSG_ERR:code[%d] msg[%s] __%d__",\
                    htons(*(unsigned short*)(buf+2)),buf+4,__LINE__);
            ret_val= -6;
            break;
        }
    }
    close(sfd);
    return ret_val;
}

int do_upload(int sfd,struct sockaddr_in sin)
{
    int block=0;
    int ret_val=0;
    char buf[516]="";
    char filename[20]="";
    printf("请输入上传文件名>>>");
    scanf("%s",filename);
    while(getchar()!=10);

    int size =sprintf(buf,"%c%c%s%c%s%c",0,2,filename,0,"octet",0);
    if(sendto(sfd,buf,size,0,(struct sockaddr*)&sin,sizeof(sin))<0)
    {
        ERR_MSG("sendto");
        return -7;
    }


    ssize_t res=0;
    ssize_t rec=0;
    unsigned short num=0;

    socklen_t addrlen =sizeof(sin);

    FILE* fd=fopen(filename,"r");
    if(fd==NULL)
    {
        ERR_MSG("fopen");
        return -8;
    }
    while(1)
    {block++;
        bzero(buf,sizeof(buf));
        res=recvfrom(sfd,buf,4,0,(struct sockaddr*)&sin,&addrlen);
        if(res<0)
        {
            ERR_MSG("recvfrom");
            ret_val= -11;
            break;
        }
        if(buf[1]==4)
        {
            rec=fread(buf+4,sizeof(buf)-4,1,fd);
            if(rec<0)
            {
                ERR_MSG("fread");
                ret_val =-9;
                break;
            }
            buf[1]=3;
            buf[3]=block;
            if(sendto(sfd,buf,rec+4,0,(struct sockaddr*)&sin,sizeof(sin))<0)
            {
                ERR_MSG("sendto");
                ret_val = -10;
                break;
            }    

            if(rec<512)
            {
                ERR_MSG("fread");
                ret_val=-11;
                break;
            }
        }
        else if(buf[1]==5)
        {
            printf("MSG_ERR:code[%d] msg[%s] __%d__",\
                    htons(*(unsigned short*)(buf+2)),buf+4,__LINE__);
            ret_val= -6;
            break;
        }
    }
    fclose(fd);
    close(sfd);
    return ret_val;
}

主函数

#include "head.c"
int main(int argc, const char *argv[])
{
    int sfd=socket(AF_INET,SOCK_DGRAM,0);
    if(sfd<0)
    {
        ERR_MSG("socket");
        return -1;
    }
    struct sockaddr_in sin;
    sin.sin_family     = AF_INET;
    sin.sin_port     = htons(PORT);
    sin.sin_addr.s_addr= inet_addr(IP);
    
    char choose=0;
    while(1)
    {
        printf("~~~~~~~~~~~~~~~~~~~~~~\n");
        printf("~~~~~~~1,下载~~~~~~~~\n");
        printf("~~~~~~~2,上传~~~~~~~~\n");
        printf("~~~~~~~3,退出~~~~~~~~\n");
        printf("~~~~~~~~~~~~~~~~~~~~~~\n");
        printf("请操作:");
        choose=getchar();
        while(getchar()!=10);

        switch(choose)
        {
        case '1':do_download(sfd,sin);
            break;
        //case '2':do_upload();
            break;
        case '3':
            goto END;
            break;
        default:
            printf("输入错误,请再次输入:");
            break;
        }
    }
END:
    close(sfd);
    return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值