473,385 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

get client ip address in C# socket client application

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
13 28667
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Krzysztof Pa¼ | last post by:
Hi, I want to make simple client in phyton, which would be able to communicate with Java server using SSL sockets. There is the Java clients, which is doing this - so I'm pretty sure, that Java...
4
by: jas | last post by:
I have a basic client/server socket situation setup....where the server accepts a connection and then waits for commands. On the client side, I create a socket, connect to the server...then I...
3
by: Matthew King | last post by:
Hi all I've written a asynchronous socket client class, but i've found that in order to consume it I have to use events, and cannot simply for example SocketClient client = new...
2
by: trint | last post by:
I have an app that uses the standard Socket Client code. I send it a message to call a function and it receives the message, debugs through the function, but the function doesn't actually fire. ...
7
by: Ole | last post by:
Hi, I'm going to develop a socket communication between an instrument (running CE 5.0 with Compact Fraework V2) and a PC. As the instrument should only connect to one PC at a time I believe that...
4
by: SpreadTooThin | last post by:
client: import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("192.168.1.101", 8080)) print 'Connected' s.send('ABCD') buffer = s.recv(4) print buffer s.send('exit')
0
by: swisSL | last post by:
Hi, im developing a client server socket program in C#.net When a client is connected to the server socket is there a function or way to detect the clients IP Adderss. Thank you.. swisSL
3
by: ChristianProgrammer | last post by:
Its been a month and a half now. I have tried to get my WSSF web service to reference a class library that IS a Socket Client. The Socket Client in question runs fine when called by a testing exe....
0
by: Demosthenes | last post by:
I am using vs 2003, below is the callback method for my socket. The way I understand how all this works is: sockDataArrival gets started as a thread, and reads from the server using EndReceive. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.