Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RemoteEndPoint vs. LocalEndPoint

As a beginner in C# networking, I wrote a simple Client-Server application. I'm connecting to my local IPAddress and port 8080 where the server is listening.

On Client Side:

        IPAddress remoteaddr = IPAddress.Parse("127.0.0.1");
        int port = 8880;
        TcpClient tcpclient = new TcpClient();
        tcpclient.Connect(remoteaddr, port);
        NetworkStream networkstream = tcpclient.GetStream();

        IPEndPoint RemAddr = (IPEndPoint)tcpclient.Client.RemoteEndPoint;
        IPEndPoint LocAddr = (IPEndPoint)tcpclient.Client.LocalEndPoint;

        if (RemAddr != null)
        {
            // Using the RemoteEndPoint property.
            Console.WriteLine("I am connected to " + RemAddr.Address + "on port number " + RemAddr.Port);
        }

        if (LocAddr != null)
        {
            // Using the LocalEndPoint property.
            Console.WriteLine("My local IpAddress is :" + LocAddr.Address + "I am connected on port number " + LocAddr.Port);
        }

the output is:

I am connected to 127.0.0.1 on port number 8880
My local IpAddress is :127.0.0.1 I am connected on port number 46715

So What's the difference between RemoteEndPoint and LocalEndPoint? What is the use of LocalEndPoint Port (46715 in my example) and where does it came from? Thanks.

like image 306
4 revs, 2 users 67% Avatar asked Jan 01 '16 17:01

4 revs, 2 users 67%


Video Answer


1 Answers

Remote End point will show you which client(ip) is connected to you're local end point (that is more likely to be the server ip 127.0.0.1)

like image 119
Be_careable Avatar answered Oct 10 '22 12:10

Be_careable