Thread: need help understanding how to use data sent via a socket and writing it into a file

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    5

    need help understanding how to use data sent via a socket and writing it into a file

    Hi everyone.

    I am working on creating a system that takes sensor readings from an android device, and outputs them into a useable format ie csv.

    Android Server (Sensor data) -> .C Client -> CSV format -> MATLAB

    I've followed : How I do what I do: Socket Server to transfer Accelerometer Sensor data to PC this tutorial, but have looked around on how to write the sent data into a csv and am having trouble understanding how to do this.

    I've attempted to directly fwrite the buffer into a csv file, which didn't work as there were a bunch of gibberish characters.

    Any advice or pointers would be greatly appreciated!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Posting your actual code attempt would be good.
    Saying "it didn't work" doesn't help us inform you where you went wrong.

    So what does the code on the link actually do, if you just compile and run it?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2015
    Posts
    5
    Code:
    int main(int argc, char *argv[])
    {
        const char * filename;
        int sockfd, portno, n;
        struct sockaddr_in serv_addr;
        struct hostent *server;
      FILE *fp = fopen("test.csv", "wb");  //creating the csv file
    
        char buffer[256] = "\0";
        if (argc < 3) {
           fprintf(stderr,"usage %s hostname port\n", argv[0]);
           exit(0);
        }
     
        portno = atoi(argv[2]);
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0) 
            error("ERROR opening socket");
        server = gethostbyname(argv[1]);
     
        if (server == NULL) {
            fprintf(stderr,"ERROR, no such host\n");
            exit(0);
        }
     
        bzero((char *) &serv_addr, sizeof(serv_addr));
        serv_addr.sin_family = AF_INET;
        bcopy((char *)server->h_addr, 
             (char *)&serv_addr.sin_addr.s_addr,
             server->h_length);
        serv_addr.sin_port = htons(portno);
        if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
            error("ERROR connecting");
     
    
        bzero(buffer,256);
        n = write(sockfd,buffer,strlen(buffer));
        if (n < 0) 
             error("ERROR writing to socket");
         
        while(1){   
            bzero(buffer,256);
            n = read(sockfd,buffer,255);
    
            if (n < 0) 
                error("ERROR reading from socket");
            printf("%s",buffer); 
    fwrite(buffer,sizeof(buffer),12,fp); // my attempt at writing it , 
    
    
          
        }
         
        
        close(sockfd);
       
        fclose(fp);
        return 0;
    }
    I applied the commsThread with my app, to get the socket communication working, and used implemented the client code on netbeans.
    Communication works, what I'm trying to figure out is how to make use of the information being transported via the sockets.

    Cheers

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Just use
    fprintf(fp,"%s",buffer);

    fwrite - as you have written, always writes 3K of data (including much junk), regardless of the length of the actual text.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2015
    Posts
    5
    Hi,

    I tried to use fprintf, nothing gets written to the file :S.
    Any other ideas?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Does your printf print something?

    It's hard to see how you would see one and not the other.

    Post your latest code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Oct 2015
    Posts
    5
    Hi, the codes the same, I just replaced the fwrite with fprintf(fp,"%s",buffer);
    And yes, printf prints :
    need help understanding how to use data sent via a socket and writing it into a file-1-png

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    A couple of thoughts.

    1. make sure the file doesn't exist before you run it.

    2. add error checks,
    Code:
    FILE *fp = fopen("test.csv", "w");  // no need for b mode
    if ( fp == NULL ) {
      perror("open failed");
      return 1;
    }
    Code:
    if ( fprintf(fp,"%s",buffer) < 0 ) {
      printf("write failed\n");
    }

    > bzero(buffer,256);
    > n = write(sockfd,buffer,strlen(buffer));
    The write does nothing. strlen() on a buffer full of zeros will return zero.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Oct 2015
    Posts
    5
    Hi,

    Yeah I've made sure the file doesn't exist.
    Included the error checks, none of the errors occured.
    I did :
    Code:
    if ( fprintf(fp,"%s",buffer) > 0 ) {
    
      printf("Success\n");
    
    }

    And it printed out success.
    Nothing got written out to the file though.
    But something should've been written right?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Which OS are you running the C code on?

    How are you determining whether anything is written to the file? If you're waiting for the file to grow in size by looking at the file using some kind of file explorer, you could be in for a wait. Data may be buffered before being copied to the file.

    Try fflush(fp); after every fprintf() to make sure the data is flushed to the file straight away.

    Since fwrite worked, maybe this hacky approach
    fwrite(buffer,1,strlen(buffer),fp);

    > And it printed out success.
    > Nothing got written out to the file though.
    > But something should've been written right?
    I would certainly have expected so - yes.
    Very odd...
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Reading/Writing Problems [Socket Programming]
    By MaSSaSLaYeR in forum C Programming
    Replies: 5
    Last Post: 11-17-2011, 11:39 AM
  2. Writing to a Data File
    By joxerjen in forum C++ Programming
    Replies: 6
    Last Post: 10-15-2005, 03:39 AM
  3. reading from the open socket then writing to a file...
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-06-2002, 04:15 PM
  4. writing a file to an open socket...
    By Unregistered in forum C Programming
    Replies: 9
    Last Post: 06-30-2002, 08:43 PM
  5. Writing Data to a File
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-18-2002, 03:17 AM

Tags for this Thread