Windows窗体中的套接字问题。更改选项卡控件时套接字停止回复

时间:2019-03-20 08:00:04

标签: c# sockets

我编写了一些程序来启动一个处理所有客户端请求的异步套接字。

套接字在调用Form1之前启动,并且工作正常

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();
    }
}

-编辑---(添加了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
            {
            }
        }


    }
}

一旦我进入GUI,客户端就可以连接到服务器以要求他们应答,并且服务器会回复睡眠数据包,直到用户在GUI上输入有效路径并单击“开始”为止。

也可以。

我的问题是,成功连接客户端后,它将开始向服务器发送回调,并且服务器在进度条中从我进行的自定义控件中输出其回调,这也“有效”,但问题是:

如果我将GUI保留在服务器的“开始”选项卡上。一切正常,客户端获得答案,服务器添加和更新客户端的进度栏。但是,当我将选项卡控件更改为显示所有客户端进度条的选项卡时,服务器停止向客户端发送任何答复。即使我返回到服务器的“开始”选项卡,它也不再答复。

我对此非常努力。有人知道我的错误来自哪里吗?

我还对调试客户端和问题的一些照片进行了修饰:

客户端连接到服务器并获得答案:

Client connected to server and getting answers

更改GUI标签和服务器停止:

Changeing GUI tab and server stopping

1 个答案:

答案 0 :(得分:1)

问题解决了。感谢Aram Kocharyan和Mong Zhu。

错误是我试图从其他线程访问GUI元素。 要从另一个线程修改GUI元素,必须使用controll的Invoke方法(Invoke从拥有组件的线程中调用该方法)

示例:

Form1.GetInstance.flowLayoutPanel_progress.Invoke(
                new Action(() => Parse0(splited[1], out toReturn)));