1

Background:

The application I am programming uses async sockets (using BeginSend, EndSend, BeginReceive, EndReceive) to send data between each other. The sockets are TCP, no socket flags, on IPV4.

It uses the system where it sends a 4-byte (int) message, followed by a message with the length specified in the previous message. I use function helpers that handle the MessageLength, and the MessageBody. The flow is something like this

  1. BeginReceive()
  2. EndReceive()
  3. MessageLengthReceived()
  4. BeginReceive()
  5. MessageBodyReceived()

Issue:

The issue arrives when I send file data, in chunks of 16kb (with an additional small overhead: offset, pieceIndex, etc). Occasionally, when receiving the MessageLength, it receives a data from a random part in the previous message, instead of the actual message length. Part of this issue is that it doesn't always happen at a set offset (eg beginning or end of file / piece / 16 kb chunk) and can happen with any file, but happens more if I send a lot more files / larger files.

There are internal messages that are sent (eg RequestMessages) that never experience this problem. All the internal messages are < 100 bytes.

I've tried waiting for the file chunk to save completely before requesting another chunk, but it still fails. I've also tried limiting how many chunks to send at a time, but this only resolves the issue when using 127.0.0.1 (local clients), and not cross network (LAN).

I've spent hours going through my application to see if there's any issues, but I have yet to see any where it would be sending the wrong data as a header. The issue always seems to inbetween the send and the receive of the two clients. Is there settings for socket / method of sending that I should be using? Or could it be some sort of race condition (I thought about race condition, but the fact that the data can be anywhere randomly in a file made me rethink this).

2
  • Sounds like a bug in your code. It's going to be a guessing game without a short self contained code example.
    – spender
    Apr 27, 2012 at 20:09
  • You're probably right. I'm busy at work atm, I'll see if I can add some more specific code later tonight.
    – Mike
    Apr 27, 2012 at 21:20

1 Answer 1

0

From the question, i guess the problem you are dealing with is inside the MonoTorrent library.

I myself has never encountered such problem. and by looking at the codes, i think the receive part is already ordered because the network IO will not try to receive a second message until the first one has been handled. PieceMessages' write requests are queued in DiskIO also so that should not be the problem.

however, in sending procedure, the ProcessQueue function can be called from several places. and the EnqueueSendMessage called by ProcessQueue indirectly doesn't actually enqueue the message to any queue. it just simply call the Socket.BeginSend. I don't know if Socket.BeginSend() has any queue mechanism inside. If there is not, this may bring some problem when multiple threads are trying to make the same socket "BeginSend" different data.

1
  • Not entirely monotorrent, but very similar (I made a few custom modifications). The piecemessage length changed, and therefor the ChunkLength was wrong. I updated it accordingly and it worked fine. Kudos for the connection though!
    – Mike
    May 4, 2012 at 23:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.