简体   繁体   中英

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:

"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."

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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