简体   繁体   中英

C# Unique ID for tcp/ip server client connection

below is roughly how I normally do when trying to setup a connection on my server with my slient device:

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);

Any client device can connect to my server if they know the server ip and port number. Now, I want to do filtering of client device. Only specific client device can connect to my server.

Is it that we can insert a unique ID to client so that the client with only that unique ID can connect to my server? How do we ensure that the client is the specific client that we want?

This is for security reason to prevent unauthorized client from connecting to the server.

You can use IPEndPoint Address method to retrieve the remote IPAddress and make a whitelist to check who can connect your server.

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


------------------------------------------------------------------------------------------------------------------------------------
Updated:
I'm not sure Csharp Socket has verified device method or not. But I have a method on below.
Use NetworkStream to pass data from Client to Server that verified the client.

Server Side:

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();
}

Client Side:

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM