# send() trashing data

**URL:** <https://forums.digi.com/t/send-trashing-data/3589>\
**Category:** Linux\
**Tags:** socket\
**Created:** [August 10, 2009, 11:47pm UTC](https://forums.digi.com/t/send-trashing-data/3589 "2009-08-10T23:47:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![egawtry](https://avatars.discourse-cdn.com/v4/letter/e/8e8cbc/32.png) [@egawtry](https://forums.digi.com/u/egawtry)\
**Post date:** [August 10, 2009, 11:47pm UTC](https://forums.digi.com/t/send-trashing-data/3589/1 "2009-08-10T23:47:50Z")

</div>

I am reading a file and writing the contents to a port using send(). The data is corrupted - missing data on the start and extra junk on the end. Here is my code, could someone tell me what I am doing wrong?

char buffer[16384];  
long i, iRead;

send(socket, “This works!”, 10, 0);

for(i = 0; i \< size; i += iRead)  
{  
iRead = fread(buffer, 1, sizeof(buffer)-16, fp);  
if( iRead \<= 0 ) break;  
//buffer[iRead] = ‘\0’;  
//printf(“%s”, buffer); // Test to see if data correct  
send(socket, (const char \*)buffer, iRead, 0);  
}

P.S. The fread() is returning the correct data. Also this is NetOS7.4.

Thanks,  
-Erik

---

<div class="post-metadata">

**Author:** ![sbrown](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@sbrown](https://forums.digi.com/u/sbrown)\
**Post date:** [August 12, 2009, 1:48pm UTC](https://forums.digi.com/t/send-trashing-data/3589/2 "2009-08-12T13:48:32Z")

</div>

You did not say what platform you doing your development work on, but there is an excellent “Serial Driver Sample” application available under the Connect Core 9P-9360 platform.

I would recommend looking at this for some guidance. This sample application demonstrates sending data from one serial port (TEST\_COM0) to another serial port(TEST\_COM1) of the NET+OS development board. The data is then stored in a file (data.c).

Let me know if this example doesn’t answer your questions.

Scott

---

<div class="post-metadata">

**Author:** ![charliek](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@charliek](https://forums.digi.com/u/charliek)\
**Post date:** [August 15, 2009, 4:15am UTC](https://forums.digi.com/t/send-trashing-data/3589/3 "2009-08-15T04:15:58Z")

</div>

Digi updated the forums to stop spammers from posting.

\>\>send(socket, “This works!”, 10, 0);  
you’re only sending 10 bytes out of the 11 in the string, so you’re losing a byte you think you should have.

Try throwing ‘static’ in front of char buffer[16384], i.e.:

static char buffer[16384];

It could be your buffer is too large and is getting corrupted because of a stack overflow.

Other than those two things, I don’t see anything wrong with the snippet you sent. If the above doesn’t fix it, I’d have to see the actual code (or a simplified example that reproduces the problem).

---

<div class="post-metadata">

**Author:** ![egawtry](https://avatars.discourse-cdn.com/v4/letter/e/8e8cbc/32.png) [@egawtry](https://forums.digi.com/u/egawtry)\
**Post date:** [August 12, 2009, 2:23pm UTC](https://forums.digi.com/t/send-trashing-data/3589/4 "2009-08-12T14:23:10Z")

</div>

Scott,  
Thanks for your reply.  
I posted to the ConnectME forum, I am using a ConnectME 9210. Serial ports aren’t an issue, I am working with a Ethernet port that is messing up.

-Erik

P.S. The tag used to work a long time ago. Digi must have “updated” the forum software and broken it.

---

<div class="post-metadata">

**Author:** ![egawtry](https://avatars.discourse-cdn.com/v4/letter/e/8e8cbc/32.png) [@egawtry](https://forums.digi.com/u/egawtry)\
**Post date:** [August 15, 2009, 4:53pm UTC](https://forums.digi.com/t/send-trashing-data/3589/5 "2009-08-15T16:53:56Z")

</div>

Thanks for the reply.

I finally solved it - a rookie mistake. In a string before the code snippit I was sending a string, but stupidly put a sizeof() for the length. All the extra NULLs messed up the web browsers (both IE and mozilla). Switched to a strlen() and everything works.

-Erik
