Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2011
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts

    how to get client's IP address in socket programming

    Hi,

    Friends, I am troubling to get client's IP address in socket programming.
    I am able to get it if I use "use Socket" however I am unable to find a way to get ip address of client when I use "use IO::Socket".

    Can anyway please tell me couple of lines which I can add in below code to get client's IP address?

    #!/usr/bin/perl;
    use strict;
    use warnings;
    use IO::Socket;
    my ($txt,$new_sock);
    my $data="123"; #Default data value.
    my $sock = new IO::Socket::INET (
    LocalHost => 'xxx.xxx.xx.xx', #delieberately edited IP
    LocalPort => '7070',
    Proto => 'tcp',
    Listen => SOMAXCONN,
    Reuse => 1,
    );
    die "Could not create socket: $!\n" unless $sock;
    print "Socket created Successfully\n\n";
    $new_sock = $sock->accept();
    while($data = <$new_sock>){
    if($data eq "register"){
    #print("$ip\n");
    print "Data:$data\n";
    }
    }
    #close($sock);


    Also, How can I keep client as well as server alive during their communication. Here my client script and server script exits once server prints the $string it received from client?


    Thanks,
    Last edited by rkaranjkar; 01-14-2011 at 06:17 AM.

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,943
    Thanks
    2
    Thanked 169 Times in 164 Posts
    The module's connected method documentation states:
    connected

    If the socket is in a connected state the peer address is returned. If the socket is not in a connected state then undef will be returned.
    So, try:
    Code:
    my $remote_IP = $sock->connected;

  • #3
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,943
    Thanks
    2
    Thanked 169 Times in 164 Posts
    This is the best book that I've found on the subject.

    http://www.amazon.com/Network-Progra.../dp/0201615711

  • #4
    New Coder
    Join Date
    Jan 2011
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks FishMonger.
    this connected() method is giving me some unknown output.

    Here I am getting ÚÀ¨\.

    Looks like some post processing is required. Could you please help here?

  • #5
    New Coder
    Join Date
    Jan 2011
    Posts
    18
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Got it.

    sockhost() and peerhost() gives the IP addresses of server and client.


  •  

    Tags for this Thread

    Posting Permissions

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