Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8367375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T13:08:26+00:00 2026-06-09T13:08:26+00:00

I am trying to implement a network applicative protocol client library, composed of binary

  • 0

I am trying to implement a network applicative protocol client library, composed of binary request/responses over TCP connection, and I would like this client to be fully asynchronous relying on async/await constructs of c#5.

Each request sent on the NetworkStream contains an associated applicative sequence number. The reponse made to the request, must specify the same sequence number so that response can be matched with original request.
When I issue a request R1, response to R1 can of course come at any time in the future, and other responses for other requests may come on the line before the actual response to R1.

What I would like to do in the client code of the library is something as simple stupid as

var resp = await SendSomeRequestAsync(req);

SendSomeRequestAsync will send the request asynchronously on the line (got that part right) and somehow await the associated response (matching the sequence number that was sent in the request), such as (in SendSomeRequestAsync)

var dummy = await _ns.WriteAsync(rawBytes, 0, rawBytes.Length); // _ns is a NetworkStream
var resp = await GetResponseMatchingSequenceNumberAsync(sequenceNumber);

I have a loop that is launched when client start connection which is reading incoming responses on the connection asynchronously :

 while (true)
 {
    Response rsp = await ReadNextResponseAsync(_ns);
    DispatchReceivedResponse(rsp);
 }

I can’t figure out how to implement the GetResponseMatchingSequenceNumberAsync, or if I am already doing it totally wrong.

Hope my question is clear enough.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T13:08:27+00:00Added an answer on June 9, 2026 at 1:08 pm

    I’ve run into exactly this issue, and the following seemed pretty clean:

    Create an IDictionary<int,Response> and store TaskCompletionSource<Response> instances in it. When you get a response back, find the TaskCompletionSource and set it completed. I make no claims for the thread safety of this code. The dictionary should probably be of the concurrent kind, or at least accessed in a lock of some sort.

    public Task<Response> GetResponseMatchingSequenceNumberAsync(sequenceNumber)
    {
       var tcs=new TaskCompletionSource<Response>();
       pendingTasksDictionary.Add(sequenceNumber,tcs);
       return tcs.Task;
    }
    
    private void ResponseHandler(int sequenceNumber,Response response)
    {
       var pendingTcs=pendingTasksDictionary[sequenceNumber];
       //remove from dictionary
       pendingTcs.SetCompleted(response);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement an existing network protocol which makes heavy use of Unsigned
I'm trying to implement a CRC verification on a network based protocol. CRC calculation
I'm trying to implement OSGI bundle with network server which uses network sockets. This
I am trying to call HTTP request with network credential in blackberry. i have
Hy everybody, I'm trying to implement a neural network with matlab using nntool. Does
I am trying to implement a network core-periphery measure from an article (link: Borgatti
Good Day, We are trying to implement network load balancing of windows in one
I am trying to implement a wormhole attack detection program for network sensors. I
I am trying to send an object in java over a physical network (not
I'm trying to send the device name over a network implemented with CFNetwork. My

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.