This project has moved. For the latest updates, please go here.

Async Request / Receive

Apr 23, 2014 at 8:55 AM
Hello Guys,

I'm using your wonderful library, my question is how can I match the correct request and response if I'm not putting it in a queue?

Appreciate your response in advance.
Coordinator
Apr 24, 2014 at 1:39 AM
I'm assuming that the responses can occur in any order (otherwise, you could just match the next response with the oldest request).

You'll have to add an identifier that makes sense for your type of message. If there isn't one that makes sense, then just use Guid.NewGuid(). The server would copy that Id into the response, and the client would then match the Ids to determine which response is for which request.

-Steve
Apr 24, 2014 at 5:43 AM
Edited Apr 24, 2014 at 5:44 AM
Thanks for reply Steve, this is what I've done before however it needs like to be put in queue for me to consolidate request and response data.
Another question, can I send just only the packet without sending the length? When I'm sending the length the remote host will not respond back.

I modified this method from SocketProtocol class:
public void WritePacketAsync(IAsyncTcpConnection socket, byte[] packet)
{
     //WritePacketAsync(socket, packet, null);

     //Simply send the packet
     WriteAsync(packet, 0. packet.length);
}
The remote host will respond back as it shows in Wireshark, but it will send RST, ACK due to the error handling on PacketArrived event.
Error usually shows: "Packet length less than zero (Corrupted mesage)"

What you will advise considering I'm not having control of remote host and all I know the remote host is designed to be a synchronous socket server.
Coordinator
Apr 24, 2014 at 2:56 PM
Yes, you will need to maintain a collection of sent requests to match the incoming responses against.

The protocols used in the example code use length-prefixing and regular heartbeats to detect a loss of connection. If you don't control the other side, then you'll just have to use whatever protocol they have defined.

I do have a TCP/IP .NET Sockets FAQ on my blog which goes into protocol design a bit. In particular, you should make sure their protocol has some kind of message framing and heartbeat.

-Steve
Apr 27, 2014 at 3:05 PM
Hello Steve,

Thanks for your never-ending help, actually I cannot control the remote host where my project is connect and I cannot embed message heart beat too. There are some limitations for us, my only goal is to see if request packets where properly sent to remote host without dropping the connection.

Anyhow I read your FAQ and it clearly help me to draw the line what could I create from this library.


Cheers!
techguy