Esempio n. 1
0
        /// <summary>
        /// Callback when one client successfully connected to the server.
        /// </summary>
        private void AcceptCallback(IAsyncResult ar)
        {
            try
            {
                // Signal the connect thread to continue.
                connectDone.Set();

                // Dont accept connection if the server is not running and the listener is null
                if (!IsServerRunning || _listener == null)
                {
                    log.Error("Server is not running");
                    return;
                }

                // Get the socket that handles the client request.
                Socket listener = (Socket)ar.AsyncState;
                Socket handler  = listener.EndAccept(ar);

                IPEndPoint ipEndPoint = handler.RemoteEndPoint as IPEndPoint;

                // Try to get the Hostname of the client which will connect to the server
                IPHostEntry entry = ServerFunctions.GetIPHostEntryOfClient(ipEndPoint);

                // Check if the client which wants to connect is already connected.
                // If Client is not connected then try to connect to the server
                if (!ServerFunctions.CheckClientStatus(this.ConnectedClients, handler, false) && ipEndPoint != null)
                {
                    // Add connected Client to the listbox
                    ServerWindow.Dispatcher.Invoke(new AddClientHandler(ServerWindow.AddClient), ipEndPoint, entry);

                    // Add connected client to the connected client list
                    this.ConnectedClients.Add(handler);

                    log.Info("Client " + entry.HostName + " successfully connected to the server.");
                    log.Info("");
                }
                else
                {
                    // Check if the already connected client is still connected.
                    // If the already connected client is connected then close the same client which tries to connect again
                    if (ServerFunctions.CheckClientStatus(this.ConnectedClients, handler, true))
                    {
                        log.Info("Client " + entry.HostName + " already connected. Dont connect to server.");

                        // Close socket because this socket is already connected
                        handler.Shutdown(SocketShutdown.Both);
                        handler.Close();
                    }
                    else
                    {
                        // Delete the already connected client and connect the new client which tries to connect
                        this.DeleteAndAddConnectedClient(this.ConnectedClients, handler);
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message, ex);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Method which which deletes the disconnected client and connect the new client which tries to connect
        /// </summary>
        private void DeleteAndAddConnectedClient(IList <Socket> socketList, Socket socket)
        {
            IPEndPoint s1 = socket.RemoteEndPoint as IPEndPoint;

            for (int i = 0; i < socketList.Count; i++)
            {
                try
                {
                    IPEndPoint s2 = socketList[i].RemoteEndPoint as IPEndPoint;

                    if (s1.Address.Equals(s2.Address))
                    {
                        ServerWindow.Dispatcher.Invoke(new RemoveClientSocketHandler(ServerWindow.RemoveClient), s1.Address.ToString(), false);

                        ServerWindow.Dispatcher.Invoke(new RemoveClientHandler(ServerWindow.RemoveClientSocket), s1.Address.ToString(), false);

                        // Add connected Client to the listbox
                        // Try to get the Hostname of the client which will connect to the server
                        IPEndPoint  ipEndPoint = socket.RemoteEndPoint as IPEndPoint;
                        IPHostEntry entry      = ServerFunctions.GetIPHostEntryOfClient(ipEndPoint);

                        // Add connected Client to the listbox
                        ServerWindow.Dispatcher.Invoke(new AddClientHandler(ServerWindow.AddClient), ipEndPoint, entry);

                        // Add connected client to the connected client list
                        this.ConnectedClients.Add(socket);

                        log.Info("Client " + entry.HostName + " successfully reconnected to the server.");
                        log.Info("");

                        return;
                    }
                }
                catch (Exception ex)
                {
                    log.Error(ex.Message, ex);
                }
            }
        }