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

标签 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 上,客户端可以连接到服务器要求他们回答,服务器回复一个 sleep 数据包,直到用户在 GUI 上输入有效路径并单击开始。

嗯,这也很好用。

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

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

我对这个很执着。有谁知道我的错误从何而来?

我还上传了一些调试客户端和我的问题的图片:

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

Client connected to server and getting answers

更改 GUI 选项卡和服务器停止:

Changeing GUI tab and server stopping

最佳答案

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

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

示例:

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

关于c# - Windows 窗体中的套接字问题。更改选项卡控件时套接字停止回复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55255978/

相关文章:

c# - 用于 C# 作业的弹性 apm

c - 如何使用 tcp/ip 将文件及其文件名从客户端发送到服务器?

php - 在 PHP 中使用 ssl 证书时出错

java - BufferReader 在连接关闭时仍在运行

c# - 从另一个 VR Oculus 应用启动 VR Oculus 应用

c# - BinaryReader 的 FillBuffer 方法有什么作用?

java - 通过socket从服务器到客户端的图像

c - 套接字编程(sys/socket.h)

c# - 我的 "if"语句没有按预期工作

c# - 适用于 .NET 的 ZXing 条码扫描器