Mail Archives: djgpp/1998/10/15/22:42:59
I use the following code segment for my project which is a simple file transfer program and I experience some weird problem -- the last 2 bytes of the packet are always corrupted. It goes away when I add 2 to the total packet size. The problem is why is the packet corrupted and although the problem goes away when I add 2 to packet size, I don't understand why there is a need to add 2.
Does anyone know the problem?
--------------------------------------------
dp.packet.opcode = DATA;
dp.packet.blockno = blockno;
dp.packet.comp = ~blockno;
dp.packet.checksum = checksum(dp.packet.data, bytes_read);
dgram_send(ss, dp.buf, bytes_read + (2 * sizeof(unsigned short)) + (2 * sizeof(unsigned int)) + 2); <== added 2 to packet size and the problem goes away. But why??
printf("Sent data packet %5u (%3d bytes, %5u).\n", blockno, bytes_read, dp.packet.checksum);
--------------------------------------------------------
dgram_send function
void dgram_send(int ss, char * sbuf, int length)
{
sendto(ss, sbuf, length, 0, (struct sockaddr*)&from, sizeof(from));
}
-----------------------------------------------
The Data Packet definition
union DATAPacket
{
struct
{
unsigned short opcode;
unsigned int blockno;
unsigned int comp;
unsigned short checksum;
char data[BUFSIZE];
} packet;
char buf[524];
};
----------------------------------------
- Raw text -