UDP实现文件内容传输

##程序可以直接复制使用,不受环境影响;

============================================================================

该设计框图左边实现文件循环写入并读取,读取数据存放于buf中,通过sendto()发送出去;

右边实现内容接收并保存到文件中,由recvfrom()接收暂存于buf缓冲区,write()将buf 内容存入文件,

read()进行读取验证。如果是只需要发送与接收功能:左边去掉write()部分,右边去掉read()

(不对信息进行验证)部分。

============================================================================

UDP实现文件内容传输_第1张图片

============================================================================

                                                      client 端口实现发送

/*client端实现对文件的追加写入以及发送;

*提示用户手动输入发送文件的路径与名称;

*文件必须是已经存在的;(后期修改自动创建文件)

*如果不需要向文件写入buf内容,可以将WriteInfo()函数功能去掉;

*/

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

 

#define PORT 8886

#define BUF_SIZE 1024

//#define FILE_PATH "./udpFile.txt"

 

int UdpClient(char path1[BUF_SIZE],int n1);

int WriteInfo(char path0[BUF_SIZE],int n0);

/*一维数组作为参数传递,手动输入发送文件文件路径*/

int main(void)

{

int n;

char path[BUF_SIZE];

n = sizeof(path)/sizeof(char);

printf("Please input send file path and name:\n");

scanf("%s",path);

UdpClient(path,n);

return 0;

}

/*<----------------从当前位置偏移2位,写入buf内容到文件----------------------->*/

int WriteInfo(char path0[BUF_SIZE],int n0)

{

int fd,len;

char buf[]="No xx:UDP send file content and save in receive file!";

fd=open(path0,O_WRONLY|O_APPEND);

if(fd<0)

{

perror("Write open file error ");

return 1;

}

else if(write(fd,buf,53)<0)

{

perror("write message failed!\n");

return 1;

}

else if(lseek(fd,4,SEEK_CUR)<0)/*当前位置偏移4位更改内容*/

{

perror("Write lseek error!");

return 1;

}

else

{

printf("Write message to file sucess!\n");

}

close(fd);

len=strlen(buf); /*接收端第0次位空白*/

return len;

}

/*<----------------发送buff缓存的内容,循环一次偏移一次----------------------->*/

int UdpClient(char path1[BUF_SIZE],int n1)

{

int i=0,StepSize,Steplen=0;

int fd;

struct sockaddr_in s_addr; //套接字地址结构

int sock; //套接字描述符

int addr_len; //地址结构长度

int len; //发送字节长度

char buff[BUF_SIZE];

/* 创建数据报模式的套接字 */

if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { //SOCK_DGRAM表示数据包套接字UDP

perror("socket error");

exit(errno);

}

else

printf("create socket successful.\n\r");

/* 设置对方地址和端口信息 */

s_addr.sin_family = AF_INET; //地址族

s_addr.sin_port = htons(PORT);

s_addr.sin_addr.s_addr = htonl(INADDR_ANY);//inet_addr(argv[1]);

addr_len = sizeof(s_addr); //地址结构长度

/*从客户端的buff缓冲区中发送消息到地址结构为s_addr的远端机器*/

printf("-----------------------------%d------------------------------\n",i);

while(1)

{

/*write and Read file message to buff*/

StepSize=WriteInfo(path1,n1);

fd=open(path1,O_RDWR); /*以读写方式打开,O_RDNOLY 只读方式存在问题*/

if(fd<0)

{

perror("Read Open file error");

return 1;

}

else if(lseek(fd,Steplen,SEEK_SET)<0) /*步长+strlen(buf),循环一次读取新写入的内容*/

{

perror("Read lseek error!");

return 1;

}

else

printf("Read open file sucess!\n");

if(read(fd,buff,53)<0)

{

perror("read file content failed ");

return 1;

}

else

printf("Read file information save to buff sucessful!\n");

close(fd);

len = sendto(sock, buff, sizeof(buff), 0,(struct sockaddr *) &s_addr, addr_len);

if (len < 0) { /*如果发送失败*/

printf("\n\rsend error.\n\r");

return 1;

}

printf("Send buff info successful!\n\r");

Steplen+=StepSize;

printf("StepSize=%d\tSteplen=%d\n",StepSize,Steplen);

sleep(2);

printf("-----------------------------%d------------------------------\n",++i);

 

}

close(sock);

return 0;

}

###############################################################################

                                                           server端口实现接收

/*server端实现对文件的接收并保存到指定文件中;

*提示用户手动输入保存文件的路径与名称;

*文件必须是已经存在的;(后期修改自动创建文件)

*保存后在进行读取打印,确认信息无误。

*/

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

 

#define PORT 8886

#define BUF_SIZE 1024

//#define FILE_PATH "./udpSave.txt"

 

int WriteFileInfo(char buff[BUF_SIZE],int Steplen,char path[BUF_SIZE],int n);

int ReadFileInfo(int Steplen,char path[BUF_SIZE],int n);

int UdpServerRecv(char path[BUF_SIZE],int n);

/*一维数组作为参数传递,手动输入保存文件路径*/

int main(void) /*main(int argc, char **argv)*/

{

int n;

char path[BUF_SIZE];

n = sizeof(path)/sizeof(char);

printf("Please input save file path and name:\n");

scanf("%s",path);

UdpServerRecv(path,n);

return 0;

}

int UdpServerRecv(char path[BUF_SIZE],int n)

{

struct sockaddr_in s_addr; //服务器地址结构,IPV4对应的是sockaddr_in结构体

struct sockaddr_in c_addr; //客户端地址结构

int sock,i=0; //套接字描述符

socklen_t addr_len; //地址结构长度

int len,StepSize,Steplen=0; //接收到的消息字节数

char buff[BUF_SIZE ]; //存放接收消息的缓冲区

 

if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {

perror("socket");

exit(errno);

}

else

printf("create socket successful.\n\r");

 

memset(&s_addr, 0, sizeof(struct sockaddr_in));

 

s_addr.sin_family = AF_INET;

s_addr.sin_port = htons(PORT); //把16位值从主机字节序转换成网络字节序

s_addr.sin_addr.s_addr = INADDR_ANY; //输入本机IP地址

if ((bind(sock, (struct sockaddr *) &s_addr, sizeof(s_addr))) == -1) {

perror("bind error");

exit(errno);

}

else

printf("bind address to socket successfuly.\n\r");

 

addr_len = sizeof(c_addr);

printf("-------------------------------------%d---------------------------------------\n",i);

while (1) {

len = recvfrom(sock, buff, sizeof(buff) - 1, 0,(struct sockaddr *) &c_addr, &addr_len);

if (len < 0) {

perror("recvfrom error");

exit(errno);

}

StepSize=strlen(buff)-1;

buff[len] = '\0';

printf("Received from a remote computer:%s ;The port number is:%d\nBuf Message is: \n%s\n\r",inet_ntoa(c_addr.sin_addr), ntohs(c_addr.sin_port), buff);

WriteFileInfo(buff,Steplen,path,n);

ReadFileInfo(Steplen,path,n);

Steplen+=StepSize;

printf("StepSize=%d\tSteplen=%d\n",StepSize,Steplen);

sleep(2);

printf("-------------------------------------%d---------------------------------------\n",++i);

}

close(sock);

return 0;

}

int WriteFileInfo(char buff[BUF_SIZE],int Steplen,char path[BUF_SIZE],int n)

{

int fd;

fd= open(path,O_WRONLY); //"O_WRONLY"

if(fd<0)

{

perror("Write open file error");

return 1;

}

else if(lseek(fd,Steplen,SEEK_SET)<0) /*步长+strlen(buf),循环一次读取新写入的内容*/

{

perror("Read lseek error!");

return 1;

}

else if(write(fd,buff,53)<0)

{

perror("write file error");

return 1;

}

else

printf("write sucessful!\n");

close(fd);

return 0;

}

int ReadFileInfo(int Steplen,char path[BUF_SIZE],int n)

{

int fd;

char buf[BUF_SIZE];

fd=open(path,O_RDWR); /*以读写方式打开,O_RDNOLY 只读方式存在问题*/

if(fd<0)

{

perror("Read open file error");

return 1;

}

else if(lseek(fd,Steplen,SEEK_SET)<0) /*步长+strlen(buf),循环一次读取新写入的内容*/

{

perror("Read lseek error!");

return 1;

}

else if(read(fd,buf,53)<0)

{

perror("Read file error");

return 1;

}

else

printf("Read info on file sucessful!\n");

close(fd);

printf("The save info is : %s\n",buf);

return 0;

}

============================================================================

 

 

 

 

你可能感兴趣的:(Linux,C,语言编程,高级C语言编程)