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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:01:05+00:00 2026-05-27T21:01:05+00:00

I am writing a client-server application using TCP Sockets. The server is written in

  • 0

I am writing a client-server application using TCP Sockets. The server is written in C# using Visual Studio 2010 (.NET 4.0). The server allows multiple client connections at the same time therefore I am using the Async. style. The server needs to connect to a database (MySQL) and while the server program is running I need to interact with the database. For this reason I am using a BackgroundWorker to run the infinite listening loop (that will Accept Clients) in order to prevent the interface from freezing and be able to access the database from the visual interface.

Everything is working fine unless when the server program losses focus. Examples of losing focus are: the PC that is running the server program starts the screensaver, you click on the clock on the task-bar to see the time, you open another widow (windows explorer, calculator…). If the server program looses focus and a client tries to connect, the server closes completely! If I am running the server from Visual Studio it does not throw any error or exception. If I am running a released version of the server, Microsoft Windows will show the “Programming not responding…” dialog.

Here is how my code is structured:

Variables in the main class:

const int portNo  = 12345;
static System.Net.IPAddress localAdd = System.Net.IPAddress.Parse("192.168.1.67");
static TcpListener tcpListenerReadWrite  = new TcpListener(localAdd, portNo);

In the main Form a button starts the server:

private void button1_Click(object sender, EventArgs e)
{
    tcpListenerReadWrite.Start();
    backgroundWorker1.RunWorkerAsync();
}

The background worker DoWork function:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    while (true)
    {
        ChatClient clientRead = new ChatClient(tcpListenerReadWrite.AcceptTcpClient());
    }
}

In the Server Class, the constructor is as follows:

public ChatClient(TcpClient client) //constructor
{
    _client = client;
    _clientIP = client.Client.RemoteEndPoint.ToString();

    //start reading data from the client in a separate thread
    data = new byte[_client.ReceiveBufferSize];
    _client.GetStream().BeginRead(data, 0, Convert.ToInt32(_client.ReceiveBufferSize), ReceiveMessage, client);
}

When a message is received by the server:

public void ReceiveMessage(IAsyncResult asyncResult)
{
    //read received message from client
}

Two final notes: (1) when a client connects to the server the first thing it does is sending a message to the server like a Hello message, (2) before writing the server in a visual interface I was testing it in a Console mode and this problem was not existing, the screensaver used to run and nothing happened. Sure in the console mode I didn’t use any BackgroundWorker.

Any ideas why the server is crashing?

  • 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-05-27T21:01:05+00:00Added an answer on May 27, 2026 at 9:01 pm

    I found what was crashing my server (it took me some time because I was lately focusing on the client programming, it is today when I returned to the server programming). As @Moo-Juice suggested, it is a problem related to accessing the UI. In fact here is the scenario that was causing the crash:

    1. The server starts
    2. It enters into an infinite loop listening for clients to connect. This infinite loop is in the backgroundWorker.
    3. A client connects to the server.
    4. I print a message on a textBox that a client has connected. //here is the problem
    5. To be able to establish point (4) I am using the backgroundWorker.ReportProgress method.

    To review the problem, the server crashes only if the focus is not on the program (a screen saver is running for example), but no crashing if the focus is on the program and logging is working. When I stopped the logging things went OK. So know I know the source of the problem but I don’t know why it is causing the crash; as I know the ReportProgress is designed to be use to report progress to the user interface. Anyway I will try to solve this now especially that I know from where to go. In case you are interested to see how I was doing the logging here is the function that I used:

    private void PrintString(string stringToPrint)
    {
        var myForm = Form1.ActiveForm as Form1;
        myForm.backgroundWorker1.ReportProgress(1, stringToPrint);
    }
    

    And here is the ReportProgress function:

    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        textBox1.Text += (string)e.UserState;
        textBox1.Text += Environment.NewLine;
    }
    

    Nothing so fancy but enough to cause me headache 🙂 Thanks for all your help.

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

Sidebar

Related Questions

I'm writing a client/server application in Java and I'm using TCP to transfer data
I'm writing a client-server app using BSD sockets. It needs to run in the
I'm writing a simple client/server application and I found that using DataInputStream to read
I am writing an application that communicates using sockets. I have a server running
I am writing a client-server application using Java-RMI. Some server-side ressources have to be
I'm writing single-page web application. Client interacts with server using AJAX retrieving quite complex
I'm writing a client application to communicate with a server program via UDP. The
I`m writing client-server app for windows using WinSock and I have class for server.
I'm writing a client for a server program written in C++. As is not
I'm using C to implement a client server application. The client sends info 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.