Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket issue in windows form. Socket stop replying when change tab control

Tags:

c#

sockets

i've made some program that starts an asynchronous socket that handles all client request.

Socket starts before Form1 is called, and works fine

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        AsynchronousServer ascSv = new AsynchronousServer();
        Application.Run(new Form1());
        if (ascSv != null)
            ascSv.Stop();
    }
}

--Edited--- (Added AsynchronousServer.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Server
{
    public class StateObject
    {
        // Client  socket.  
        public Socket workSocket = null;
        // Size of receive buffer.  
        public const int BufferSize = 1024;
        // Receive buffer.  
        public byte[] buffer = new byte[BufferSize];
        // Received data string.  
        public StringBuilder sb = new StringBuilder();
    }

    public class AsynchronousServer
    {
        public ManualResetEvent allDone = new ManualResetEvent(false);
        Socket listener;
        public Thread t;
        public AsynchronousServer()
        {
            t = new Thread(StartListening);
            t.Start();
        }

        public void Stop()
        {
            try
            {
                listener.Shutdown(SocketShutdown.Both);
                listener.Disconnect(false);
                try
                {
                    listener.Close();
                    listener.Dispose();
                }
                catch { }
            }
            catch
            {

            }
            if (t!=null &&t.IsAlive)
            {
                t.Abort();
                t = null;
            }
            listener = null;
        }

        public void StartListening()
        {
            // Establish the local endpoint for the socket.  
            // The DNS name of the computer  
            IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress ipAddress = ipHostInfo.AddressList[0];
            IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("192.168.100.115"), 11000);

            // Create a TCP/IP socket.  
            listener = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);

            // Bind the socket to the local endpoint and listen for incoming connections.  
            try
            {
                listener.Bind(localEndPoint);
                listener.Listen(100);

                while (true)
                {
                    // Set the event to nonsignaled state.  
                    allDone.Reset();

                    // Start an asynchronous socket to listen for connections.  
                    //Console.WriteLine("Waiting for a connection...");
                    listener.BeginAccept(
                        new AsyncCallback(AcceptCallback),
                        listener);

                    // Wait until a connection is made before continuing.  
                    allDone.WaitOne();
                }

            }
            catch
            {
                //MessageBox.Show(e.Message);
            }
        }

        public void AcceptCallback(IAsyncResult ar)
        {
            try
            {
                // Signal the main thread to continue.  
                allDone.Set();

                // Get the socket that handles the client request.  
                Socket listener = (Socket)ar.AsyncState;
                Socket handler = listener.EndAccept(ar);

                // Create the state object.  
                StateObject state = new StateObject();
                state.workSocket = handler;
                handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                    new AsyncCallback(ReadCallback), state);
            }
            catch
            {

            }

        }

        public void ReadCallback(IAsyncResult ar)
        {
            try
            {
                String content = String.Empty;

                // Retrieve the state object and the handler socket  
                // from the asynchronous state object.  
                StateObject state = (StateObject)ar.AsyncState;
                Socket handler = state.workSocket;

                // Read data from the client socket.   
                int bytesRead = handler.EndReceive(ar);

                if (bytesRead > 0)
                {
                    // There  might be more data, so store the data received so far.  
                    state.sb.Append(Encoding.ASCII.GetString(
                        state.buffer, 0, bytesRead));

                    // Check for end-of-file tag. If it is not there, read   
                    // more data.  
                    content = state.sb.ToString();
                    if (content.IndexOf("<EOF>") > -1)
                    {
                        // All the data has been read from the   
                        // client. Display it on the console.  
                        //Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",content.Length, content);
                        // Echo the data back to the client.

                        //Choose what to do with the packet
                        string callBack= ClientController.GenerateResponseTo(content);
                        //Choose what server has to reply to client
                        Send(handler, @callBack+"*<EOF>");
                        //Send(handler, @"N:\tmp\2parts\save.cnf*1*<EOF>");
                    }
                    else
                    {
                        // Not all data received. Get more.  
                        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                        new AsyncCallback(ReadCallback), state);
                    }
                }
            }
            catch { }

        }

        private void Send(Socket handler, String data)
        {
            try
            {
                // Convert the string data to byte data using ASCII encoding.  
                byte[] byteData = Encoding.ASCII.GetBytes(data);

                // Begin sending the data to the remote device.  
                handler.BeginSend(byteData, 0, byteData.Length, 0,
                    new AsyncCallback(SendCallback), handler);
            }
            catch { }
        }

        private void SendCallback(IAsyncResult ar)
        {
            try
            {
                // Retrieve the socket from the state object.  
                Socket handler = (Socket)ar.AsyncState;

                // Complete sending the data to the remote device.  
                int bytesSent = handler.EndSend(ar);
                //Console.WriteLine("Sent {0} bytes to client.", bytesSent);

                handler.Shutdown(SocketShutdown.Both);
                handler.Close();

            }
            catch
            {
            }
        }


    }
}

Once i'm on GUI clients can connect to server to ask for they answer and server replies an sleep packet untill user inputs a valid path on GUI and clicks START.

Well that also works fine.

My issue is that when a client is succesfully conected it starts sending a callback to the server and server outputs its callback in a progressbar from a custom control i've made and that also "works" but the issue is:

If i keep the GUI on the server start tab. All works fine, clients get their answers and server adds and updates client's progress bar. BUT when i change the tab controll to the tab where the progress bar of all clients are displayed, server stops sending any replys to clients. Even if i go back to the server start tab it doesn't reply anymore.

I'm so hardstuck on this. Does anyone know where does my error come from?

I also upladed some pictures of a debug client and my issue:

Client connected to server and getting answers:

Client connected to server and getting answers

Changeing GUI tab and server stopping:

Changeing GUI tab and server stopping

like image 392
Aniol Avatar asked Dec 17 '25 05:12

Aniol


1 Answers

Problem solved. Thanks to Aram Kocharyan and Mong Zhu.

The error was that I was trying to access a GUI element from a different thread. To modify a GUI element from another thread must use Invoke method of controll (Invoke calls the method from the thread that owns the component)

Example:

Form1.GetInstance.flowLayoutPanel_progress.Invoke(
                new Action(() => Parse0(splited[1], out toReturn)));
like image 180
Aniol Avatar answered Dec 19 '25 23:12

Aniol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!