From: "Y.K. Goh" Sender: "Y.K. Goh" Subject: Problem with socket programming Newsgroups: comp.os.msdos.djgpp X-User-Info: 153.20.61.145 s6606555 npnsnews NNTP-Posting-Host: 153.20.15.92 Message-ID: <3626a63b.0@nsuxnews> Date: 16 Oct 1998 09:49:47 +0800 X-Trace: 16 Oct 1998 09:49:47 +0800, 153.20.15.92 Lines: 38 To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com 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]; }; ----------------------------------------