TcpClient and IP

  • Thread starter Thread starter user
  • Start date Start date
U

user

Hello
How can i read IP of incoming connection when i have TcpClient for that
connection ?

Thanx
 
Hello
How can i read IP of incoming connection when i have TcpClient for
that connection ?

Thanx

you can get that from a socket object the RemoteEndPoint propery
returns a IPEndPoint structure you can use.

The slightly trick bit is getting the socket object, there are 2
options..

1./ The TcpClient class has a Socket property, but it is protected, so
you could create a custom class that derives from it.

2./ You can use the TcpListener.AcceptSocket method which returns a
socket. (you can create a network stream from the socket that you can
use to create BinaryReader and binaryWriter objects)

then you simply use the socket i.e.

(IpEndPoint)socket.RemoteEndPoint.Address.ToString();

Hope that Helps.

Regards Tim.
 
Tim said:
(IpEndPoint)socket.RemoteEndPoint.Address.ToString();

oops, missed a pair of brackets..

((IpEndPoint)socket.RemoteEndpoint).address.TString();
 
Yes and introducing a couple of new errors too :P

((IpEndPoint)socket.RemoteEndPoint).Address.ToString();
 
Morten said:
Yes and introducing a couple of new errors too :P

((IpEndPoint)socket.RemoteEndPoint).Address.ToString();

lol, that will teach me to type quick and just hit send...

I hope though that my typo's aside the point was made :-)


Cheers Tim.
 
Ok, i tried to create my own TcpClient class:
public class MyTcpClient: System.Net.Sockets.TcpClient
{
public MyTcpClient()
{
}

public Socket ReturnSocket()
{
return Client;
}
}

and in server.cs code i have:
MyTcpClient handler = (MyTcpClient)listener.AcceptTcpClient();
(listener is TcpListener).

It compiles, but when i start program it hangs on that line.
Why ? How can i change TcpClient to MyTcpClient so i could use
ReturnSocket() ?

Thanx again :)
 
Ok, i tried to create my own TcpClient class:
public class MyTcpClient: System.Net.Sockets.TcpClient
{
public MyTcpClient()
{
}

public Socket ReturnSocket()
{
return Client;
}
}

and in server.cs code i have:
MyTcpClient handler = (MyTcpClient)listener.AcceptTcpClient();
(listener is TcpListener).

It compiles, but when i start program it hangs on that line.
Why ? How can i change TcpClient to MyTcpClient so i could use
ReturnSocket() ?

Thanx again :)

To be honest I wouldn't be inclined to use this method, I would be more
inclined to use the 2nd method I suggested, as the first is a bit of a
hack.

The reason your code is not working is because you have introduced a
new method of the class, what you need to do is simply change the
visibility of the Socket property from protected to public in the
tcpclient class, it's a hack because although the typecast will work,
the accepttcpclient method does not return your type, it returns the
tcpclient.

Regards Tim.
 
Is there a way to get the incoming IP using a TcpClient? or is this only available using a Socket

Stephen Richardso
(e-mail address removed)

----- Tim Jarvis wrote: ----

(e-mail address removed) wrote
Hell
How can i read IP of incoming connection when i have TcpClient fo
that connection

you can get that from a socket object the RemoteEndPoint proper
returns a IPEndPoint structure you can use

The slightly trick bit is getting the socket object, there are
options.

1./ The TcpClient class has a Socket property, but it is protected, s
you could create a custom class that derives from it

2./ You can use the TcpListener.AcceptSocket method which returns
socket. (you can create a network stream from the socket that you ca
use to create BinaryReader and binaryWriter objects

then you simply use the socket i.e

(IpEndPoint)socket.RemoteEndPoint.Address.ToString()

Hope that Helps

Regards Tim
 
Stephen said:
Is there a way to get the incoming IP using a TcpClient? or is
this only available using a Socket?

AFAIK you can only get the ipaddress from a socket (but just because I
don't know doesn't mean that there isn't some other way.) . However a
tcpClient does have a socket property, it is protected though and only
available to decendant classes.

Cheers Tim.
 
Back
Top