Obtaining a Connecting Client's IP Address
What I would like to know is how I can go about obtaining the IP of a client connecting to my server while trying to login. The reason for this is because I would like my server to compare the IP with acceptable IPs in an XML file.
Now I know one solution would be to make my own packets and have the client send his/her IP somewhere in a login packet, put implementing packets takes quite a lot of time and this is only a small project, experiment, w/e.
Any help will be greatly appreciated. :)
Re: Obtaining a Connecting Client's IP Address
Re: Obtaining a Connecting Client's IP Address
It really depends what you are using for connection.
is it sockets? ordinary HTTP? what are you using for client to connect?
Re: Obtaining a Connecting Client's IP Address
I am using source based off of the example found here:
http://www.codeguru.com/Csharp/Cshar...cle.php/c7695/
My client connects using this function:
void Join(object sender, System.EventArgs e)
{
if(port.Text == "" || IP.Text == "")
{
MessageBox.Show("IP Address and Port Number are required to connect to the Server\n");
return;
}
try
{
m_clientSocket = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
IPAddress ip = IPAddress.Parse (IP.Text);
int iPortNo = System.Convert.ToInt16 ( port.Text);
IPEndPoint ipEnd = new IPEndPoint (ip,iPortNo);
m_clientSocket.Connect ( ipEnd );
if(m_clientSocket.Connected)
{
WaitForData();
}
}
catch(SocketException se)
{
string str = "\nConnection failed, is the server running?\n" + se.Message;
MessageBox.Show (str);
}
}
Re: Obtaining a Connecting Client's IP Address
get address list from Ipendpoint.
Re: Obtaining a Connecting Client's IP Address
care to elaborate on that? :)
I'm still new to C# sockets...
Re: Obtaining a Connecting Client's IP Address
in your server...
Socket m_ConnectedClientSocketHandler = /*Client connection*/
Console.WriteLine(IPAddress.Parse (((IPEndPoint)ConnectedClientSocketHandler .RemoteEndPoint).Address.ToString ()));
Re: Obtaining a Connecting Client's IP Address
Quote:
Originally Posted by MAL1C3
What I would like to know is how I can go about obtaining the IP of a client connecting to my server while trying to login. The reason for this is because I would like my server to compare the IP with acceptable IPs in an XML file.
Now I know one solution would be to make my own packets and have the client send his/her IP somewhere in a login packet, put implementing packets takes quite a lot of time and this is only a small project, experiment, w/e.
Any help will be greatly appreciated. :)
try either of this
x=Request.ServerVariables["REMOTE_ADDR"]
or
x=Request.ServerVariables["REMOTE_HOST"]
Re: Obtaining a Connecting Client's IP Address
suhaib, that would work perfect for a web request. but here, as i understand, he is working using Sockets and need to get the IP address of the connecting client.
thanks for you suggestion. it is nice, now I will know too :)