Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Mar 2007
    Messaggi
    230

    fwrite segmantaion fault

    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;



  2. #2
    come minimo non controlli il valore restituito da:
    Codice PHP:
    FILE *fout fopen("/home/pippo.txt","wb"); 
    Se non è valido, allora il crash è il minore dei mali.

  3. #3
    Utente di HTML.it
    Registrato dal
    Mar 2007
    Messaggi
    230
    Domani controllo.
    grazie

  4. #4
    Utente di HTML.it
    Registrato dal
    Mar 2007
    Messaggi
    230
    Grazie per il tuo consiglio, ho provveduto ad inserire un controllo.
    L'errore era dovuto al fatto che con fopne tentavo di aprire un file in scrittura al di fuori dell'are che mi è stata assegnata come utente.
    in paratica fopen ("/home/pippo.txt") non si può eseguire perchè non ho i permessi per la cartella home.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.