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

get client ip address in C# socket client application

P: n/a
I am making socket client application in C#
how can i get ip address of client who has connected to server
Jan 12 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
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
Jan 12 '06 #2

P: n/a
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
Jan 12 '06 #3

P: n/a
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
Jan 12 '06 #4

P: n/a
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
Jan 12 '06 #5

P: n/a
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
Jan 12 '06 #6

P: n/a
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
Jan 12 '06 #7

P: n/a
Sandeep Singh wrote:
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.ToStrin g());
//==========

--
Vit Zayko
Jan 12 '06 #8

P: n/a
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.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 12 '06 #9

P: n/a
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[i]= new client(server.AcceptTcpClient());


--
Vit Zayko
Jan 12 '06 #10

P: n/a
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
"Vitaly Zayko" <vitaly_at_zayko_dot_net> wrote in message
news:eG*************@TK2MSFTNGP14.phx.gbl...
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[i]= new client(server.AcceptTcpClient());


--
Vit Zayko

Jan 18 '06 #11

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: <ea**************@TK2MSFTNGP15.phx.gbl> <uC**************@TK2MSFTNGP11.phx.gbl> <43************@newsgroups.nospam>
<#j**************@TK2MSFTNGP09.phx.gbl>
<ul*************@tk2msftngp13.phx.gbl>
<eG*************@TK2MSFTNGP14.phx.gbl>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: <#b*************@TK2MSFTNGP15.phx.gbl>
Newsgroups: vitaly_at_zayko_dot_net,microsoft.public.dotnet.la nguages.csharpNNTP-Posting-Host: dsl-del-dynamic-145.85.246.61.touchtelindia.net 61.246.85.145Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP15.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[i].
in next line to accept client
cl[i]= new client(server.AcceptTcpClient());
cl[i].?RemoteEndpoint is not displayed
"Vitaly Zayko" <vitaly_at_zayko_dot_net> wrote in message
news:eG*************@TK2MSFTNGP14.phx.gbl...
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[i]= new client(server.AcceptTcpClient());


--
Vit Zayko



Jan 20 '06 #12

P: n/a
can we get remote ip address in a simple way
using framework 2.0
""TerryFei"" <v-******@online.microsoft.com> wrote in message
news:cO**************@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: <ea**************@TK2MSFTNGP15.phx.gbl>

<uC**************@TK2MSFTNGP11.phx.gbl> <43************@newsgroups.nospam>
<#j**************@TK2MSFTNGP09.phx.gbl>
<ul*************@tk2msftngp13.phx.gbl>
<eG*************@TK2MSFTNGP14.phx.gbl>
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: <#b*************@TK2MSFTNGP15.phx.gbl>
Newsgroups:

vitaly_at_zayko_dot_net,microsoft.public.dotnet.la nguages.csharp
NNTP-Posting-Host: dsl-del-dynamic-145.85.246.61.touchtelindia.net

61.246.85.145
Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP15.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.languages.csharp:37937 4
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

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
"Vitaly Zayko" <vitaly_at_zayko_dot_net> wrote in message
news:eG*************@TK2MSFTNGP14.phx.gbl...
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[i]= new
client(server.AcceptTcpClient());

--
Vit Zayko


Jan 20 '06 #13

P: n/a
Hi Ankit,
Nice to hear from you again! :)
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. 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" <ef*****@newsgroups.nospam>
References: <ea**************@TK2MSFTNGP15.phx.gbl> <uC**************@TK2MSFTNGP11.phx.gbl> <43************@newsgroups.nospam>
<#j**************@TK2MSFTNGP09.phx.gbl>
<ul*************@tk2msftngp13.phx.gbl>
<eG*************@TK2MSFTNGP14.phx.gbl>
<#b*************@TK2MSFTNGP15.phx.gbl>
<cO**************@TK2MSFTNGXA02.phx.gbl>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: <OR**************@TK2MSFTNGP09.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 NGP09.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"" <v-******@online.microsoft.com> wrote in message
news:cO**************@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: <ea**************@TK2MSFTNGP15.phx.gbl>

<uC**************@TK2MSFTNGP11.phx.gbl> <43************@newsgroups.nospam> <#j**************@TK2MSFTNGP09.phx.gbl>
<ul*************@tk2msftngp13.phx.gbl>
<eG*************@TK2MSFTNGP14.phx.gbl>
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: <#b*************@TK2MSFTNGP15.phx.gbl>
Newsgroups:

vitaly_at_zayko_dot_net,microsoft.public.dotnet.la nguages.csharp
NNTP-Posting-Host: dsl-del-dynamic-145.85.246.61.touchtelindia.net

61.246.85.145
Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFT NGP15.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.languages.csharp:3793 74
X-Tomcat-NG: microsoft.public.dotnet.languages.csharp

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
"Vitaly Zayko" <vitaly_at_zayko_dot_net> wrote in message
news:eG*************@TK2MSFTNGP14.phx.gbl...
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[i]= new
> client(server.AcceptTcpClient());

--
Vit Zayko



Jan 23 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.