Socket Whitelist IP accepted connection C#

Asked

Viewed 64 times

0

I am trying to make a Socket system (Server -> <- Client), however I want to make an ip Whitelist that can be accepted in the client connection to the server, but I do not know how, short I want to make system where the Server checks if the ip that is in the "Whitelist" that will be an array, if the ip has la will the client connection will be accepted if there is the ip there won’t connect!.

Server:

        public Client(Socket ClientSocket)
    {
        //Client = this;
        this.ClientSocket = ClientSocket;
        ClientStream = new NetworkStream(ClientSocket);

        EndPoint = (IPEndPoint)ClientSocket.RemoteEndPoint;

        ClientThread = new Thread(ClientCallback);
        ClientThread.Start();

        LogFactory.GetLog(this).LogInfo("Client <{0}> connected to the server!", EndPoint);
    }

1 answer

0

Only create a List with the allowed ips, and when the connection comes check if the list contains the ip you connected:

    private void teste(Socket ClientSocket)
    {
        List<string> whiteList = new List<string>
        {
            "192.168.0.1",
            "192.168.0.2",
            "192.168.0.3",
            "192.168.0.4",
            "192.168.0.5",
            "192.168.0.6"                
        };

        string _ip = ((IPEndPoint)ClientSocket.RemoteEndPoint).Address.ToString();

        if (whiteList.Contains(_ip))
        {
            //permite
        }
        else
        {
            ClientSocket.Disconnect(false);
        }

    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.