【问题标题】:C# Unique ID for tcp/ip server client connection用于 tcp/ip 服务器客户端连接的 C# 唯一 ID
【发布时间】:2018-12-07 01:26:04
【问题描述】:

以下是我尝试在我的服务器上使用我的智能设备建立连接时的大致做法:

IPEndPoint localEP = new IPEndPoint(IPAddress.Any, nConst3rdPartyPort);
the3rdPartyListener = new Socket(localEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
the3rdPartyListener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
the3rdPartyListener.Bind(localEP);
the3rdPartyListener.Listen(100);
the3rdPartyListener.BeginAccept(new AsyncCallback(AcceptConnectBack), the3rdPartyListener);

只要知道服务器 ip 和端口号,任何客户端设备都可以连接到我的服务器。 现在,我想做客户端设备的过滤。只有特定的客户端设备可以连接到我的服务器。

我们是否可以向客户端插入一个唯一 ID,以便只有该唯一 ID 的客户端可以连接到我的服务器? 我们如何确保客户是我们想要的特定客户?

这是出于安全原因,以防止未经授权的客户端连接到服务器。

【问题讨论】:

    标签: c# .net sockets tcp-ip tcplistener


    【解决方案1】:

    您可以使用IPEndPointAddress 方法检索远程IPAddress 并制作白名单以检查谁可以连接您的服务器。

    string clientIP = ((IPEndPoint)(the3rdPartyListener.RemoteEndPoint)).Address.ToString();
    


    -------------------------------------------------- -------------------------------------------------- --------------------------------
    更新:
    我不确定 Csharp Socket 是否已验证设备方法。但我在下面有一个方法。
    使用NetworkStream 将数据从客户端传递到验证客户端的服务器。

    服务器端:

    TcpClient client;
    TcpListener server;
    Thread thread;
    Thread threadTwo;
    byte[] datalength = new byte[4];
    
    public Server()
    {
        InitializeComponent();
        server = new TcpListener(IPAddress.Any, 2018);
        server.Start();
        thread = new Thread(() =>
        {
            client = server.AcceptTcpClient();
            ServerReceive();
        }
        thread.Start();
    }
    
    void ServerReceive()
    {
        networkStream = client.GetStream();
        threadTwo = new Thread(() =>
        {
            while ((i = networkStream.Read(datalength, 0, 4)) != 0)
            {
                byte[] data = new byte[BitConverter.ToInt32(datalength, 0)];
                networkStream.Read(data, 0, data.Length);
                this.Invoke((MethodInvoker)delegate
                {
                    if(Encoding.Default.GetString(data) == "unique ID")
                    {
                        //Do your job
                    }
                }
            }
        }
        threadTwo.Start();
    }
    

    客户端:

    NetworkStream networkStream;
    TcpClient client;
    byte[] datalength = new byte[4];
    
    public Client()
    {
        InitializeComponent();
        try
        {
            client = new TcpClient("your server ip", 2018);
            ClientSend("unique ID");
        }
         catch (Exception ex)
        {
             MessageBox.Show(ex.Message);
        }
    }
    
    void ClientSend(string msg)
    {
        try
        {
            networkStream = client.GetStream();
            byte[] data;
            data = Encoding.Default.GetBytes(msg);
            int length = data.Length;
            byte[] datalength = new byte[4];
            datalength = BitConverter.GetBytes(length);
            networkStream.Write(datalength, 0, 4);
            networkStream.Write(data, 0, data.Length);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    

    【讨论】:

    • 奥利弗,感谢您的评论。但是有没有像蓝牙配对这样有唯一ID的方法?
    • Oliver,恐怕一旦未经授权的客户端知道了我们使用的白名单IP,它就已经可以连接了..有没有像客户端这样的方法必须给服务器一个唯一的ID,并且如果唯一ID与服务器匹配,则可以连接。
    • @Coolguy 在将数据发送到您的服务器之前,客户端必须连接到您的服务器,我也更新了我的答案。
    • 奥利弗,感谢您的回答。但是如果客户端不发送任何唯一的 id 怎么办?如果客户端只是连接并匿名收听服务器怎么办..我不希望这种情况发生。我们是否必须为客户端设置某种计时器来发送 ID。如果服务器在一段时间内没有收到 id,则断开客户端。
    猜你喜欢
    • 2012-08-31
    • 2018-12-30
    • 2017-08-04
    • 1970-01-01
    • 2019-06-18
    • 2021-08-12
    • 1970-01-01
    • 2012-07-04
    • 1970-01-01
    相关资源
    最近更新 更多