C# Check if socket is disconnected?
Asked Answered
A

2

6

How can you check if a non-blocking socket is disconnect without using Poll?

Antineutron answered 18/4, 2011 at 4:9 Comment(0)
A
5

Create a cusomt socket class inheriting .net socket class :

public delegate void SocketEventHandler(Socket socket);
    public class CustomSocket : Socket
    {
        private readonly Timer timer;
        private const int INTERVAL = 1000;

        public CustomSocket(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
            : base(addressFamily, socketType, protocolType)
        {
            timer = new Timer { Interval = INTERVAL };
            timer.Tick += TimerTick;
        }

        public CustomSocket(SocketInformation socketInformation)
            : base(socketInformation)
        {
            timer = new Timer { Interval = INTERVAL };
            timer.Tick += TimerTick;
        }

        private readonly List<SocketEventHandler> onCloseHandlers = new List<SocketEventHandler>();
        public event SocketEventHandler SocketClosed
        {
            add { onCloseHandlers.Add(value); }
            remove { onCloseHandlers.Remove(value); }
        }

        public bool EventsEnabled
        {
            set
            {
                if(value)
                    timer.Start();
                else
                    timer.Stop();
            }
        }

        private void TimerTick(object sender, EventArgs e)
        {
            if (!Connected)
            {
                foreach (var socketEventHandler in onCloseHandlers)
                    socketEventHandler.Invoke(this);
                EventsEnabled = false;
            }
        }

        // Hiding base connected property
        public new bool Connected
        {
           get
           {
              bool part1 = Poll(1000, SelectMode.SelectRead);
              bool part2 = (Available == 0);
              if (part1 & part2)
                 return false;
              else
                 return true;
           }
        }
    }

Then use it like this :

        var socket = new CustomSocket(
                //parameters
                );

        socket.SocketClosed += socket_SocketClosed;
        socket.EventsEnabled = true;


        void socket_SocketClosed(Socket socket)
        {
            // do what you want
        }

I have just implemented a Socket close event in each socket. so your application should register event handlers for this event. then socket will inform your application if it was closed itself ;)

if there was any problem with code, inform me.

Aqueduct answered 18/4, 2011 at 4:42 Comment(7)
Socket.Connected is unrealiableAntineutron
new Connected property implemented in Custom Class. try it, it will work.Aqueduct
The Poll method does not work on linux. Even when the socket is closed it will continue to return false. That is why I am looking for a way without using Poll.Antineutron
you want to use this method in mono ? I have edited answer to stop propagating events after socket closed. Poll method is working for me in windows. I do not know where you want to use this.Aqueduct
It does work on windows with mono but on linux with mono it does not.Antineutron
mono in linux is not complete enough to do this. you are right.Aqueduct
Is there any other way or am I stuck implementing a ping/pong?Antineutron
H
0

The Socket class has a Connected property. According to MSDN the call to check is non-blocking. Is this not what you're looking for?

Hawken answered 18/4, 2011 at 4:22 Comment(1)
The Connected property always seems to be true unless I disconnect the Socket myself. I just checked and even though the socket was disconnected by the remote server it still returns true.Antineutron

© 2022 - 2024 — McMap. All rights reserved.