Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#在循环中运行的异步客户端_C#_Multithreading_Sockets_Tcp - Fatal编程技术网

c#在循环中运行的异步客户端

c#在循环中运行的异步客户端,c#,multithreading,sockets,tcp,C#,Multithreading,Sockets,Tcp,我试图在这里编写MSDN示例的一个非常简单(对我来说似乎是这样)的扩展: 客户端将在循环中运行。在下面的例子中,我每次都重新初始化客户端 while (true) { Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); client.BeginConnect(r

我试图在这里编写MSDN示例的一个非常简单(对我来说似乎是这样)的扩展:

客户端将在循环中运行。在下面的例子中,我每次都重新初始化客户端

    while (true)
        {
            Socket client = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp);
            client.BeginConnect(remoteEP,
                new AsyncCallback(ConnectCallback), client);
            connectDone.WaitOne();

            string myCommand = "";

            Console.WriteLine("Enter command:");
            myCommand = Console.ReadLine();



            if (myCommand == "quit") break;
            // Send test data to the remote device.
            Send(client, myCommand + "<EOF>");
            sendDone.WaitOne();

            // Receive the response from the remote device.
            Receive(client);
            receiveDone.WaitOne();

            // Write the response to the console.
            Console.WriteLine("Response received : {0}", response);
            // Release the socket.
            client.Shutdown(SocketShutdown.Both);
            client.Close();

        }
while(true)
{
套接字客户端=新套接字(AddressFamily.InterNetwork,
流,ProtocolType.Tcp);
client.BeginConnect(remoteEP,
新的异步回调(ConnectCallback),客户端);
connectDone.WaitOne();
字符串myCommand=“”;
Console.WriteLine(“输入命令:”);
myCommand=Console.ReadLine();
如果(myCommand==“退出”)中断;
//将测试数据发送到远程设备。
发送(客户端,myCommand+“”);
sendDone.WaitOne();
//从远程设备接收响应。
接收(客户);
receiveDone.WaitOne();
//将响应写入控制台。
WriteLine(“收到的响应:{0}”,响应);
//松开插座。
client.Shutdown(SocketShutdown.Both);
client.Close();
}
它的工作方式不一致,经常会出现错误“无法访问已处理的对象”,但无法找出触发它的原因。
我是socket编程和多线程的初学者。

如果我重新初始化手动事件处理程序,似乎可以工作。不知道这东西是干什么的:)

while(true)
{
使用(套接字客户端=新套接字)(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp)
{
connectDone=新手动重置事件(错误);
sendDone=新手动重置事件(错误);
receiveDone=新手动重置事件(假);
client.BeginConnect(remoteEP,
新的异步回调(ConnectCallback),客户端);
connectDone.WaitOne();
字符串myCommand=“”;
//睡眠(100);
//创建TCP/IP套接字。
Console.WriteLine(“输入命令:”);
myCommand=Console.ReadLine();
如果(myCommand==“退出”)中断;
//将测试数据发送到远程设备。
发送(客户端,myCommand+“”);
sendDone.WaitOne();
//从远程设备接收响应。
接收(客户);
receiveDone.WaitOne();
//将响应写入控制台。
WriteLine(“收到的响应:{0}”,响应);
//松开插座。
client.Shutdown(SocketShutdown.Both);
client.Close();
}
}

尝试将代码包装到Using语句中,以确保所有内容都已正确处理。查看控制台,当我发现错误时,这些行已按顺序打印:套接字连接到192.168.56.1:11000 Enter命令:测试已向服务器发送9个字节。收到响应:无法识别的命令Enter command:Socket连接到192.168.56.1:11000我将所有内容都放在using语句中的while循环中,仍然会收到相同的错误。我知道这有点不对劲,但您可能还希望将代码放在单独的异步任务函数中,并在每次等待时调用它。我不能保证它会工作,但它可以防止任何资源冲突。
ResetEvent
可以描述为一扇门。当您尝试使用
WaitOne()
进入并且门关闭时(当未设置MRE时),该行的代码执行将被阻止,您必须等待门打开后才能继续执行。如果门已经开了,执行就继续。
        while (true)
        {
            using (Socket client = new Socket(AddressFamily.InterNetwork,
                SocketType.Stream, ProtocolType.Tcp))
            {
                connectDone = new ManualResetEvent(false);
                sendDone =  new ManualResetEvent(false);
                receiveDone =  new ManualResetEvent(false);

                client.BeginConnect(remoteEP,
                    new AsyncCallback(ConnectCallback), client);
                connectDone.WaitOne();



                string myCommand = "";

                //Thread.Sleep(100);  
                // Create a TCP/IP socket.

                Console.WriteLine("Enter command:");
                myCommand = Console.ReadLine();



                if (myCommand == "quit") break;
                // Send test data to the remote device.
                Send(client, myCommand + "<EOF>");
                sendDone.WaitOne();

                // Receive the response from the remote device.
                Receive(client);
                receiveDone.WaitOne();

                // Write the response to the console.
                Console.WriteLine("Response received : {0}", response);
                // Release the socket.
                client.Shutdown(SocketShutdown.Both);
                client.Close();
            }

        }