By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,965 Members | 2,055 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,965 IT Pros & Developers. It's quick & easy.

getting client ip in sock-client on server

P: n/a
The code of classes given below is for server to which clients connect
i want to get ip address of client which has connected
pls help how can i get
//listen class
public class listen
{
TcpListener server=null;
Thread tcpthread=null;
client[] cl=new client[3];

public listen()
{
//
// TODO: Add constructor logic here
//
}
public void startlisten()
{
Int32 port = 3310;
IPAddress localAddr = IPAddress.Parse("192.168.0.6");

// TcpListener server = new TcpListener(port);
server = new TcpListener(localAddr, port);

// Start listening for client requests.
server.Start();
// Enter the listening loop.
for(int i=0;i<3;i++)
{
cl[i]=new client();
cl[i].status=true;
}

Boolean flag;
while(true)
{
try
{
flag=false;
// Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
for(int i=0;i<3;i++)
{
if(cl[i].status==true)
{
cl[i]= new client(server.AcceptTcpClient());
tcpthread=new Thread(new ThreadStart(cl[i].getClient));
tcpthread.Start();
flag=true;
break;
}
}
if(flag!=true)
{
//MessageBox.Show("All Clients Busy");

}


}
catch(Exception se)
{

}
}

}
public void stoplisten()
{
server.Stop();
}
}


//client class
public class client
{
TcpClient tcpClient;
public Boolean status;
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
IPAddress localAddr = IPAddress.Parse("192.168.0.6");

public client()
{ //
// TODO: Add constructor logic here
//
//status=true;
}
public client(TcpClient Client)
{
tcpClient =Client;
//
// TODO: Add constructor logic here
//
status=false;
}
public void getClient()
{
try
{
data = null;
// Get a stream object for reading and writing
NetworkStream stream = tcpClient.GetStream();
int i;
// Loop to receive all the data sent by the client.
while((i = stream.Read(bytes, 0, bytes.Length))!=0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
// Process the data sent by the client.
string replyMsg = data;
clamdCommand x=new clamdCommand();
replyMsg=x.Command(replyMsg);
int
lowerport=int.Parse(ConfigurationSettings.AppSetti ngs.Get("lowerport"));
int
upperport=int.Parse(ConfigurationSettings.AppSetti ngs.Get("upperport"));
for(int y=lowerport;y<upperport;y++)
{
if(replyMsg==y.ToString())
{
replyMsg=replyMsg+"\r\n\0";
byte[] msg = System.Text.Encoding.ASCII.GetBytes(replyMsg);
// Send back a response.
stream.Write(msg, 0, msg.Length);
TcpListener listener=new TcpListener(localAddr,y);
listener.Start();
// Buffer for reading data
Byte[] bs = new Byte[256];
String ds = null;
// Enter the listening loop.
while(true)
{ // Perform a blocking call to accept requests.
// You could also user server.AcceptSocket() here.
TcpClient bufferclient = listener.AcceptTcpClient();
ds = null;
// Get a stream object for reading and writing
NetworkStream strm = bufferclient.GetStream();
int l;
// Loop to receive all the data sent by the client.
while((l = strm.Read(bs, 0, bs.Length))!=0)
{ ds = System.Text.Encoding.ASCII.GetString(bs, 0, l);
// Process the data sent by the client.
clamresults obj=new clamresults();
ds=obj.loadDatabase("STREAM",ds);

replyMsg=ds;
bufferclient.Close();
break;
}
listener.Stop();
x.freeport(y-lowerport);
break;
} break;
}

}
replyMsg=replyMsg+"\r\n\0";
byte[] msgs = System.Text.Encoding.ASCII.GetBytes(replyMsg);
// Send back a response.
stream.Write(msgs, 0, msgs.Length);

}
}
catch(Exception se)
{
MessageBox.Show(se.ToString());

}
// Shutdown and end connection
finally
{
tcpClient.Close();
status=true;
}
}

}
Jan 19 '06 #1
Share this Question
Share on Google+
14 Replies


P: n/a
DNS.Resolve()
Jan 19 '06 #2

P: n/a
will it give ip address of client
i want to fetch ip address on server of client which is accepted or
connected
"Marko Becirevic" <ma*****************@inMail24.com> wrote in message
news:dq**********@sunce.iskon.hr...
DNS.Resolve()

Jan 19 '06 #3

P: n/a

"Ankit Aneja" <ef*****@newsgroups.nospam> wrote in message
news:Oz**************@TK2MSFTNGP15.phx.gbl...
will it give ip address of client
i want to fetch ip address on server of client which is accepted or
connected

....

No, it won't. I can't find anything in TcpClient documentation (fw 1.1)
class that would reveal client's IP address (but I may be wrong here) . If
you can use Socket class, then you can use RemoteEndPoint property.

Regards,
Goran
Jan 19 '06 #4

P: n/a

No, it won't. I can't find anything in TcpClient documentation (fw 1.1)
class that would reveal client's IP address (but I may be wrong here)

can i get it using framework 2.0
pls help


"Goran Sliskovic" <gs******@yahoo.com> wrote in message
news:e0**************@TK2MSFTNGP10.phx.gbl...

"Ankit Aneja" <ef*****@newsgroups.nospam> wrote in message
news:Oz**************@TK2MSFTNGP15.phx.gbl...
will it give ip address of client
i want to fetch ip address on server of client which is accepted or
connected

...

No, it won't. I can't find anything in TcpClient documentation (fw 1.1)
class that would reveal client's IP address (but I may be wrong here) . If
you can use Socket class, then you can use RemoteEndPoint property.

Regards,
Goran

Jan 19 '06 #5

P: n/a
can i get remote endpoint in TcpClient if i use framework2.0
"Goran Sliskovic" <gs******@yahoo.com> wrote in message
news:e0**************@TK2MSFTNGP10.phx.gbl...

"Ankit Aneja" <ef*****@newsgroups.nospam> wrote in message
news:Oz**************@TK2MSFTNGP15.phx.gbl...
will it give ip address of client
i want to fetch ip address on server of client which is accepted or
connected

...

No, it won't. I can't find anything in TcpClient documentation (fw 1.1)
class that would reveal client's IP address (but I may be wrong here) . If
you can use Socket class, then you can use RemoteEndPoint property.

Regards,
Goran

Jan 19 '06 #6

P: n/a
Hi Ankit,

In this current situation, I suggest you could try to invoke getpeername /
getsockname using PInvoke mechanism. On a connected socket, getpeername
will give you the remote address and getsockname will give you the local
address for the connection. We could use Socket.Handle Property to get the
socket handle and pass it into these API as parameters.

I hope the above inforamtion is helpful for you.If you have any questions
or concerns, please let me know. Thanks again and have a nice day!

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Best Regards,

--------------------
From: "Ankit Aneja" <ef*****@newsgroups.nospam>
References: <uq**************@TK2MSFTNGP14.phx.gbl> <dq**********@sunce.iskon.hr> <Oz**************@TK2MSFTNGP15.phx.gbl>
<e0**************@TK2MSFTNGP10.phx.gbl>Subject: Re: getting client ip in sock-client on server
Date: Thu, 19 Jan 2006 19:18:12 +0530
Lines: 30
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Original
Message-ID: <Ox**************@TK2MSFTNGP09.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: dsl-del-dynamic-067.85.246.61.touchtelindia.net 61.246.85.67Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.languages.csharp:379684
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
No, it won't. I can't find anything in TcpClient documentation (fw 1.1)
class that would reveal client's IP address (but I may be wrong here)

can i get it using framework 2.0
pls help


"Goran Sliskovic" <gs******@yahoo.com> wrote in message
news:e0**************@TK2MSFTNGP10.phx.gbl...

"Ankit Aneja" <ef*****@newsgroups.nospam> wrote in message
news:Oz**************@TK2MSFTNGP15.phx.gbl...
will it give ip address of client
i want to fetch ip address on server of client which is accepted or
connected

...

No, it won't. I can't find anything in TcpClient documentation (fw 1.1)
class that would reveal client's IP address (but I may be wrong here) . If you can use Socket class, then you can use RemoteEndPoint property.

Regards,
Goran



Jan 20 '06 #7

P: n/a
is there anything in framework 2.0
to get the remote address
""TerryFei"" <v-******@online.microsoft.com> wrote in message
news:9G**************@TK2MSFTNGXA02.phx.gbl...
Hi Ankit,

In this current situation, I suggest you could try to invoke getpeername /
getsockname using PInvoke mechanism. On a connected socket, getpeername
will give you the remote address and getsockname will give you the local
address for the connection. We could use Socket.Handle Property to get the
socket handle and pass it into these API as parameters.

I hope the above inforamtion is helpful for you.If you have any questions
or concerns, please let me know. Thanks again and have a nice day!

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Best Regards,

--------------------
From: "Ankit Aneja" <ef*****@newsgroups.nospam>
References: <uq**************@TK2MSFTNGP14.phx.gbl>

<dq**********@sunce.iskon.hr> <Oz**************@TK2MSFTNGP15.phx.gbl>
<e0**************@TK2MSFTNGP10.phx.gbl>
Subject: Re: getting client ip in sock-client on server
Date: Thu, 19 Jan 2006 19:18:12 +0530
Lines: 30
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Original
Message-ID: <Ox**************@TK2MSFTNGP09.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: dsl-del-dynamic-067.85.246.61.touchtelindia.net

61.246.85.67
Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.languages.csharp:37968 4
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
No, it won't. I can't find anything in TcpClient documentation (fw 1.1)
class that would reveal client's IP address (but I may be wrong here)

can i get it using framework 2.0
pls help


"Goran Sliskovic" <gs******@yahoo.com> wrote in message
news:e0**************@TK2MSFTNGP10.phx.gbl...

"Ankit Aneja" <ef*****@newsgroups.nospam> wrote in message
news:Oz**************@TK2MSFTNGP15.phx.gbl...
will it give ip address of client
i want to fetch ip address on server of client which is accepted or
connected
...

No, it won't. I can't find anything in TcpClient documentation (fw 1.1)
class that would reveal client's IP address (but I may be wrong here) . If you can use Socket class, then you can use RemoteEndPoint property.

Regards,
Goran


Jan 20 '06 #8

P: n/a
Hi Ankit,
Thanks for your quick response!
is there anything in framework 2.0 to get the remote address? Based on my knowledge, there is no this feature in framework 2.0. But I'll
consult this question for the relevant team. If I have an easier way to
achieve this goal later, I'll inform of you as soon as possible.
Thanks for your understanding!

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
From: "Ankit Aneja" <ef*****@newsgroups.nospam>
References: <uq**************@TK2MSFTNGP14.phx.gbl> <dq**********@sunce.iskon.hr> <Oz**************@TK2MSFTNGP15.phx.gbl>
<e0**************@TK2MSFTNGP10.phx.gbl>
<Ox**************@TK2MSFTNGP09.phx.gbl>
<9G**************@TK2MSFTNGXA02.phx.gbl>Subject: Re: getting client ip in sock-client on server
Date: Fri, 20 Jan 2006 10:47:03 +0530
Lines: 81
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Original
Message-ID: <uA*************@tk2msftngp13.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: dsl-del-dynamic-130.84.246.61.touchtelindia.net 61.246.84.130Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msft ngp13.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.languages.csharp:379924
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

is there anything in framework 2.0
to get the remote address
""TerryFei"" <v-******@online.microsoft.com> wrote in message
news:9G**************@TK2MSFTNGXA02.phx.gbl...
Hi Ankit,

In this current situation, I suggest you could try to invoke getpeername

/ getsockname using PInvoke mechanism. On a connected socket, getpeername
will give you the remote address and getsockname will give you the local
address for the connection. We could use Socket.Handle Property to get the socket handle and pass it into these API as parameters.

I hope the above inforamtion is helpful for you.If you have any questions
or concerns, please let me know. Thanks again and have a nice day!

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Best Regards,

--------------------
From: "Ankit Aneja" <ef*****@newsgroups.nospam>
References: <uq**************@TK2MSFTNGP14.phx.gbl>

<dq**********@sunce.iskon.hr> <Oz**************@TK2MSFTNGP15.phx.gbl>
<e0**************@TK2MSFTNGP10.phx.gbl>
Subject: Re: getting client ip in sock-client on server
Date: Thu, 19 Jan 2006 19:18:12 +0530
Lines: 30
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Original
Message-ID: <Ox**************@TK2MSFTNGP09.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: dsl-del-dynamic-067.85.246.61.touchtelindia.net

61.246.85.67
Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.languages.csharp:3796 84
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
No, it won't. I can't find anything in TcpClient documentation (fw 1.1)
class that would reveal client's IP address (but I may be wrong here)

can i get it using framework 2.0
pls help


"Goran Sliskovic" <gs******@yahoo.com> wrote in message
news:e0**************@TK2MSFTNGP10.phx.gbl...

"Ankit Aneja" <ef*****@newsgroups.nospam> wrote in message
news:Oz**************@TK2MSFTNGP15.phx.gbl...
> will it give ip address of client
> i want to fetch ip address on server of client which is accepted or
> connected
...

No, it won't. I can't find anything in TcpClient documentation (fw 1.1)
class that would reveal client's IP address (but I may be wrong here) .

If
you can use Socket class, then you can use RemoteEndPoint property.

Regards,
Goran




Jan 20 '06 #9

P: n/a
Thanks Sir
will wait for your reply
""TerryFei"" <v-******@online.microsoft.com> wrote in message
news:7Q**************@TK2MSFTNGXA02.phx.gbl...
Hi Ankit,
Thanks for your quick response!
is there anything in framework 2.0 to get the remote address? Based on my knowledge, there is no this feature in framework 2.0. But I'll
consult this question for the relevant team. If I have an easier way to
achieve this goal later, I'll inform of you as soon as possible.
Thanks for your understanding!

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
From: "Ankit Aneja" <ef*****@newsgroups.nospam>
References: <uq**************@TK2MSFTNGP14.phx.gbl>

<dq**********@sunce.iskon.hr> <Oz**************@TK2MSFTNGP15.phx.gbl>
<e0**************@TK2MSFTNGP10.phx.gbl>
<Ox**************@TK2MSFTNGP09.phx.gbl>
<9G**************@TK2MSFTNGXA02.phx.gbl>
Subject: Re: getting client ip in sock-client on server
Date: Fri, 20 Jan 2006 10:47:03 +0530
Lines: 81
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Original
Message-ID: <uA*************@tk2msftngp13.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: dsl-del-dynamic-130.84.246.61.touchtelindia.net

61.246.84.130
Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msft ngp13.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.languages.csharp:37992 4
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

is there anything in framework 2.0
to get the remote address
""TerryFei"" <v-******@online.microsoft.com> wrote in message
news:9G**************@TK2MSFTNGXA02.phx.gbl...
Hi Ankit,

In this current situation, I suggest you could try to invoke getpeername

/ getsockname using PInvoke mechanism. On a connected socket, getpeername
will give you the remote address and getsockname will give you the local
address for the connection. We could use Socket.Handle Property to get the socket handle and pass it into these API as parameters.

I hope the above inforamtion is helpful for you.If you have any
questions
or concerns, please let me know. Thanks again and have a nice day!

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Best Regards,

--------------------
From: "Ankit Aneja" <ef*****@newsgroups.nospam>
References: <uq**************@TK2MSFTNGP14.phx.gbl>
<dq**********@sunce.iskon.hr> <Oz**************@TK2MSFTNGP15.phx.gbl>
<e0**************@TK2MSFTNGP10.phx.gbl>
Subject: Re: getting client ip in sock-client on server
Date: Thu, 19 Jan 2006 19:18:12 +0530
Lines: 30
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
X-RFC2646: Format=Flowed; Original
Message-ID: <Ox**************@TK2MSFTNGP09.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: dsl-del-dynamic-067.85.246.61.touchtelindia.net
61.246.85.67
Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP09.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.languages.csharp:37968 4
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
No, it won't. I can't find anything in TcpClient documentation (fw 1.1)
class that would reveal client's IP address (but I may be wrong here)

can i get it using framework 2.0
pls help


"Goran Sliskovic" <gs******@yahoo.com> wrote in message
news:e0**************@TK2MSFTNGP10.phx.gbl.. .
>
> "Ankit Aneja" <ef*****@newsgroups.nospam> wrote in message
> news:Oz**************@TK2MSFTNGP15.phx.gbl...
>> will it give ip address of client
>> i want to fetch ip address on server of client which is accepted or
>> connected
> ...
>
> No, it won't. I can't find anything in TcpClient documentation (fw
> 1.1)
> class that would reveal client's IP address (but I may be wrong here)
> .
If
> you can use Socket class, then you can use RemoteEndPoint property.
>
> Regards,
> Goran
>
>


Jan 20 '06 #10

P: n/a

""TerryFei"" <v-******@online.microsoft.com> wrote in message
news:9G**************@TK2MSFTNGXA02.phx.gbl...
Hi Ankit,

In this current situation, I suggest you could try to invoke getpeername /
getsockname using PInvoke mechanism. On a connected socket, getpeername
will give you the remote address and getsockname will give you the local
address for the connection. We could use Socket.Handle Property to get the
socket handle and pass it into these API as parameters.

I hope the above inforamtion is helpful for you.If you have any questions
or concerns, please let me know. Thanks again and have a nice day!


....

The problem is that TcpClient does not expose underlying socket. Socket
TcpClient.Client is protected property (holds underlying socket). I suspect
OP could get this using reflection (I heard this is possible using
reflection, but haven't tried it myself and it is bad practice to do this of
course). Socket has RemoteEndPoint property, so it should not be needed to
PInvoke. Haven't tested it, though. Or maybe I missed the original problem?

Regards,
Goran
Jan 20 '06 #11

P: n/a
can u give me some sample code
how i can get remote ip without changing much of my present code
"Goran Sliskovic" <gs******@yahoo.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...

""TerryFei"" <v-******@online.microsoft.com> wrote in message
news:9G**************@TK2MSFTNGXA02.phx.gbl...
Hi Ankit,

In this current situation, I suggest you could try to invoke getpeername
/
getsockname using PInvoke mechanism. On a connected socket, getpeername
will give you the remote address and getsockname will give you the local
address for the connection. We could use Socket.Handle Property to get
the
socket handle and pass it into these API as parameters.

I hope the above inforamtion is helpful for you.If you have any questions
or concerns, please let me know. Thanks again and have a nice day!


...

The problem is that TcpClient does not expose underlying socket. Socket
TcpClient.Client is protected property (holds underlying socket). I
suspect
OP could get this using reflection (I heard this is possible using
reflection, but haven't tried it myself and it is bad practice to do this
of
course). Socket has RemoteEndPoint property, so it should not be needed to
PInvoke. Haven't tested it, though. Or maybe I missed the original
problem?

Regards,
Goran

Jan 21 '06 #12

P: n/a

"Ankit Aneja" <ef*****@newsgroups.nospam> wrote in message
news:eb**************@TK2MSFTNGP14.phx.gbl...
can u give me some sample code
how i can get remote ip without changing much of my present code

....

Hi,

Here it is:
<CUT>
using System;
using System.Reflection;
using System.Reflection.Emit;

using System.Net;
using System.Net.Sockets;

namespace ConsoleApplication1 {
public class Test {
public static void Main() {
TcpListener tcpListener = new TcpListener(IPAddress.Any, 10000);
tcpListener.Start();
TcpClient client = tcpListener.AcceptTcpClient();
Type tcpClientType = typeof(TcpClient);
//get value of protected TCPClient property Client that holds
socket...
Socket clientSocket =
(Socket)tcpClientType.InvokeMember("Client", BindingFlags.GetProperty |
BindingFlags.NonPublic | BindingFlags.Instance , null, client, null);
//socket RemoteEndpoint is address of connected tcpClinet...
IPEndPoint endPoint = (IPEndPoint) clientSocket.RemoteEndPoint;
string remotAddr = endPoint.Address.ToString();
}
}
}
</CUT>

Please note that such thing is considered bad practice and potentialy
dangerous. No guarantees.

Regards,
Goran

Jan 21 '06 #13

P: n/a
Hi Goran,
Socket has RemoteEndPoint property, so it should not be needed to
PInvoke. Haven't tested it Thanks for your reply! Yes, Socket has RemoteEndPoint property and we could
access relevant address with it in theory. However customer has tried it
and failed. So I suggest use PInvoke to achieve this goal.

Best Regards,

Terry Fei[MSFT]
Microsoft Community Support
Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

--------------------
From: "Goran Sliskovic" <gs******@yahoo.com>
References: <uq**************@TK2MSFTNGP14.phx.gbl> <dq**********@sunce.iskon.hr> <Oz**************@TK2MSFTNGP15.phx.gbl>
<e0**************@TK2MSFTNGP10.phx.gbl>
<Ox**************@TK2MSFTNGP09.phx.gbl>
<9G**************@TK2MSFTNGXA02.phx.gbl>Subject: Re: getting client ip in sock-client on server
Date: Fri, 20 Jan 2006 14:03:53 +0100
Lines: 27
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1506
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1506
Message-ID: <#q*************@tk2msftngp13.phx.gbl>
Newsgroups: microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: 194-152-209-27.adsl.net.t-com.hr 194.152.209.27
Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msft ngp13.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.languages.csharp:379994
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
""TerryFei"" <v-******@online.microsoft.com> wrote in message
news:9G**************@TK2MSFTNGXA02.phx.gbl...
Hi Ankit,

In this current situation, I suggest you could try to invoke getpeername
/ getsockname using PInvoke mechanism. On a connected socket, getpeername
will give you the remote address and getsockname will give you the local
address for the connection. We could use Socket.Handle Property to get the socket handle and pass it into these API as parameters.

I hope the above inforamtion is helpful for you.If you have any questions
or concerns, please let me know. Thanks again and have a nice day!


...

The problem is that TcpClient does not expose underlying socket. Socket
TcpClient.Client is protected property (holds underlying socket). I suspect
OP could get this using reflection (I heard this is possible using
reflection, but haven't tried it myself and it is bad practice to do this

ofcourse). Socket has RemoteEndPoint property, so it should not be needed to
PInvoke. Haven't tested it, though. Or maybe I missed the original problem?

Regards,
Goran


Jan 23 '06 #14

P: n/a

""TerryFei"" <v-******@online.microsoft.com> wrote in message
news:3n****************@TK2MSFTNGXA02.phx.gbl...
Hi Goran,
Socket has RemoteEndPoint property, so it should not be needed to
PInvoke. Haven't tested it
Thanks for your reply! Yes, Socket has RemoteEndPoint property and we

could access relevant address with it in theory. However customer has tried it
and failed. So I suggest use PInvoke to achieve this goal.

....

Hi,

I posted example with tcpclient/remoteendpoint that works (no my lan),
framework 1.1. Though I can guess there may be problems in non-trivial
network setups (NAT for example).

Regards,
Goran
Jan 23 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.