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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:30:25+00:00 2026-06-17T13:30:25+00:00

I have a TCPClient that creates a stream that I read from when DataAvailable.

  • 0

I have a TCPClient that creates a stream that I read from when DataAvailable.

Every 20 seconds that !DataAvailable I ping the socket with an ACK message to keep the stream from closing.

But I seem to be getting mixed results. It seems like every other time I open the stream(basically restart my Service) I get transport errors.

This is a shortened version of my Connect function:

client = new StreamClient();
client.Connect(new IPEndPoint(clientAddress, senderPort));
stream = client.GetStream();
bool status = SendMessage(seq, sync, MessageTypes.Init);

The SendMessage function does:

if (stream == null) return false;
stream.Write(TransmitBuffer, 0, TransmitMessageLength);

My Close function does:

if (stream != null)
{
    SendMessage(seq, sync, MessageTypes.Finish);
    stream.Close();
}
stream = null;

client.Close();
client = null;

It is expected that the SendMessage calls will fail occasionally due to nature of the socket.

But sometimes, once I Connect, everything runs fine, no failed messages. But other times the ACK’s will fail. When the ACK’s fail, I call Close, which will force a Connect and validate the other end of the socket is open. If that fails then I know that end is down. But sometimes that call doesn’t fail and then 20 seconds later the ACK does.

Can anyone give me an opinion on why this may happen? Is 20 seconds too long to wait? Am I not closing my end of the socket properly?

The specific error message i’m fighting with is:

Unable to write data to the transport connection: An established connection was aborted by the software in your host machine.

And it occurs at stream.Write(TransmitBuffer, 0, TransmitMessageLength);

  • 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-17T13:30:27+00:00Added an answer on June 17, 2026 at 1:30 pm

    The main thing I see in your implementation that jumps out at me is that it looks like you’re treating the Stream as the Connection, and it isn’t. Your checks on the Stream instance should instead be checks on the TcpClient instance. I’m not sure if that’s the source of your problem, but it definitely looks strange to me.

    Instead of this:

    stream = client.GetStream();
    if (stream != null)
    {
        SendMessage(seq, sync, MessageTypes.Finish);
        stream.Close();
    }
    stream = null;
    

    I usually do something more like this:

    if (client != null)
    {
        if (client.Connected)
        {
            client.GetStream().Close();
        }
    
        client.Close();
        client = null;
    }
    

    You should be checking TcpClient.Connected before working with the stream, not the stream itself.
    Another thing I would mention is to be sure to always use the async methods to connect, read, and write with your TcpClient. The synchronous ones are easier, but my experience has been that relying on them can get you into trouble.

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

Sidebar

Related Questions

I have a TcpClient application that send a message from client to the server.
I have a listener that returns a TcpClient that must read data until the
I have some pretty simple code that reads lines from a network stream that
I have a class that derives from TcpClient, e.g. class ZClient : TcpClient {
I have 3 types, (TCPClient, string, class) Note that the class is defined at
I needed a class that would have everything TcpClient has + a couple extra
I have wrote a simple client that use TcpClient in dotnet to communicate. In
I have a thread that uses TcpClient 's BeginAcceptTcpClient() method. It resembles: // this
I have a class which reads data from port through TcpClient. I've created 2
I have a simple program that uses TcpClient and SslStream to write data to

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.