user3716786
user3716786

Reputation:

C# RemoteEndPoint doesnt exist

I am making a simple TCP Server in C#. It works, i can connect to it, except for one thing, it says RemoteEndpoint doesnt exist. Im using the remoteendpoint to get the client IP:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace tcpsockets
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener t = new TcpListener(IPAddress.Parse("0.0.0.0"),45123);
            t.Start();
            while (true)
            {
                Console.WriteLine("Waiting for a connection");
                TcpClient client = t.AcceptTcpClient();
                Console.WriteLine("Connection Recieved!");
                StreamReader sr = new StreamReader(client.GetStream());
                StreamWriter sw = new StreamWriter(client.GetStream());
                string text = sr.ReadLine();
                Console.WriteLine("text");
                IPEndPoint ipep = (IPEndPoint)t.RemoteEndpoint;
                IPAddress ipa = ipep.Address;
                Console.WriteLine(ipa.ToString());
                sw.WriteLine("Hello from the server");
                sw.Flush();
                client.Close();
            }
        }
    }
}

Here is my error:

Error   1   'System.Net.Sockets.TcpListener' does not contain a definition for 'RemoteEndpoint' and no extension method 'RemoteEndpoint' accepting a first argument of type 'System.Net.Sockets.TcpListener' could be found (are you missing a using directive or an assembly reference?)   c:\users\logan\documents\visual studio 2013\Projects\tcpsockets\tcpsockets\Program.cs   27  49  tcpsockets

Upvotes: 0

Views: 1113

Answers (1)

ie.
ie.

Reputation: 6101

TcpListener does not have RemoteEndPoint indeed. You might be want client.Client.RemoteEndPoint instead.

Actually TCP listener purpose is very simple and restricted to accepting of new client connections. As soon as the client is accepted (new connection established), the listener forgets about just arrived. All further communication goes through TcpClient instance (and underlying Socket - TcpClient.Client Property). Therefore, you should not expect any knowledge of remote endpoint from the listener.

Upvotes: 1

Related Questions