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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:13:51+00:00 2026-06-03T17:13:51+00:00

I am getting into trouble with this part of code. In fact I want

  • 0

I am getting into trouble with this part of code.
In fact I want to set a Client/Server Application.
In the client part I launch a Thread which function is only to check everytime if it is connected to the server (if the connection to the server is still established)
TraceLog is a class that uses its Info() method to write in a file.
this is is the client code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;

namespace ClientApp
{
    class ClientOpenConnection
    {
        private static Thread threadConnect;
        static TcpClient myClient = new TcpClient();
        static String host = "";
        static Int32 port = 0;

        //Function that makes the client runs
        public static void RunClient(String hostname, Int32 hostport)
        {
            host = hostname;
            port = hostport;
            int _tryAgain = 0;

            while (!myClient.Connected) {
                try
                {   //I start the connection
                    myClient.Connect(host, port);
                }
                catch {

                }
                _tryAgain += 10;
                if (_tryAgain == 1000)
                    break;
                 //_tryAgain allows me to define how long will the client try to connect to the server.
            }
            TraceLog.Info("Out of the while ", ""); // This is to know where am I

            if (_tryAgain != 1000)
            {    //If I get out because _tryAgain is less than 1000. It means that I am already connected to the server
                //Here I start a Thread to be sure that I am always connected to the server
                threadConnect = new Thread(isConnected);
                threadConnect.Start();
                TraceLog.Info("Launch the thread","");
            }
            //While threadConnect is executing parallely I continue my program
        }

        private static void isConnected() { 
            //I keep my eyes on the network connection
            while (myClient.Connected) { 
                //Nothing is done
            }
            TraceLog.Info("The connection has been lost","");
            RunClient(host,port);
        }
    }
}

The problem that I am having, when I start the client before the server I enter the first WHILE loop. it is OK at this level.
and when I start the server after, I launch the threadConnect but the problem is that if now I stop the server, normally i should have inside the log file “The connection has been lost” but I have nothing.
What is wrong with this part of code?
Have you already done something like this in the past?

I come with a modification but still having problem to obtain what I want, ie the client still get trying to contact the server eveytime even if the server is stopped .

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;

namespace ClientApp
{
    class ClientOpenConnection
    {
        private static Thread threadConnect;
        static TcpClient myClient = new TcpClient();
        static String host = "";
        static Int32 port = 0;

        //Function that makes the client runs
        public static void RunClient(String hostname, Int32 hostport)
        {
            host = hostname;
            port = hostport;

            TraceLog.Info(" -> "+myClient.Connected,"");

            while (!myClient.Connected) {
                try
                {
                    myClient.Connect(host, port);
                    TraceLog.Info(" <-> " + myClient.Connected, "");
                }
                catch {
                    TraceLog.Info("Trying to contact the server","");
                }
            }
            TraceLog.Info("I am connected ", "");

            //Here I start a Thread to be sure that I am always connected to the server
            threadConnect = new Thread(isConnected);
            threadConnect.Start();
            TraceLog.Info("Launch the thread to be sure I am constantly online","");
        }

        private static void isConnected() { 
            //I keep my eyes on the network connection
            TraceLog.Info("->>"+myClient.Connected,"");
            while (myClient.Connected) {

                Thread.Sleep(500);

                try
                {
                    NetworkStream stream = myClient.GetStream();

                    ASCIIEncoding ascii = new ASCIIEncoding();

                    byte[] _incomingMsg = new byte[1024];

                    stream.Read(_incomingMsg, 0, _incomingMsg.Length);

                    String strToGet = System.Text.Encoding.ASCII.GetString(_incomingMsg);

                    strToGet = strToGet.Trim();

                    if (!strToGet.Equals("ONLINE"))
                        if (strToGet.Equals(""))
                        {
                            TraceLog.Info("The message receive is empty","");
                            break;
                        }
                }
                catch {
                    break;
                }
            }          
            TraceLog.Info("The connection has been lost", "");
            RunClient(host, port);
        }
    }
}

But when I call the RunClient() in the isConnected() function it executes in the WHILE and output TraceLog.Info(“Trying to contact the server”,””); even if I start the server again, the client remains in the while loop and never connects at all.

  • 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-03T17:13:53+00:00Added an answer on June 3, 2026 at 5:13 pm

    From MSDN:

    The Connected property gets the connection state of the Client socket
    as of the last I/O operation. When it returns false, the Client socket
    was either never connected, or is no longer connected.

    Because the Connected property only reflects the state of the
    connection as of the most recent operation, you should attempt to send
    or receive a message to determine the current state. After the message
    send fails, this property no longer returns true. Note that this
    behavior is by design. You cannot reliably test the state of the
    connection because, in the time between the test and a send/receive,
    the connection could have been lost. Your code should assume the
    socket is connected, and gracefully handle failed transmissions.

    In other words, in order to check if you are still connected, you need to send or receive some data and then check the connection state.

    Since your code doesn’t send any packets after the connection is made, the connected property always returns true, and the loop never exits.

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

Sidebar

Related Questions

I'm writing a ASP.NET Web Application and I'm running into trouble getting the CSS
Just getting into the NoSQL stuff so forgive me if this is a simple
I am just getting into more client-side stuff in ASP.NET using Javascript, and there's
I'm getting into SDK documentation, but this site has so many eyes and great
I have ran into a bit of trouble getting my pong game to work,
I found this snippet of code here that allows you to log into a
I'm having trouble getting this to work. I'm writing a Vim snippet that will
I'm having trouble getting this loop to return all of the rows that I
I am having trouble getting this to work correctly (obviously) - I am ALMOST
I have an application that uses several instances of getJSON, and I'm getting into

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.