CSharp/C# >> get client ip address in C# socket client application

by Sandeep Singh » Thu, 12 Jan 2006 14:19:30 GMT

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

CSharp/C# >> get client ip address in C# socket client application

by Vitaly Zayko » Thu, 12 Jan 2006 15:17:51 GMT



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.

--
Vit Zayko

CSharp/C# >> get client ip address in C# socket client application

by Sandeep Singh » Thu, 12 Jan 2006 18:36:42 GMT

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

CSharp/C# >> get client ip address in C# socket client application

by Sandeep Singh » Thu, 12 Jan 2006 18:37:13 GMT

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

CSharp/C# >> get client ip address in C# socket client application

by Sandeep Singh » Thu, 12 Jan 2006 18:37:47 GMT

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

CSharp/C# >> get client ip address in C# socket client application

by Sandeep Singh » Thu, 12 Jan 2006 18:38:15 GMT

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

CSharp/C# >> get client ip address in C# socket client application

by Sandeep Singh » Thu, 12 Jan 2006 18:38:38 GMT

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

CSharp/C# >> get client ip address in C# socket client application

by Vitaly Zayko » Thu, 12 Jan 2006 19:35:57 GMT


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());
//==========

--
Vit Zayko

CSharp/C# >> get client ip address in C# socket client application

by Sandeep Singh » Thu, 12 Jan 2006 21:04:37 GMT

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[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();
}
}




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

CSharp/C# >> get client ip address in C# socket client application

by Vitaly Zayko » Thu, 12 Jan 2006 21:27:24 GMT

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.


--
Vit Zayko

CSharp/C# >> get client ip address in C# socket client application

by Ankit Aneja » Wed, 18 Jan 2006 21:39:11 GMT

i am not able to get .RemoteEndpoint after cl[i].
in next line to accept client
cl[i]= new client(server.AcceptTcpClient());
cl[i].?RemoteEndpoint is not displayed

CSharp/C# >> get client ip address in C# socket client application

by v-terryf » Fri, 20 Jan 2006 11:21:29 GMT

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,

--------------------
< XXXX@XXXXX.COM > < XXXX@XXXXX.COM >
<# XXXX@XXXXX.COM >
< XXXX@XXXXX.COM >
< XXXX@XXXXX.COM >
vitaly_at_zayko_dot_net,microsoft.public.dotnet.languages.csharp
61.246.85.145

CSharp/C# >> get client ip address in C# socket client application

by Ankit Aneja » Fri, 20 Jan 2006 13:38:45 GMT

can we get remote ip address in a simple way
using framework 2.0

CSharp/C# >> get client ip address in C# socket client application

by v-terryf » Mon, 23 Jan 2006 14:23:29 GMT

i 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.)
--------------------
< XXXX@XXXXX.COM > < XXXX@XXXXX.COM >
<# XXXX@XXXXX.COM >
< XXXX@XXXXX.COM >
< XXXX@XXXXX.COM >
<# XXXX@XXXXX.COM >
< XXXX@XXXXX.COM >
61.246.84.130
/
the
< XXXX@XXXXX.COM >
which
my


Similar Threads

1. Getting client ip address - CSharp/C#

2. getting client ip in sock-client on server

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

 } 


3. Socket Server - Client IP address and Name - Microsoft .NET Framework

4. Getting Client IP Address

Hi there,
              there are many articles on the web describing how to retrieve 
the IP Address of the client machine that is connected to your web server. 
The examples all seem to use the ServerVariables to get this.  Why would you 
use:

Request.ServerVariables["HOST_ADDR"]

rather than the built in attribute like so?:

Request.UserHostAddress


5. Getting the IP address of the client PC - Asp.Net

6. Getting client machine's configured DNS IP addresses

7. Getting MAC address/Physical IP address of machine through C# - CSharp/C#

8. Get Client IP address

Hi,



In my ASP.NET application I am using client IP, I was using

"Request.ServerVariables["REMOTE_ADDR"]" to get client IP. Now I moved my 
servers to under Cisco content switch for load balancing. Now 
"Request.ServerVariables["REMOTE_ADDR"]" and

"Request.UserHostAddress", both returning Content switch IP instead of 
client IP. Is there any way I can get client IP from C# under load balancing 
mode??

Thanks in Advance,

SA.