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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:03:16+00:00 2026-06-05T16:03:16+00:00

I have a class the communicates with a device over TCP. I send commands,

  • 0

I have a class the communicates with a device over TCP.
I send commands, and then issue stream.Read(reply, 0, 1) on the TcpClient’s GetStream().
I read out every single byte, and then combine the read bytes in a string, and compare the string to a termination string which lets me know when to stop reading.

There are some flaws in the device software, so that the termination string is not always sent. I would like to handle this in a proper way and using a period of time to determine whether the read should be stopped.

If I set the stream.ReadTimeout to something other than -1, then the socket will be closed when this timeout is exceeded, which I don’t want it to.

If I use the stream.BeginRead(..) with a stream.ReadTimeout = -1 then I will be sucking dry the thread pool after a while.

What intelligent solution can be constructed to address this issue?

  • 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-05T16:03:17+00:00Added an answer on June 5, 2026 at 4:03 pm

    I managed to implement something using stream.DataAvailable.

    It goes like this:

    do
    {
        try
        {
            if (socket.Poll(socketTimeout, SelectMode.SelectRead))
            {
                socket.Receive(reply, 0, 1, SocketFlags.None);
            }
            else
            {
                throw new SocketException((int)SocketError.TimedOut);
            }
            replyString.Append(Encoding.ASCII.GetString(reply, 0, reply.Length));
            // Termination string logic...
        }
        catch (SocketException e)
        {
            // Check on e.SocketErrorCode if necessary.
            break;
        }
    
    } while (!terminationReached);
    

    where socket = _tcpClient.Client;

    Edit:
    socket.Receive also closes the socket on timeout, socket.Poll does not.
    Changed to using socket.Receive(..) instead of stream.Read(..).

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

Sidebar

Related Questions

I have developed a USB device that communicates with linux over a simple but
I have a class that communicates with a server (let's call it 'Downloader'). Sometimes,
I have a QThread derived class that communicates with the main thread by sending
I have a class that represents an external physical measuring device. The simplified version
I have a class that communicates with a web service and is used throughout
I have an android application which (over the network) communicates with a desktop server
I have an application that communicates to server. I want my application to READ
I have a Singleton class that manages the connection to an external device. The
I have created a library class CircuitController that communicates with a control circuit via
I have a class which communicates with an API and needs to do some

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.