简体   繁体   English

TCP客户端和套接字的问题

[英]problems with TCP clients and sockets

Background : my application has the option to check automatically whether a device on the network is connected to the network. 背景:我的应用程序可以选择自动检查网络上的设备是否已连接到网络。

If a device is not connected, it is represented as a blue square, and a green square for connected. 如果未连接设备,则将其表示为蓝色正方形,并以绿色正方形表示已连接。 I've tested the program rather thoroughly and have a few different sets of data to look at; 我已经对该程序进行了相当全面的测试,并查看了一些不同的数据集。

If a device starts off disconnected, i can connect, and then disconnect, which goes from blue, to green, to blue, but stops functioning after this. 如果设备从断开连接开始,我可以连接,然后断开连接,该连接从蓝色变为绿色,然后变为蓝色,但此后停止运行。

If a device starts of connected, i can disconnect, but then not connect again, so this goes from green, to blue, but then stops functioning. 如果设备开始连接,我可以断开连接,但不能再连接,因此从绿色变为蓝色,然后停止运行。

Here are some snippets of code to show how i'm connecting to these (this is not asynchronous as some devices to not have callbacks(?)); 这是一些代码片段,以显示我如何与它们连接(这不是异步的,因为某些设备没有回调(?));

TcpClient _tc = new TcpClient();
if (!_tc.Client.Connected)
            {
                try
                {
                    _tc.Client.Connect(device.deviceIPAddress, device.devicePort);
                    _tc.Client.ReceiveTimeout = 10;
                }
                catch
                {
                    if (deviceDownNotified == false)
                    {

                        //do stuff to notify me

                    }
                }

            }


            if (_tc.Client.Connected)
                {
                  tcpSet = true;
                    try
                    {
                        result = this.SendCommandResult(bData, 72);

                        if (result[0] == 0xF0 && result[1] == 0xF0 && result[2] == 0x00 && result[3] == 0x02 && result[68] == 0xF0 && result[69] == 0xF0)
                        {

                            CheckChanges(result, device, exit);
                            deviceDownNotified = false;

                        }
                    }
                    catch
                    {

                    }


                 }

edit: also i was just thinking, theres not really any need for me to disconnect manually sometimes. 编辑:也是我只是在想,有时候我真的不需要任何手动断开连接。 if a client isn't connected, then connect, if it is, then do stuff. 如果客户端未连接,则进行连接,如果已连接,则进行处理。 i'm a bit baffled as to which it switches between states once and stops working though. 我对它一次在状态之间切换并停止工作感到困惑。

I think my issue may be due to the way i am disconnecting the socket / client if the device is disconnected. 我认为我的问题可能是由于如果设备断开连接,我断开套接字/客户端的方式引起的。 Anyone have any ideas? 有人有想法么? if you need more information, just ask. 如果您需要更多信息,请询问。

edit 2: i wired up a reconnect button that i clicked manually when it should reconnect and i get the following error exception: 编辑2:我连接了一个重新连接按钮,该按钮在应重新连接时手动单击,并且出现以下错误异常:

"Once the socket has been disconnected, you can only reconnect again asynchronously, and only to a different EndPoint. BeginConnect must be called on a thread that won't exit until the operation has been completed." “一旦套接字断开连接,您只能再次异步重新连接,并且只能重新连接到另一个EndPoint。必须在不会完成操作的情况下退出的线程上调用BeginConnect。”

fixed my own problem. 解决了我自己的问题。 finally. 最后。 Here's some information for anyone out there reading this that may be looking for a solution. 这是供任何阅读此书的人的信息,他们可能正在寻找解决方案。

from what i gather, when a TCP connection is interrupted, you must create a new one as it's not possible to reconnect or reuse in this way. 从我收集到的信息来看,当TCP连接中断时,您必须创建一个新连接,因为无法以这种方式重新连接或重用。 what i did was this: 我所做的是:

(modified from code in my question) (根据我的问题的代码修改)

if (!_tc.Connected)
            {
                try
                {


                    connect2(deviceIPAddress, devicePort);

                }
                catch
                {
                    if (deviceDownNotified == false)
                    {

                        ((ListBox)mainUI.Controls["lbLog"]).InvokeEx(f => ((ListBox)mainUI.Controls["lbLog"]).Items.Add("device " + device.deviceDescription + " down at " + System.DateTime.Now));
                        (mainUI.Controls["btn" + device.deviceButtonNumber]).BackgroundImage = null;
                        (mainUI.Controls["btn" + device.deviceButtonNumber]).BackColor = Color.Blue;
                        deviceDownNotified = true;
                        try
                        {

                            _tc = new TcpClient();

                            initialPoll = false;


                            MessageBox.Show("here");
                        }
                        catch (Exception error)
                        {
                            MessageBox.Show(error.Message);
                        }
                    }
                }

            }

Here i simply created a new tcplistener with the same name (my original one was a variable of an instance of a class (what a tongue twister). creating this new TCP instance and then setting my inialPoll variable allowed everything to get back into track. 在这里,我只是简单地创建了一个同名的新tcplistener(我的原始tcplistener是一个类实例的变量(绕口令),创建这个新的TCP实例,然后设置我的inialPoll变量可使一切恢复正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM