hi!

i use tcpclient to get connections from clients to my server. but i need the information of the ip address of that client. i know that i have to derive a mytcpclient from the tcpclient class, but i get some errors when extracting the socket out of it. the following code was done by me, but it won´t work!

class mytcpclient : tcpclient
        {
        public mytcpclient(TcpClient client)
             {}
        public new Socket Client
             {
              get {return base.Client;}
             }
         }

now the code in the main:

TcpClient client = new TcpClient;
mytcpclient mclient = new MyTcpClient(client);
Socket s = mclient.Client;               <--------- using a method of this

client comes up with an error, that there is no instance to that object or so.

can someone please help me? i really need to use tcpclient and to extract that socket to get the remote ip addy!

thany, gry

Dani AI

Generated

Short answer: you do not need to subclass TcpClient just to read the remote IP. The TcpClient you get from AcceptTcpClient (or from Connect on the client side) exposes the underlying Socket via the Client property; cast that socket’s RemoteEndPoint to IPEndPoint and read its Address. (learn.microsoft.com)

Example (server side — assume client is a connected TcpClient):

var remote = client.Client.RemoteEndPoint as IPEndPoint;
string clientIp = remote?.Address.ToString() ?? "<unknown>";

This pattern is the simplest and most reliable way to obtain the peer IP; RemoteEndPoint is the documented source for remote address/port and may throw if the socket is closed, so guard with null-checks / try/catch. (learn.microsoft.com)

Notes tied to the thread: — the crash you saw was likely caused by inspecting a TcpClient that wasn’t the actual connected instance (your mytcpclient constructor did not attach the underlying socket). — capitalization fixes were correct but not the root cause. — pointing at RemoteEndPoint was the right idea; remember to cast to IPEndPoint and handle null/exception cases. If you really must wrap a TcpClient, set the underlying socket in your constructor (for example, this.Client = other.Client) rather than leaving the derived object uninitialized. (learn.microsoft.com)

For production code also consider IPv6 vs IPv4, connection lifecycle (avoid inspecting sockets after dispose), and whether you should use raw Socket APIs for advanced control. (learn.microsoft.com)

Recommended Answers

All 4 Replies

try this:
class mytcpclient : TcpClient

instead of

class mytcpclient : tcpclient

(note the difference in capitalization)

thanx for answer, but of course i used TcpClient not tcpclient, the compiler instead would have told me ;)

can you post the exact error you are getting

can you post the exact error you are getting

Hi,

I dont think the problem is with the capitalization and you are using the correct format.

Here you code is a little bit confusing. I can't understand why you are extending the TcpClient class and adding socket object to it.

If your requirement is to get the Client's IP address, U can do in following way.

TcpClient client = new TcpClient(servername or ip , port);
IpEndPoint ipend = client.Client.RemoteEndPoint;
Console.WriteLine(IPAddress.Parse(ipend.Address.ToString());

Thanks,
Kedar

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.