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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:51:42+00:00 2026-06-13T05:51:42+00:00

I have a class that connects to another application over TCP/IP sents a request

  • 0

I have a class that connects to another application over TCP/IP sents a request (in the form of XML) and receives a response (also in the form of XML).

It does work however I have the following concerns:

1) It only works because of an arbitrary System.Threading.Thread.Sleep(5000); if I take this out it jumps straight to the end of the class with partial data. I need it to wait until it reaches the end of the stream or times out.
2) It is not very elegant

The entire class is listed below and any suggestions are very welcome.

public XDocument RetrieveData()
{
    // Initialize Connection Details
    TcpClient Connection = new TcpClient();
    Connection.ReceiveTimeout = Timeout;
    MemoryStream bufferStream = new MemoryStream();

    // Compose Request
    String Request = "";
    Byte[] Data = ASCIIEncoding.ASCII.GetBytes(Request);

    // Connect to PG
    IAsyncResult ConnectionResult = Connection.BeginConnect(IPAddress, IPPort, null, null);
    while (!Connection.Connected)
    {
        System.Threading.Thread.Sleep(1000);
    }
    Connection.EndConnect(ConnectionResult);

    NetworkStream ConnectionStream = Connection.GetStream();

    // Send the request
    ConnectionStream.Write(Data, 0, Data.Length);
    ConnectionStream.Flush();

    // TODO. Tidy this up - Wait to ensure the entire message is recieved.
    System.Threading.Thread.Sleep(5000);

    // Read the response
    StringBuilder Message = new StringBuilder();
    byte[] ReadBuffer = new byte[1024];

    if (ConnectionStream.CanRead)
    {
        try
        {
            byte[] myReadBuffer = new byte[1024];
            int BytesRead = 0;

            do
            {
                BytesRead = PGConnectionStream.Read(myReadBuffer, 0, myReadBuffer.Length);
                Message.AppendFormat("{0}", Encoding.ASCII.GetString(myReadBuffer, 0, BytesRead));
            }
            while (PGConnectionStream.DataAvailable);
        }
        catch
        {
        }
    }

    XDocument doc = XDocument.Parse(Message.ToString());
    return doc;
}
  • 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-13T05:51:44+00:00Added an answer on June 13, 2026 at 5:51 am

    This is the problem:

    while (PGConnectionStream.DataAvailable);
    

    That just checks whether there’s any data available right now. It can’t tell whether there’s more coming in the stream later.

    It’s not clear whether you need to accommodate multiple message in the same stream. If you don’t, it’s really easy: just keep reading until Read returns a non-positive value. Otherwise, you’ll need to consider a scheme for indicating the end of data:

    • A delimiter (difficult in terms of escaping the delimiter within a message)
    • Length-prefix (means you can’t start writing until you know how much data you’ll get)
    • Chunked (length-prefix, but on an individual chunk basis, with (say) a 0-length chunk to indicate the end of data)

    (As per Adriano’s comment, using async APIs when you really want to do everything synchronously is pointless… and your variable naming is inconsistent.)

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

Sidebar

Related Questions

I have a class that I instantiate to save or load xml data. For
I have another class that creates the connection (see refined class half way down
I have another PHP question for you. I have a class that is supposed
I have been asked to write a class that connects to a server, asynchronously
I have a Java application that connects to a MySQL database and uses a
I have application running on Weblogic. And I have another application which connects to
I have a class that extends AsyncTask. In the doInBackground() method, I connect to
I have class that extend FragmentActivity in it I add fragment to layout as
I have class that looks like this: class A { public: class variables_map vm
I have class that represents users. Users are divided into two groups with different

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.