1

Ive been getting strange results with the sendto function in C.(LINUX) What I am trying to do is to implement a reliable UDP kind of scheme.

The snippet of code provided below is a part of the timer that expires when a packet is dropped and sends a message to a process to retransmit that packet.

The weird problem which I am having is that when transferring a large file say... >300KB The code given below works perfectly (i.e. THE ELSE PART IS EXECUTED) But after a certain number of packets are sent...it executes the then part.!!!

THIS IS STRANGE BECAUSE THE CODE WORKED FINE FOR ABOUT 250 PACKETS BUT WITH THE 251 PACKET kabooom!!!

 n =  sendto(sockfd, &(forwardPeer->id), sizeof(forwardPeer->id), 0, (struct sockaddr*)&tcpd_addr, sizeof(tcpd_addr));
 if(n<0)
    printf("\n error sending to tcpdc");
 else 
    printf("\n message sent to tcpdc");     

PLEASE HELP!!!! THanks in advance

Peter G.
  • 14,786
  • 7
  • 57
  • 75

1 Answers1

1

The Bad file descriptor error means that the passed sockfd value is incorrect. Either:

  • The file descriptor has been closed; or
  • The value of that variable has been overwritten by junk, probably due to a bounds overflow somewhere in your program.

To catch the second case, run the program under a debugger and set a watchpoint on the sockfd variable - this will break into the debugger whenver the value changes, which should let you see where it's being changed when it shouldn't be.

You could also try running the program under valgrind, and fixing any issues it reports.

caf
  • 233,326
  • 40
  • 323
  • 462