c - Sending non-text files via UDP doesn't work -


i have problem in c don't solution...

i've written server- , client-application in c.

the server-application reads file , cuts chunks of 1024 bytes send via udp client application.

the client-application gets these chunks, creates , writes new file.

if input-file text file applications working.

for project i'm working on need send media files (.mp3, .mp4, .ogg etc.) (yes, need use udp that). if want send files these it's not working.

my problems:

  • when data via udp socket 1024 of data every time receive message - except last package. application saves data gets via udp in char array. lenght of message, check array empty fields. (i tried different, didn't work...) works fine text-files, not media-files. how can better? (probably not inefficent bad coding style, too...)

    int l = 0; n = 0; for(l = 0; l<sizeof(buffer) ;l++){      if(buffer[l] != null){         n++;     }  } 
  • what have send non-text data correctly? there better solution sending files via udp? have advice?

here c-files:

server.c

#include<arpa/inet.h> #include<netinet/in.h> #include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<sys/socket.h> #include<stdlib.h> #include<string.h>  #define port 9333    #define server_ip "127.0.0.1"    #define buffer_size 1024   file *input; file *checkfile;  int n;  int count = 0;   struct sockaddr_in remote;   int s;                 int main(){  unsigned char buffer[buffer_size];     char check[100];  printf("press enter send file!\n"); fgets(check, 100, stdin);   input = fopen("input.txt", "r"); checkfile = fopen("checkfile.txt", "w");  if(input){      if((s = socket(af_inet, sock_dgram, ipproto_udp)) == -1){         fprintf(stderr, "error getting socket! \n");         exit(-1);     }      memset((char *) &remote, 0, sizeof(remote));      remote.sin_family = af_inet;     remote.sin_port = htons(port);       if(inet_aton(server_ip, &remote.sin_addr) == 0){         fprintf(stderr, "maybe no valid adress\n");         exit(1);     }      while(!feof(input)){         n = fread(buffer, 1, buffer_size, input);         count += n;         printf("n = %d\n", n);          if(sendto(s, buffer, n, 0, &remote, sizeof(remote)) == -1){             fprintf(stderr, "error while sending data!\n");             exit(-1);         }         fwrite(buffer, 1, n, checkfile);     }     printf("%d bytes sent. \n", count);  }else{     printf("error while opening input file!"); } printf("file sent!!\n");  close(s); fclose(input);  return 0; } 

client.c:

#include<arpa/inet.h> #include<netinet/in.h> #include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<sys/socket.h> #include<stdlib.h> #include<string.h>  #define port 9333 #define buffer_size 1024  int n; file *output;  struct sockaddr_in local, remote; int len_remote = sizeof(remote); int s; int count = 0;  int main(){  char buffer[buffer_size]; output = fopen("output.txt", "w");  if((s = socket(af_inet, sock_dgram, ipproto_udp)) == -1){     fprintf(stderr, "error getting socket!\n");     exit(-1); }  memset((char *) &remote, 0, sizeof(remote)); memset((char *) &local, 0, sizeof(remote));  local.sin_family = af_inet; local.sin_port = htons(port); local.sin_addr.s_addr = htonl(inaddr_any);  if(bind(s, &local, sizeof(local)) == -1){     fprintf(stderr, "cannot connect service!\n");     exit(-1); }  int flag = 1;     while(flag == 1){      memset(buffer, '\0', buffer_size);      if(recvfrom(s, buffer, buffer_size, 0, &remote, &len_remote) == -1){         fprintf(stderr, "fail while receiving data! \n");         exit(-1);     }      int l = 0;     n = 0;     for(l = 0; l<sizeof(buffer) ;l++){          if(buffer[l] != null){             n++;         }      }      count += n;     printf("n = %d\n", n);      fwrite(buffer, 1, n, output);      if(n<1024){         flag = 0;     }  } printf("got file!\n");  close(s); fclose(output); return 0;  } 

if has help/advices me, great.

best regards,

phil

from client.c:

int   nbrecv = 0;     while(flag == 1){      memset(buffer, '\0', buffer_size);      if((nbrecv = recvfrom(s, buffer, buffer_size, 0, &remote, &len_remote)) == -1){         fprintf(stderr, "fail while receiving data! \n");         exit(-1);     }      count += nbrecv;     printf("n = %d\n", nbrecv);      fwrite(buffer, 1, nbrecv, output);      if(nbrecv<1024){         flag = 0;     }  } 

recvfrom() return number of received bytes, use it.

be aware udp doesn't have guarantees (the packets are'nt re-ordered, or whatever). if use udp, may want create basic protocol contain informations (number of packet, number of bytes in packet, etc.).


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -