/// <summary>
        /// Starts listening for the incomming connections on specified port.
        /// </summary>
        private void Listening()
        {
            var hosts = new List <RemoteHost>();

            try
            {
                var endPoint = new IPEndPoint(IPAddress.Any, (int)port);

                mainSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

                mainSocket.Bind(endPoint);

                mainSocket.Listen((int)(SocketOptionName.MaxConnections));

                OnStateChanged(SocketServerState.Running);

                while (state == SocketServerState.Running)
                {
                    var host = new RemoteHost(mainSocket.Accept());

                    OnHostAcceptConnection(host);

                    hosts.Add(host);

                    var thread = new Thread(HostConnection);

                    thread.IsBackground = true;

                    thread.Start(host);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
            finally
            {
                OnStateChanged(SocketServerState.Stopped);

                for (int index = 0; index < hosts.Count; index++)
                {
                    var host = hosts[index];

                    if (host.Connection.Connected)
                    {
                        host.Connection.Close();
                    }
                }

                hosts.Clear();
            }
        }
        /// <summary>
        /// Start listening the server socket.
        /// </summary>
        private void Listening()
        {
            RemoteHost host = null;

            try
            {
                var address = new IPEndPoint(IPAddress.Any, port);

                mainSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

                mainSocket.Bind(address);

                var hostAddress = address as EndPoint;

                OnStateChanged(SocketServerState.Running);

                while(state == SocketServerState.Running)
                {
                    byte[] buffer = new byte[1024];

                    var count = mainSocket.ReceiveFrom(buffer, ref hostAddress);

                    if(count > 0)
                    {
                        host = new RemoteHost(mainSocket, ((IPEndPoint)hostAddress).Address.ToString());

                        OnReceivedMessage(host, buffer);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
            finally
            {
                OnStateChanged(SocketServerState.Stopped);

                if (host != null && host.Connection != null && host.Connection.Connected)
                {
                    host.Connection.Close();
                }
            }
        }
        /// <summary>
        /// Raises the server received message event.
        /// </summary>
        /// <param name="host">Information about host connection.</param>
        /// <param name="message">Message byte array.</param>
        protected void OnReceivedMessage(RemoteHost host, byte[] message)
        {
            var handler = ReceivedMessage;

            if (handler != null)
            {
                handler(this, new ReceiveMessageEventArgs(host, message));
            }
        }
        /// <summary>
        /// Initializes a new instance of the receive message event arguments/> class.
        /// </summary>
        /// <param name="host">The information about host connection.</param>
        /// <param name="message">The message byte array.</param>
        public ReceiveMessageEventArgs(RemoteHost host, byte[] message)
        {
            Host = host;

            Message = message;
        }
 protected void SocketServer_HostClosedConnection (object sender, RemoteHost host)
 {
     AddMessage(string.Format("Host : {0} - Status : Closed ", host.Address));
 }
        /// <summary>
        /// Raises a host closed connection event.
        /// </summary>
        /// <param name="host">Information about host connection.</param>
        protected void OnHostClosedConnection(RemoteHost host)
        {    
            var handler = HostClosedConnection;

            if (handler != null)
            {
                handler(this, host);
            }
        }
        /// <summary>
        /// Starts listening for the incomming connections on specified port.
        /// </summary>
        private void Listening()
        {
            var hosts = new List<RemoteHost>();
        
            try
            {  
                var endPoint = new IPEndPoint(IPAddress.Any, (int)port);
                        
                mainSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        
                mainSocket.Bind(endPoint);
        
                mainSocket.Listen((int)(SocketOptionName.MaxConnections));
        
                OnStateChanged(SocketServerState.Running);
        
                while (state == SocketServerState.Running)
                {
                    var host = new RemoteHost(mainSocket.Accept());

                    OnHostAcceptConnection(host);

                    hosts.Add(host);
        
                    var thread = new Thread(HostConnection);
        
                    thread.IsBackground = true;
        
                    thread.Start(host);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
            finally
            {
                OnStateChanged(SocketServerState.Stopped);
        
                for (int index = 0; index < hosts.Count; index++)
                {
                    var host = hosts[index];
        
                    if (host.Connection.Connected)
                    {
                        host.Connection.Close();
                    }
                }
        
                hosts.Clear();
            }
        }
Beispiel #8
0
        /// <summary>
        /// Initializes a new instance of the receive message event arguments/> class.
        /// </summary>
        /// <param name="host">The information about host connection.</param>
        /// <param name="message">The message byte array.</param>
        public MessageReceivedEventArgs(RemoteHost host, byte[] message)
        {
            Host = host;

            Message = message;
        }