我是靠谱客的博主 结实刺猬,最近开发中收集的这篇文章主要介绍LINUX下实现UDP传输文件,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

客户端使用sendto
服务器端使用recv

UDP要想用send需要先使用connect

客户端代码

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

#define BUFFER_SIZE 1024

int main()
{
    int sockcd;
    struct sockaddr_in server;
    char recv[4096],send1[4096];
    char filepath[100];//file to translate

    FILE *fp;
    int lenpath; //filepath length
    char *buffer;//file buffer
    int fileTrans;
    buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE);
    bzero(buffer,BUFFER_SIZE); 
    //memset(buffer,0,sizeof(buffer));

    if((sockcd = socket(AF_INET,SOCK_DGRAM,0))<0)
    {
        printf("socket build error!n");
    }
    memset(&server,0,sizeof(server));
    server.sin_family= AF_INET;
    server.sin_port = htons(9999);
    if(inet_pton(AF_INET,"192.168.61.143",&server.sin_addr)<0)
    {
        printf("inet_pton error!n");
    }
    while(1)
    {
        printf("input filepath:n");
        //memset(filepath,'',sizeof(filepath));
        scanf("%s",filepath);//get filepath
        lenpath = sendto(sockcd,filepath,sizeof(filepath),0,(struct sockaddr*)&server,sizeof(server));
        //lenpath = send(sockcd,filepath,strlen(filepath),0);// put file path to sever 
        if(lenpath<0)
        {
            printf("filepath send error!n");
        }   

        fp = fopen(filepath,"r");//opne file
        sleep(3);
        while((fileTrans = fread(buffer,sizeof(char),BUFFER_SIZE,fp))>0)
        {
            printf("fileTrans =%dn",fileTrans);
            if(sendto(sockcd,buffer,fileTrans,0,(struct sockaddr*)&server,sizeof(server))<0)
            {
                printf("send failed!n");
                break;      
            }
            bzero(buffer,BUFFER_SIZE); 
            //memset(buffer,0,sizeof(buffer));  
        }
        fclose(fp);
    }


    close(sockcd);



    return 0;
}

服务器端代码

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

#define BUFFER_SIZE 1024

int main()
{
    int sockcd;
    struct sockaddr_in server;
    char recv1[4096],send1[4096];

    char filename[100];
    char filepath[100];

    char *buffer;//file buffer
    int fileTrans;

    buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE);
    bzero(buffer,BUFFER_SIZE); 
    //memset(buffer,0,sizeof(buffer));
    int lenfilepath;
    FILE *fp;
    int writelength;

    if((sockcd = socket(AF_INET,SOCK_DGRAM,0))<0)
    {
        printf("socket build error!n");
    }
    memset(&server,0,sizeof(server));
    server.sin_family= AF_INET;
    server.sin_addr.s_addr = htonl(INADDR_ANY);
    server.sin_port = htons(9999);

    if((bind(sockcd,(struct sockaddr*)&server,sizeof(server)))==-1)
    {
        printf("bind error!n");
    }
    while(1)
    {

        //printf("client IP:   %dn",ntohl(client.sin_addr.s_addr));
            //printf("client PORT: %dn",ntohs(client.sin_port));

            memset(filename,'',sizeof(filename));
            memset(filepath,'',sizeof(filepath));

            lenfilepath = recv(sockcd,filepath,100,0);
            printf("filepath :%sn",filepath);
            if(lenfilepath<0)
            {
                printf("recv error!n");
            }
            else
            {
                int i=0,k=0;  
                for(i=strlen(filepath);i>=0;i--)  
                {  
                    if(filepath[i]!='/')      
                    {  
                        k++;  
                    }  
                    else   
                        break;    
                }  
                strcpy(filename,filepath+(strlen(filepath)-k)+1);   
            }
            printf("filename :%sn",filename);
            fp = fopen(filename,"w");
            if(fp!=NULL)
            {
                while(fileTrans =recv(sockcd,buffer,BUFFER_SIZE,0))
                {
                    if(fileTrans<0)
                    {
                        printf("recv error!n");
                        break;
                    }
                    writelength = fwrite(buffer,sizeof(char),fileTrans,fp);
                    if(writelength <fileTrans)
                    {
                        printf("write error!n");
                        break;
                    }
                    bzero(buffer,BUFFER_SIZE); 
                    //memset(buffer,0,sizeof(buffer));
                }
                printf("recv finished!n");
                fclose(fp);
            }

    }
    close(sockcd);



    return 0;
}

最后

以上就是结实刺猬为你收集整理的LINUX下实现UDP传输文件的全部内容,希望文章能够帮你解决LINUX下实现UDP传输文件所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(51)

评论列表共有 0 条评论

立即
投稿
返回
顶部