在C中使用UDP进行服务器 - 客户端文件传输 [英] Server-client file transfer using UDP in C

查看:165
本文介绍了在C中使用UDP进行服务器 - 客户端文件传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用UDP进行服务器 - 客户端文件传输。我创建了一个基本服务器,它接收客户端发送的消息。就这样。现在主要是: -



1.客户端发送的消息是文件名。

2.现在服务器检查是否存在此文件。

3.如果存在,则将文件发送到客户端,并且还保留客户端发出的文件请求数。



这是服务器代码



 #include< sys / socket.h> 
#include< arpa / inet.h>
#include< stdio.h>
#include< unistd.h>
#include< fcntl.h>
#include< sys / types.h>
#include< string.h>




int main()
{

char buff [2000];
char file_buffer [2000];
int sd,connfd,len;

struct sockaddr_in servaddr,cliaddr;

sd = socket(AF_INET,SOCK_DGRAM,0);

if(sd == - 1)
{
printf(socket not not in server\\\
);
退出(0);
}
else
{
printf(在server \\\
中创建的套接字);
}

bzero(& servaddr,sizeof(servaddr));

servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons(7802);

if(bind(sd,(struct sockaddr *)& servaddr,sizeof(servaddr))!= 0)
printf(Not binded\\\
);
else
printf(Binded\\\
);



len = sizeof(cliaddr);

recvfrom(sd,buff,1024,0,
(struct sockaddr *)& cliaddr,& len);

printf(%s \ n,buff);
/ * * /
FILE * fp;
fp = fopen(buff,r);
if(fp == NULL)
{
printf(file not exists\\\
);
}

fseek(fp,0,SEEK_END);
size_t file_size = ftell(fp);
fseek(fp,0,SEEK_SET);
if(fread(file_buffer,file_size,1,fp)< = 0)
{
printf(无法将文件复制到buffer \ n);
退出(1);
}
if(sendto(sd,file_buffer,strlen(file_buffer),0,(struct sockaddr *)& cliaddr,& len)< 0){
printf(error在发送文件\\\
);
退出(1);
}
bzero(file_buffer,sizeof(file_buffer));


/ * * /
close(sd);

return(0);
}





这是客户代码

 #include< ; stdio.h中> 
#include< errno.h>
#include< sys / socket.h>
#include< resolv.h>
#include< netinet / in.h>
#include< sys / types.h>



int main()
{
char buff [2000];
int sockfd,connfd,len;

struct sockaddr_in servaddr,cliaddr;

//在客户端创建套接字
sockfd = socket(AF_INET,SOCK_DGRAM,0);

if(sockfd == - 1)
{
printf(socket不在client \\\
中创建);
退出(0);
}
else
{
printf(在client \ n中创建的套接字);
}


bzero(& servaddr,sizeof(servaddr));

servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY; //任何地址或使用特定地址
servaddr.sin_port = htons(7802); //端口地址


printf(输入你的UDP客户端消息\ n);
scanf(%s,buff);

//将msg发送到服务器

sendto(sockfd,buff,strlen(buff),0,
(struct sockaddr *)& servaddr,sizeof( struct sockaddr));
char file_buffer [2000];

if(recvfrom(sockfd,file_buffer,2000,0,(struct sockaddr *)& servaddr,sizeof(struct sockaddr))< 0)
{
printf( 收到文件中的错误\ n);
退出(1);
}

char new_file [] =复制;
strcat(new_file,buff);
FILE * fp;
fp = fopen(new_file,w +);
if(fwrite(file_buffer,1,sizeof(file_buffer),fp)< 0)
{
printf(error writinging file \\\
);
退出(1);
}


//关闭客户端连接
close(sockfd);

return(0);
}









服务器从中读取数据文件并写入并发送到客户端,另一端客户端接收数据并复制此文件并写入其中。但在编译时,它会在写入文件时出错:(

解决方案

完成!



有文件句柄有问题

而不是使用fopen,使用open(),write()和read()来完成工作Smile |:)


can有谁告诉我如何编译这段代码,我们在那里指定文件目录??????????????????????

I have to make a server-client file transfer using UDP . I have created a basic server which receives message sent by client . That's all. Now comes the major part :-

1. The message sent by client is the name of file .
2. Now the server checks whether there exists this file or not .
3. If there exists it sends the file to the client and it also keeps the count of the number of requests of file made by the client .

Here is the Server Code

#include<sys/socket.h>
#include<arpa/inet.h>
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<string.h>




int main()
{

  char buff[2000];
  char file_buffer[2000];
  int sd,connfd,len;

  struct sockaddr_in servaddr,cliaddr;

  sd = socket(AF_INET, SOCK_DGRAM, 0);

  if(sd==-1)
    {
      printf(" socket not created in server\n");
      exit(0);
    }
  else
    {
      printf("socket created in  server\n");
    }

  bzero(&servaddr, sizeof(servaddr));

  servaddr.sin_family = AF_INET;
  servaddr.sin_addr.s_addr = INADDR_ANY;
  servaddr.sin_port = htons(7802);

  if ( bind(sd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0 )
    printf("Not binded\n");
  else
    printf("Binded\n");



  len=sizeof(cliaddr);

  recvfrom(sd,buff,1024,0,
   (struct sockaddr *)&cliaddr, &len);

  printf("%s\n",buff);
  /* */
  FILE *fp;
  fp=fopen(buff,"r");
  if(fp==NULL)
    {
      printf("file does not exist\n");
    }

  fseek(fp,0,SEEK_END);
  size_t file_size=ftell(fp);
  fseek(fp,0,SEEK_SET);
  if(fread(file_buffer,file_size,1,fp)<=0)
    {
      printf("unable to copy file into buffer\n");
      exit(1);
    }
  if(sendto(sd,file_buffer,strlen(file_buffer),0,  (struct sockaddr *)&cliaddr, &len)<0)    {
    printf("error in sending the file\n");
    exit(1);
  }
  bzero(file_buffer,sizeof(file_buffer));


  /* */
  close(sd);

  return(0);
}



Here is the client code

#include <stdio.h>
#include <errno.h>
#include <sys/socket.h>
#include <resolv.h>
#include<netinet/in.h>
#include<sys/types.h>



int main()
{
    char buff[2000];
    int sockfd,connfd,len;

struct sockaddr_in servaddr,cliaddr;

// create socket in client side
sockfd = socket(AF_INET, SOCK_DGRAM, 0);

if(sockfd==-1)
{
    printf(" socket not created in client\n");
    exit(0);
}
else
{
    printf("socket created in  client\n");
}


bzero(&servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = INADDR_ANY; // ANY address or use specific address
servaddr.sin_port = htons(7802);  // Port address


    printf("Type ur  UDP client message\n");
    scanf("%s",buff);

// send  msg to server

sendto(sockfd, buff, strlen(buff), 0,
          (struct sockaddr *)&servaddr, sizeof(struct sockaddr));
      char file_buffer[2000];

    if (recvfrom(sockfd,file_buffer,2000,0,  (struct sockaddr *)&servaddr, sizeof(struct sockaddr))<0)
    {
      printf("error in recieving the file\n");
      exit(1);
    }

  char new_file[]="copied";
  strcat(new_file,buff);
  FILE *fp;
  fp=fopen(new_file,"w+");
  if(fwrite(file_buffer,1,sizeof(file_buffer),fp)<0)
    {
      printf("error writting file\n");
      exit(1);
    }


  //close client side connection
    close(sockfd);

return(0);
}





The server reads the data from file and writes into it and send to the client , On the other end client receive the data and make duplicate of this file and write into it. But on compiling it gives error in writting the file :(

解决方案

Done that !

there was something wrong with file handle
Instead of using fopen ,use of open() ,write() and read() do the work Smile | :)


can anyone tell me how to compile this code ,where we are specifying the file directory???????????


这篇关于在C中使用UDP进行服务器 - 客户端文件传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆