Ciao ragazzi,

ho scritto un server ed un receiver entrambi su protocollo UDP. L'obietto è di fare inviare un file dal sender al receiver.
La compilazione va a buon fine ma quando invio dei pacchetti al receiver mi viene dato un errore di segmentation fault in corrispondenza di una fwrite che potete vedere nel codice.
Per favore qualcuno può darmi una mano (vi riporto anche il codice del sender) .
GRAzie

Codice PHP:
sender..

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define PORT 5500
#define BUFFSIZE 100
#define MIN(_x_,_y_)(((_x_)<(_y_) )? (_x_):(_y_))
int main (int argccharargv){


  
FILEfin;
  
int sd/*socket descriptor*/
  
struct sockaddr_in dest;
  
char buf[BUFFSIZE];  /*buffer for socket*/
  
unsigned long lengthfile_sizeencoded_file_sizechunk_size;

  
memset((char *)&dest0sizeof(dest));
  
dest.sin_family PF_INET;
  
dest.sin_port htons(PORT);
  
dest.sin_addr.s_addr inet_addr("10.0.0.201"); /*destination's address*/



   
  
if ((sd socket(PF_INET,SOCK_DGRAM,0))<0) {
  
perror("socket creation");
  }
  
fin fopen("/home/dileo/prova.txt","rb");
  if (
fin != NULL){
      
fseek(fin,0SEEK_END);
      
file_size ftell(fin);
      
fseek(fin,0SEEK_SET);
      
encoded_file_size htonl(file_size); /*send dim file*/
       
if(sendto(sd, &encoded_file_sizesizeof(encoded_file_size), 0, (struct sockaddr*)&destsizeof(dest))<0){
       
perror("send to error");
       }
       for (
length=0length file_sizelength += chunk_size ){
           
chunk_size MIN(BUFFSIZEfile_size-length);
           
fread(bufchunk_size,1fin);  /*Check*/
           
if (chunk_size != sendto(sdbufchunk_size,0,(struct sockaddr*)&destsizeof(dest)))
              
perror("Unable to send data");
       }
    
fclose(fin);
    }else{
    
printf("Unable to open file");
    }
close(sd);   


Codice PHP:

RECEIVER
.

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#define BUFSIZE 5000
#define DEBUG 1
#define PORT 5500 /*receiver's port*/
#define MIN(_x_,_y_)(((_x_)<(_y_) )? (_x_):(_y_))

int main (int argcchar** argv){

  
int sdsfamilystypesprotocol;
  
struct sockaddr_in receivertheir_addr;
  
socklen_t their_addr_len;
  
unsigned long file_size;

    
memset((char *)&receiver0sizeof(receiver)); /*clean */     
    
receiver.sin_family PF_INET;
    
receiver.sin_port htons(PORT);
    
receiver.sin_addr.s_addr INADDR_ANY;
    
    if ((
sd socket(PF_INETSOCK_DGRAM0))<0)
       
perror("socket creation error"); 
    if (
bind(sd, (struct sockaddr*)& receiversizeof(receiver))){
    
perror("bind error");
    exit(
1);
    }
 
    if (
recvfrom(sd,&file_sizesizeof(unsigned long),0, (struct sockaddr*)&their_addrtheir_addr_len)<0){
         
perror("receive error");
    }
    
printf("received packet from %s\n "inet_ntoa(their_addr.sin_addr));
    
printf("packet contains \"%i\"\n",ntohl(file_size));        
    
   
FILE *fout fopen("/home/pippo.txt","wb"); 
   
char buffer[BUFSIZE];
   
unsigned long lengthchunk_size;
       for (
length 0length file_sizelength += chunk_size){
#ifdef DEBUG        
        
printf("length %i\n"length);
#endif        
    
chunk_size recvfrom (sd, &bufferMIN(BUFSIZEfile_size-length),0,NULLNULL);
#ifdef DEBUG
      
printf("chunk_size %i\n"chunk_size);
#endif     
     
if (!chunk_size){
        
printf("connection unexpdeately terminated");
        
close(sd); 
        }
        
    
fwrite(bufferchunk_size,1fout); /*<--questa fwrite è reposnsabile segment fault */ 
        
}        
    
 
fclose(fout);   
 
close(sd);

return 
0;