Receive only give me part of my data...

Posted on 2004-04-27
Last Modified: 2013-11-20
I have this Java server app that is sending data to my MFC (VC++6.0) app. The data is just over 3100 bytes long but for some reason i'm only getting 3000+ bytes and then I get another "OnReceive" message after i process this one.

Should I assume that this means that my data was split up into parts and WINSOCK is only giving me bits of it at a time?

Is there an easy way to make sure all the data is sent all at once in one BIG part instead of in chunks?

If it isn't possible to receive all of it at once, is there a way to read beyond the CSocket::Receive buffer? In other words, fake out an "OnReceive"?

-- Bubba
Question by:bganoush
  • 4
  • 4

Expert Comment

ID: 10931913
not sure about reading all the data at once.  But would it be possible to have the Java server append a termination pattern to it's packet data.  Then have MFC read a packet, find no termination so it sets the data aside so it can be appended to by the data from the next packet until the end of the collected data reads out as the termination pattern...

Hope someone can find a simpler solution =)

Author Comment

ID: 10932609
That's what I thought but the problem is that it allows the user to fiddle with stuff and send other requests in the mean time... Things could probably get very hairy.

My recent try just stored the incoming data (which is prepended with a length word) until all the data arrives but it's very messy and I gave up on the solution.

-- Bubba

Expert Comment

ID: 10932931
If you can't read the data all at once, you will have to specify a division somehow.  Packets are not pretty, just be glad the COM handles as much as it does :s

It is always easier if the the packet divsions are put in place by the programmer and not forced upon by translation.  So another method would be to split the server packet to portions below 128 bytes as this would alow for nearly all MTU (Maximum transfer Unit) sizes.  Can the 3100bytes be broken into any logical portions? (ie. is the packet 1 massive clump or is it a series of smallerintegers and characters).

Massive Clump:
Create a smaller packet that includes an integer packet identifier, and integer index and a portion of the data.  The identifier will remain the same until the entire clump of data has been sent,  then the identifier increments.  The index will start at 0 for any given identifier and will increment with each new packet, then reset to zero when the identifier is increased.  The client code can then use the identifier and index to piece the clump back together... Yes this is 'hairy'

Logical Portions:
Easiest way is to split the data into smaller packets of related data.  You would end up having several different packets, but all will have an identifier at the begging to tell the client what type of packet it is handling.  Then when data is changes, you send only the packet required to modify that data.   This is the most popular method as it limits network traffic.

Hope this helps :0

Author Comment

ID: 10938904

Well that's what I ended up doing but now there's something totally different happening. I now have the packets coming in a little at a time but it seems that the code is "going too fast"... and i'm losing some packets somewhere.

When I added some debugging statements that append data to the end of a log file just before my read, ALL the packets arrived with no loss. go figure!

Is it possible that I'm getting some receive notifications before the data is actually written to the socket stream?

-- Bubba
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.


Expert Comment

ID: 10939868
I would have to brush up on CSocket...  There is almost no way that the packets are lost in transite between the server and client because if the network drops a packet, the NIC and network layer of the computer will makie sure it is re-transmited.  So the computer almost guarantees that the non-streaming packets will get there.  This narrows the problem to the client.

So if your log file says that all the packets where recieved... Then what is telling you that packets are missing?

TEST:  try the following to see if this might be a multi-threading issue (new call to Recieve before current call is finished processing).  Place a TRACE or log file output at the start AND end of the packet recieving function.  Have the TRACe include a counter that increments each time the Recieve function is called.  What we are looking for is an output similar to the one below, if the numbers are out of order, then there is a Multi-thread problem.

Start 1
End 1
Start 2
End 2
Start 3
End 3

CODE for the traces:

// global integer
int iCount=0;

// function header
    // make a copy of the count for this instance of the function call
    int iNum = ++iCount;
    TRACE("Start %d",iNum);

/*** Do Processing Here ***/

    TRACE("End %d",iNum);

Sorry that this isn't a solution to the problem, but it should help gain some insight into the problem

Keep on Codin' =)

Author Comment

ID: 10948290

Well... I do have a trace on the server which is spitting out 18 lines of data being sent... I also numbered the data to see which lines I receive and which I don't.

So far the client only seems to receive all the data when I log the received packets as they come in.  Now without this logging (which slows the client on receive), the window that displays the received packets sometimes shows 18 packets, 12 packets 7 packets, 5 packets, 10 packets......

Accepted Solution

r2far earned 300 total points
ID: 10951019
Is this continous update, or a button triggered one-shot event?  I can't explain why you would not be recieving all the packets, the app should recieve all the sent packets from the network layer (which demands re-send on packet loss), the question is what happens after this.  Unless you are using a streaming socket... Streaming sockets sacrifice the re-send of lost packets in exchange for extra sending speed, this is to incorporate streaming media files which require high transfer rates which are not possible with datagram sockets.

Just for testing purposes, try slowing the packet sending from the server to a packet every half a second or so (if possible).  If you manage to get all the packets, then Recieve() may be being called repeatidle before it has a chance to complete it's operations.

Author Comment

ID: 10958634

I pondered your questions and realized that the problem was not even in this code, it was in the server code.

-- Thanks

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
GIF file bit manipulation for color change 5 107
Excel Use VBA to get user's Mac Address for their computer 5 117
has22 challenge 11 80
NotAlone Challenge 20 80
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now