getting client ip in sock-client on server

A

Ankit Aneja

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=new client();
cl.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.status==true)
{
cl= new client(server.AcceptTcpClient());
tcpthread=new Thread(new ThreadStart(cl.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.AppSettings.Get("lowerport"));
int
upperport=int.Parse(ConfigurationSettings.AppSettings.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;
}
}

}
 
A

Ankit Aneja

will it give ip address of client
i want to fetch ip address on server of client which is accepted or
connected
 
G

Goran Sliskovic

Ankit Aneja said:
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
 
A

Ankit Aneja

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
 
T

TerryFei

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,

--------------------
 
A

Ankit Aneja

is there anything in framework 2.0
to get the remote address
"TerryFei" said:
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" <[email protected]>
References: <[email protected]>
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: <[email protected]>
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!TK2MSFTNGP09.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
 
T

TerryFei

Hi Ankit,
Thanks for your quick response!
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" <[email protected]>
References: <[email protected]>
<[email protected]> <[email protected]>
<[email protected]>
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: <[email protected]>
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!tk2msftngp13.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" said:
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" <[email protected]>
References: <[email protected]>
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: <[email protected]>
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!TK2MSFTNGP09.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





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
 
A

Ankit Aneja

Thanks Sir
will wait for your reply
"TerryFei" said:
Hi Ankit,
Thanks for your quick response!
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" <[email protected]>
References: <[email protected]>
<[email protected]> <[email protected]>
<[email protected]>
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: <[email protected]>
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!tk2msftngp13.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" said:
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" <[email protected]>
References: <[email protected]>
<[email protected]> <[email protected]>
<[email protected]>
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: <[email protected]>
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!TK2MSFTNGP09.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





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
 
G

Goran Sliskovic

"TerryFei" said:
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
 
A

Ankit Aneja

can u give me some sample code
how i can get remote ip without changing much of my present code
 
G

Goran Sliskovic

Ankit Aneja said:
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
 
T

TerryFei

Hi Goran,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" <[email protected]>
References: <[email protected]>
<[email protected]> <[email protected]>
<[email protected]>
 
G

Goran Sliskovic

"TerryFei" said:
Hi Goran,
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top