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 6202023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:39:32+00:00 2026-05-24T04:39:32+00:00

As i am bit new to Rx and learning my way through it. I

  • 0

As i am bit new to Rx and learning my way through it. I checked out lots of examples out there but none fits my need.

Scenario : I have one socket server socket (created using simple socket object and not TCPListener object). I have multiple clients (client socket and not TCPClient) connected to this server socket. I am trying to use Rx (Reactive extension) to get the messages sent by client socket by using “FromAsyncPattern” operator for BeginReceive and EndReceive asynchrounous operation.

I have almost started getting the messages in the buffer. But the issue is that the buffer return either the same message received from previous operation or will have mix content from current and previous receive operation.

Here is the code used :

    private void ReceiveMessageFromClient(Socket socket)
    {
        try
        {
            if (socket != null)
            {
                byte[] buffer = new byte[400];

                var functionReceiveSocketData = Observable.FromAsyncPattern
                                                            <byte[], int, int, SocketFlags, int>(socket.BeginReceive, socket.EndReceive);

                Observable.Defer(() =>
                                functionReceiveSocketData(buffer, 0, 400, SocketFlags.None)
                                ).Repeat().Subscribe(onNext: x => OnMessageReceived(buffer));
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }

OnMessageReceived method is just an delegate to raise the message received event and send the buffer to other class to process further.

Problem : The same buffer is used evertime i get message how can i get the message received clean from previous received buffer.

— What should be done if i have message received partially and the next message is actually part of this same message.

Please help me out with the above issue with some code snippets which would be greatful. Reason of posting this question is cause the implementation i have gone through till now are using TCPListener and here the constraint is to use socket object 🙁

Thanks in advance.

  • 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-05-24T04:39:33+00:00Added an answer on May 24, 2026 at 4:39 am

    The issue you’re hitting is that you’re sharing the same instance of your buffer between repeated calls to your observable. You need to ensure that you have a new buffer instance each time. It also appears that you should truncate the buffer based on the return integer. Here’s what I suggest that needs to go in your if statement:

    var functionReceiveSocketData =
        Observable.FromAsyncPattern<byte[], int, int, SocketFlags, int>
            (socket.BeginReceive, socket.EndReceive);
    
    Func<byte[], int, byte[]> copy = (bs, n) =>
    {
        var rs = new byte[n];
        bs.CopyTo(rs, 0);
        return rs;
    };
    
    Observable
        .Defer(() =>
        {
            var buffer = new byte[400];
            return 
                from n in functionReceiveSocketData(buffer, 0, 400, SocketFlags.None)
                select copy(buffer, n);
        })
        .Repeat()
        .Subscribe(x => OnMessageReceived(x));
    

    EDIT: In response to the comment re joining message parts. Also fixed the solution above – should be 0 and not n in the CopyTo function in the copy lambda.

    I’m not an expert on using sockets, but, because the socket is (and correct me if I’m wrong) just a stream of bytes, you would need to be able to determine when each message in the stream is complete.

    Here’s one possible way to do it:

    /* include `functionReceiveSocketData` & `copy` from above */ =
    
    Func<byte[], byte[], byte[]> append = (bs1, bs2) =>
    {
        var rs = new byte[bs1.Length + bs2.Length];
        bs1.CopyTo(rs, 0);
        bs2.CopyTo(rs, bs1.Length);
        return rs;
    };
    
    var messages = new Subject<byte[]>();
    messages.Subscribe(x => OnMessageReceived(x));
    
    Observable
        .Defer(() => /* as above */)
        .Repeat()
        .Scan(new byte[] { }, (abs, bs) =>
        {
            var current = append(abs, bs);
            if (isCompleteMessage(current))
            {
                messages.OnNext(current);
                current = new byte[] { };
            }
            return current;
        })
        .Subscribe();
    

    The only thing you’d need to do is figure out how to implement the isCompleteMessage function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I wrote this little bit of code to try out some new way
I'm still relatively new to hadoop, and I've been learning a bit about it
I'm a bit new to Rails/RSpec/Capybara, so this is probably a newbie question, but
I'm a bit new to js and have been trying to figure out how
I am a bit new to MySQL and trying my hands on learning it.
CD's been an enormous learning curve for me and there's still a bit for
I am new to Java (but have a fair bit of .NET experience). I
New to C# I have done a good bit of reading on learning the
I'm a bit new to linq and I'm unsure if what I desire is
I'm a bit new to wordpress plugin coding. I've been working on a plugin

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.