NetCoreServer icon indicating copy to clipboard operation
NetCoreServer copied to clipboard

How to obtain IP address or better MAC address of a client connected

Open SenerSoft opened this issue 4 years ago • 1 comments

Hi @chronoxor, this is a great library written in Core 3.0, thank you for sharing.

I turned TCP Chat server into a TCP server. I like to gather client information when they are sent a message each time (either their IP Addresses or MAC address).

For the time being, I figured that out how to get it by using Id;

If I use TcpSession by Id, I can find client IP Address which is very good.

        protected override void OnConnected()
        {
            Console.WriteLine($"TCP session with Id {Id} connected!");
            
            TcpSession session = Server.FindSession(Id);
            string IPAdderss = ((System.Net.IPEndPoint)session.Socket.RemoteEndPoint).Address.ToString();
            Console.WriteLine($"TCP session with IP {IPAdderss} connected!");

            // Send invite message
            string message = "Hello from TCP Server! Please send a message or '!' to disconnect the client!";
            SendAsync(message);
        }

Is this a reliable way to do it?

And secondly, For example, I can have 20 devices sending their messages sometimes almost at the same time. Is the way I use still reliable and client messages won't mix-up while I am fetching their client information in order to determine which client sent what message?

Best Regards, Sener

SenerSoft avatar Jul 01 '20 17:07 SenerSoft

Hello, you can add it in your session class.

protected override void OnReceived(byte[] buffer, long offset, long size)
{
            Debug.WriteLine($"Message from {this.Socket.RemoteEndPoint}");
}

Magniveo avatar Jul 07 '20 06:07 Magniveo