Advertisement

02.27.2007 at 01:39AM PST, ID: 22415425
[x]
Attachment Details

C Programming: sending binary file thru socket UDP

Asked by godman_polly in C Programming Language, User Datagram Protocol (UDP)

Tags: udp, socket, binary, file

I am writing c programms that sends file thru socket. The client read a binary file and send the content to the server program thru socket. And the client program receive the content of the file and then write it to a file. The programs work well in sending text file but not binary file.  I use fopen (xx, "wb"), fread and fwrite. I dun know why it cannot send binary file.
 Segments of the code is as follows.
Client.c
ptr = fopen(argv[2],"rb");  /*  open the file    */
....
while(1)
  {
    /* read LEN bytes from the file to array data */
    read_bytes = fread(data, sizeof(char), LEN, ptr);
    /* check reading error */
    if (ferror(ptr))
    {
      perror("error during reading file!");
      exit(1);
    }

    if (read_bytes < LEN)
    {
      data[read_bytes] = EOF;
      read_bytes++;
    }

    printf("Number of reading byte: %d\n", read_bytes);
    printf("%s\n", data);
    //send content
    //if ((numbytes = sendto(sockfd, data, LEN, 0,
    if ((numbytes = sendto(sockfd, data, read_bytes, 0,
                         (struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {

      perror("sendto");
      exit(1);
    }

    printf("Number of sending byte: %d\n", numbytes);

    /* quite the while-loop */
    if ( feof(ptr) )
      break;
  }
........
close(fp)

server.c

/* find the position of EOF */
int findeof(char* content, int size)
{
  int i;
  for (i=0; i<size; i++)
    if (content[i]==EOF)
      return i;

  /* EOF is not found */
  return -1;
}
.....
 while(1)  // main loop
    {
      addr_len = sizeof(struct sockaddr);

      if ((numbytes=recvfrom(sockfd, buf, MAXBUFLEN, 0,
         (struct sockaddr *)&their_addr, &addr_len)) == -1) {
         exit(1);
      }

      //printf("got packet from %s\n",inet_ntoa(their_addr.sin_addr));
      printf("packet is %d bytes long\n",numbytes);
      //buf[numbytes] = '\0';
      //printf("packet contains \"%s\"\n",buf);
      printf("%s\n",buf);

      /* write the receive content into the file */
      if (findeof(buf, MAXBUFLEN) == -1)
        fwrite(buf, sizeof(char), MAXBUFLEN, fp);
      else
      {
        /* no need to write EOF to the file */
        fwrite(buf, sizeof(char), findeof(buf, MAXBUFLEN), fp);
        printf("location eof %d\n", findeof(buf, MAXBUFLEN));
      }

      /* quit the loop */
      if (findeof(buf, MAXBUFLEN) != -1)
        break;

    }   //END OF WHILE LOOP
...
    fclose(fp);
    close(sockfd);

Could anyone can help me?
Start Free Trial
[+][-]02.27.2007 at 02:02AM PST, ID: 18615696

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: C Programming Language, User Datagram Protocol (UDP)
Tags: udp, socket, binary, file
Sign Up Now!
Solution Provided By: Infinity08
Participating Experts: 4
Solution Grade: A
 
 
[+][-]02.27.2007 at 08:26AM PST, ID: 18618165

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]02.27.2007 at 11:45AM PST, ID: 18619764

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]02.27.2007 at 05:19PM PST, ID: 18622095

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]02.27.2007 at 11:31PM PST, ID: 18623238

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]02.28.2007 at 09:45PM PST, ID: 18631306

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]03.24.2007 at 07:23AM PDT, ID: 18785208

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]03.29.2007 at 06:12PM PDT, ID: 18820977

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32