get client ip address in C# socket client application

S

Sandeep Singh

I am making socket client application in C#
how can i get ip address of client who has connected to server
 
V

Vitaly Zayko

From which side? If you are talking about server then Socket.Accept
returns a socket and you can get remote IP from property
Socket.RemoteEndPoint. If you want to get client IP on client side you
should call Dns.Resolve() in .NET 1.x or Dns.GetHostAddresses() in .NET 2.
 
S

Sandeep Singh

i want to get ip address on server of client which is connected
i am getting socket.LocalEndpoint but not able to get
socket.RemoteEndpoint
 
S

Sandeep Singh

i want to get ip address on server of client which is connected
i am getting socket.LocalEndpoint but not able to get
socket.RemoteEndpoint
 
S

Sandeep Singh

i want to get ip address on server of client which is connected
i am getting socket.LocalEndpoint but not able to get
socket.RemoteEndpoint
 
S

Sandeep Singh

i want to get ip address on server of client which is connected
i am getting socket.LocalEndpoint but not able to get
socket.RemoteEndpoint
 
S

Sandeep Singh

i want to get ip address on server of client which is connected
i am getting socket.LocalEndpoint but not able to get
socket.RemoteEndpoint
 
V

Vitaly Zayko

Sandeep said:
i want to get ip address on server of client which is connected
i am getting socket.LocalEndpoint but not able to get
socket.RemoteEndpoint

Try this code in a console App:
//===========
Socket lsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
// change second param to port number you are going to use:
IPEndPoint lep = new IPEndPoint(IPAddress.Any, 21);
lsock.Bind(lep);
lsock.Listen(10);
Socket rsock = lsock.Accept();
Console.WriteLine("Remote IP: {0}",
((IPEndPoint)rsock.RemoteEndPoint).Address.ToString());
//==========
 
S

Sandeep Singh

i am using this code
first 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();
}
}




second 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;
}
}

}
 
V

Vitaly Zayko

AcceptTcpClient() return a TcpClient class. It has Client property which
is a Socket. Thus you can get IPEndPoint and its IP as I mentioned in my
last message.
cl= new client(server.AcceptTcpClient());
 
A

Ankit Aneja

i am not able to get .RemoteEndpoint after cl.
in next line to accept client
cl= new client(server.AcceptTcpClient());
cl.?RemoteEndpoint is not displayed
Vitaly Zayko said:
AcceptTcpClient() return a TcpClient class. It has Client property which
is a Socket. Thus you can get IPEndPoint and its IP as I mentioned in my
last message.
cl= new client(server.AcceptTcpClient());

 
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,

--------------------
From: "Ankit Aneja" <[email protected]>
References: <[email protected]>
<[email protected]> <[email protected]>
<#[email protected]>
Subject: Re: get client ip address in C# socket client application
Date: Wed, 18 Jan 2006 19:09:11 +0530
Lines: 16
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; Response
Message-ID: <#[email protected]>
Newsgroups: vitaly_at_zayko_dot_net,microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: dsl-del-dynamic-145.85.246.61.touchtelindia.net 61.246.85.145
Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.languages.csharp:379374
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

i am not able to get .RemoteEndpoint after cl.
in next line to accept client
cl= new client(server.AcceptTcpClient());
cl.?RemoteEndpoint is not displayed
Vitaly Zayko said:
AcceptTcpClient() return a TcpClient class. It has Client property which
is a Socket. Thus you can get IPEndPoint and its IP as I mentioned in my
last message.
cl= new client(server.AcceptTcpClient());


 
A

Ankit Aneja

can we get remote ip address in a simple way
using framework 2.0
"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: get client ip address in C# socket client application
Date: Wed, 18 Jan 2006 19:09:11 +0530
Lines: 16
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; Response
Message-ID: <#[email protected]>
Newsgroups: vitaly_at_zayko_dot_net,microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: dsl-del-dynamic-145.85.246.61.touchtelindia.net 61.246.85.145
Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.languages.csharp:379374
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

i am not able to get .RemoteEndpoint after cl.
in next line to accept client
cl= new client(server.AcceptTcpClient());
cl.?RemoteEndpoint is not displayed
Vitaly Zayko said:
AcceptTcpClient() return a TcpClient class. It has Client property which
is a Socket. Thus you can get IPEndPoint and its IP as I mentioned in my
last message.

cl= new
client(server.AcceptTcpClient());


 
T

TerryFei

Hi Ankit,
Nice to hear from you again! :)
Based on my knowledge, there is no this feature in framework 2.0. So I
suggest you could use PInove(getpeername / getsockname) to achieve this
goal.
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]>
<[email protected]>
<[email protected]>
Subject: Re: get client ip address in C# socket client application
Date: Fri, 20 Jan 2006 11:08:45 +0530
Lines: 70
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!TK2MSFTNGP09.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.languages.csharp:379927
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

can we get remote ip address in a simple way
using framework 2.0
"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,

--------------------
<#[email protected]>
Subject: Re: get client ip address in C# socket client application
Date: Wed, 18 Jan 2006 19:09:11 +0530
Lines: 16
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; Response
Message-ID: <#[email protected]>
Newsgroups: vitaly_at_zayko_dot_net,microsoft.public.dotnet.languages.csharp
NNTP-Posting-Host: dsl-del-dynamic-145.85.246.61.touchtelindia.net 61.246.85.145
Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.languages.csharp:379374
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

i am not able to get .RemoteEndpoint after cl.
in next line to accept client
cl= new client(server.AcceptTcpClient());
cl.?RemoteEndpoint is not displayed
"Vitaly Zayko" <vitaly_at_zayko_dot_net> wrote in message
AcceptTcpClient() return a TcpClient class. It has Client property which
is a Socket. Thus you can get IPEndPoint and its IP as I mentioned in my
last message.

cl= new
client(server.AcceptTcpClient());


 

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