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

  • Home
  • SEARCH
  • 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 8066741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:05:56+00:00 2026-06-05T12:05:56+00:00

I need to connect a server (with ip and port) and create a read-loop

  • 0

I need to connect a server (with ip and port) and create a read-loop that will get messages from the server as XML. sometimes there are no messages from the server.

I tried to create a connection (works fine) and read messages, I get the first message from the server and when I’m trying to read another one – it get stuck. I think that maybe there are no messages right now but I need that the loop will continue until there will be messages… it doesn’t even go to “catch” or “finally”, just do nothing..

public class Connection
{
    public Connection()
    {
        Socket server = null;
        try
        {
            string p = string.Empty;
            using (var client = new TcpClient(myIPAddress, myPort))
            using (var stream = client.GetStream())
            using (var reader = new StreamReader(stream))
            {
               while (p != null)
               {
                    try
                    {
                        p = reader.ReadLine();
                    }
                    catch (Exception e)
                    {
                       //
                    }
               }
            }
        }
        catch (Exception e)
        {
            //
        }
        finally { 
            server.Close(); 
        }
    }
}
  • 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-05T12:05:57+00:00Added an answer on June 5, 2026 at 12:05 pm

    The loop is continuing, waiting for data. The issue here seems to be simply that ReadLine() is a blocking call. You mention that there might not be a message yet; well, ReadLine() is going to block until one of two conditions is met:

    • it can successfully read some data, terminated by a newline (or EOF, i.e. a message without a newline followed by socket closure) – in which case it returns the line of data
    • no more data is received and the stream is closed, in which case it returns null

    So basically, ReadLine() is going to wait until either a message comes in, or the socket is closed. That is simply the behaviour of ReadLine(). If that is problematic, you could work closer to the socket, and check NetworkStream.DataAvailable but: note that only tells you if some data is currently available; it doesn’t mean “this is an entire message”, nor can it be used to tell if more messages will arrive. The main use of DataAvailable is to decide between sync and async access. Plus if you work close to the socket you’ll have to do all your own buffering and encoding/decoding.

    It looks to me like ReadLine() is working successfully. The only thing I would do here is re-phrase it a bit:

    string line;
    while((line = reader.ReadLine()) != null) {
        // line is meaningful; do something
    }
    

    One last thought: xml is not always trivially split into messages simply on a “per-line” basis. You might want to consider some other form of framing, but that may well mean working closer to the socket, rather than a StreamReader.

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

Sidebar

Related Questions

I need to create a bash script which will connect to an FTP server,
I need to create a server socket, which should reject a connect request, if
I need to create Client/Server application to send files from clients to Server. I
Well, I need to create hsqldb in server mode and connect to this server
I need connect to server git. http://help.github.com/win-set-up-git/ I do everything to the point, 4
i need to connect to another server we manage and have it's results (in
I need to connect to a SQL Server 2008 through PHP (WAMP, latest version).
I need to use PHP to connect to a URL via a proxy server.
I need to allow a consultant to connect to SSIS on a SQL Server
I need to connect to Oracle9,10 and 11 in my java application.The client 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.