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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:01:03+00:00 2026-05-25T16:01:03+00:00

Writing a client / server program and previously was using calls like this: TcpListener.AcceptTcpClient()

  • 0

Writing a client / server program and previously was using calls like this:

TcpListener.AcceptTcpClient() NetworkStream.Read() NetworkStream.Write()

TcpListener.AcceptTcpClient() is working fine because its callback function is only called after a connection is made.

However, BeginRead() is not working as well. For some reason it executes and continues past it even when no data is sent.. Not sure what is going on. NetworkStream.Read() was blocking just fine but this isn’t..

Here is the non-working thread that is created whenever a connection is established:

private void HandleClientCommunication(object client)
{
    TcpClient tcpClient = (TcpClient)client;
    NetworkStream clientStream = tcpClient.GetStream();

    try
    {
        byte[] header = new byte[4];

        clientStream.BeginRead(header, 0, header.Length, Read, new StateData(clientStream, header)); // Runs even if no data is sent

        int dataLength = BitConverter.ToInt32(header, 0); // Continues to this, even if no packets are sent..

        byte[] data = new byte[dataLength]; // dataLength is 0 still of course because none of the code is blocking

        clientStream.BeginRead(data, 0, dataLength, Read, new StateData(clientStream, data));
    }
    catch (IOException e)
    {
        Console.WriteLine(e.InnerException.Message);
    }

    // not relevant past here
}

Here’s the Read method too. Not sure if it’s needed to figure out the problem but I’ll include it regardless:

private void Read(IAsyncResult async)
{
    StateData stateData = (StateData)async.AsyncState;

    stateData.BytesRead += stateData.Stream.EndRead(async);

    if (stateData.BytesRead < stateData.Bytes.Length)
    {
        stateData.Stream.BeginRead(stateData.Bytes, 0, stateData.Bytes.Length, Read, stateData);
    }
    else
    {
        string message = Encoding.ASCII.GetString(stateData.Bytes);

        Console.WriteLine(message);
    }
}

So yeah I’m just trying to set up a client server interaction with asynchronous calls and have every “packet” include a header with the length of the data as an int and then right after that the data itself. Unfortunately, things seem to not be working and I’m not sure what the problem is.

Seems like I have to include some manual form of blocking but doesn’t that defeat the purpose of using asynchronous calls? Why not just go back to regular synchronous blocking calls like Read() and AcceptTcpClient() at that point then?

I don’t think I am fully understanding things here..

  • 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-25T16:01:04+00:00Added an answer on May 25, 2026 at 4:01 pm

    However, BeginRead() is not working as well. For some reason it executes and continues past it even when no data is sent.. Not sure what is going on. NetworkStream.Read() was blocking just fine but this isn’t..

    I think you’ve misunderstood what BeginRead is meant to do. It’s not meant to block – it issues an asynchronous read, executing the given callback when the read has completed. It’s meant to return immediately – so it’s no wonder your header data is empty.

    Basically, any work which requires the data should be in the callback – not in the same method which calls BeginRead. Your first callback should convert the header data and then BeginRead again with a different callback to read the message data itself… although you should also bear in mind that any Read/BeginRead call may not read all the data you’ve asked for. You’re trying to account for this in your callback, but you’re not quite there – because you’re always trying to fill the buffer from the start – which will overwrite any existing data. If you know how much data you’re expecting overall and your buffer is that long, you should read into the buffer starting at how many bytes you’ve already read.

    Note that C# 5 will make all of this a lot simpler – but if you can’t wait until then, you may well find it easier to go back to blocking calls on multiple threads (one per client).

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

Sidebar

Related Questions

I'm actually writing an MPI program. This is a basic client / server pattern.
Hi I am writing a simple client-server program. In this program I have to
This is a conceptual programming question. I'm writing a tcp server/client program in Java.
I am writing simple client-server program. Client send some messages to server using UDP
I'm writing a client for a server program written in C++. As is not
I'm writing a client application to communicate with a server program via UDP. The
I`m writing client-server app for windows using WinSock and I have class for server.
I'm writing a client-server app using BSD sockets. It needs to run in the
i am writing a simple client/server chat program with indy 10 (blocking mode) and
I am writing a simple C# tcp client and server program. The server will

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.