CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2005
    Posts
    43

    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.

  2. #2
    Join Date
    Jan 2005
    Posts
    43

    Re: Obtaining a Connecting Client's IP Address

    *bump*

  3. #3
    Andy Tacker is offline More than "Just Another Member"
    Join Date
    Jun 2001
    Location
    55°50' N 37°39' E
    Posts
    1,503

    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?
    If you think you CAN, you can, If you think you CAN'T, you are probably right.

    Have some nice Idea to share? Write an Article Online or Email to us and You may WIN a Technical Book from CG.

  4. #4
    Join Date
    Jan 2005
    Posts
    43

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

  5. #5
    Andy Tacker is offline More than "Just Another Member"
    Join Date
    Jun 2001
    Location
    55°50' N 37°39' E
    Posts
    1,503

    Re: Obtaining a Connecting Client's IP Address

    get address list from Ipendpoint.
    If you think you CAN, you can, If you think you CAN'T, you are probably right.

    Have some nice Idea to share? Write an Article Online or Email to us and You may WIN a Technical Book from CG.

  6. #6
    Join Date
    Jan 2005
    Posts
    43

    Re: Obtaining a Connecting Client's IP Address

    care to elaborate on that?

    I'm still new to C# sockets...

  7. #7
    Andy Tacker is offline More than "Just Another Member"
    Join Date
    Jun 2001
    Location
    55°50' N 37°39' E
    Posts
    1,503

    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 ()));
    If you think you CAN, you can, If you think you CAN'T, you are probably right.

    Have some nice Idea to share? Write an Article Online or Email to us and You may WIN a Technical Book from CG.

  8. #8
    Join Date
    Jan 2003
    Location
    Bangalore, INDIA
    Posts
    180

    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"]

  9. #9
    Andy Tacker is offline More than "Just Another Member"
    Join Date
    Jun 2001
    Location
    55°50' N 37°39' E
    Posts
    1,503

    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
    If you think you CAN, you can, If you think you CAN'T, you are probably right.

    Have some nice Idea to share? Write an Article Online or Email to us and You may WIN a Technical Book from CG.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured