This problem's starting to bug me now ;-)

So I've done some tests.

  1. Specify the local port as part of the LocalAddr parameter ('1.2.3.4:8080')

    Set something listening on a port:

    perl -mHTTP::Daemon -e '$d=new HTTP::Daemon(LocalPort=>12345);while ($ +c=$d->accept){sleep 10;$c->send_file_response("index.html")}'
    Try to connect to it:
    perl -e 'use LWP::Simple;@LWP::Protocol::http::EXTRA_SOCK_OPTS = (Loca +lAddr=>"localhost:54321");get("http://localhost:12345")'
    Check netstat -a
    tcp 71 0 mondas:12345 mondas:51575 ES +TABLISHED
    So we can use a string in the form 'x.x.x.x:port' with LocalAddr

  2. Two processes trying to use the same outgoing port number

    http://jonallen.info/cgi/delay.pl will wait 10 seconds, then print "Hello, World!" (to give me time to run netstat and set up processes on different machines).

    We know now that specifying an local port number works. What would happen if, under high load, your server tried to send several XML 'pings' at once?

    [jj@mondas jj]$ perl -le 'use LWP::Simple;@LWP::Protocol::http::EXTRA_ +SOCK_OPTS = (LocalAddr=>"192.168.1.1:54321");getprint("http://jonalle +n.info/cgi/delay.pl");' Hello, World!
    At the same time, try to access a different remote server using the same local port
    [jj@mondas jj]$ perl -e 'use LWP::Simple;@LWP::Protocol::http::EXTRA_S +OCK_OPTS = (LocalAddr=>"192.168.1.1:54321");getprint("http://www.goog +le.com");' 500 Can't connect to www.google.com:80 (Cannot assign requested addres +s) <URL:http://www.google.com>
    So if you are going to restrict the outgoing port numbers, be aware that in some conditions the local port you need will be in use. If you *really* need to do this, set up a range of ports and have your program try to use each one in turn and only fail if they are all in use.

  3. Using the same port number on different aliased IP addresses

    Machine has 192.168.1.10 aliased to 192.168.1.1

    [root@mondas log]# ifconfig eth0 Link encap:Ethernet HWaddr 52:54:05:C5:13:7A inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255 +.0 EtherTalk Phase 2 addr:65280/97 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:27996973 errors:0 dropped:2 overruns:0 frame:6 TX packets:32099772 errors:484 dropped:0 overruns:0 carrier: +968 collisions:146707 RX bytes:3038904039 (2898.1 Mb) TX bytes:4085277291 (3896.0 + Mb) eth0:0 Link encap:Ethernet HWaddr 52:54:05:C5:13:7A inet addr:192.168.1.10 Bcast:192.168.1.255 Mask:255.255.25 +5.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 EtherTalk Phase 2 addr:0/0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:17172 errors:0 dropped:0 overruns:0 frame:0 TX packets:17172 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 RX bytes:4381193 (4.1 Mb) TX bytes:4381193 (4.1 Mb)
    Start Apache as an unprivileged user (will bind to port 8080).
    [jj@mondas jj]$ perl -e 'use LWP::Simple;@LWP::Protocol::http::EXTRA_S +OCK_OPTS = (LocalAddr=>"192.168.1.1:8080");getprint("http://jonallen. +info/cgi/delay.pl");' 500 Can't connect to jonallen.info:80 (Address already in use) <URL:ht +tp://jonallen.info/cgi/delay.pl> [jj@mondas jj]$ perl -e 'use LWP::Simple;@LWP::Protocol::http::EXTRA_S +OCK_OPTS = (LocalAddr=>"192.168.1.10:8080");getprint("http://jonallen +.info/cgi/delay.pl");' 500 Can't connect to jonallen.info:80 (Address already in use) <URL:ht +tp://jonallen.info/cgi/delay.pl>
    You can of course use the Listen and BindAddress directives in httpd.conf to force Apache to use only a certain IP alias. However the default configuration will bind to every IP address, so it is worth checking with netstat that nothing else is using the local port that you want to use.

  4. Using the same port number for incoming & outgoing connections

    Start the HTTP::Daemon one-liner (on 192.168.1.1). From another computer, connect to this daemon with a local port of 11111

    perl -e 'use LWP::Simple;@LWP::Protocol::http::EXTRA_SOCK_OPTS = (Loca +lAddr=>"192.168.1.8:11111");getprint("http://192.168.1.1:12345");'
    Then (at the same time) from the machine running HTTP::Daemon, try to use the local port 11111:
    perl -e 'use LWP::Simple;@LWP::Protocol::http::EXTRA_SOCK_OPTS = (Loca +lAddr=>"192.168.1.1:11111");getprint("http://jonallen.info/cgi/delay. +pl");'
    Check netstat on 192.168.1.1
    tcp 0 0 mondas.coldsoluti:11111 212.69.202.117:http ES +TABLISHED tcp 73 0 mondas.coldsoluti:12345 lclus1.coldsoluti:11111 ES +TABLISHED
    So incoming connections do not restrict what ports you can use for outgoing connections.

This leads me to the conclusion that your code is fine, and as long as nothing else is listening on port 8080, it should work. Note however that if you try these sort of examples over the Internet, caches and proxies run by ISPs will change the local port numbers. So if machine 'A' opens a connection with a destination address of machine 'B' and a local port '22222', you cannot guarantee that when machine 'B' accepts the connection the remote port will be '22222'.

I would suggest that you disable your firewall (at least for outgoing connections), try your code again, and use netstat to find out exactly what it is doing.

JJ


In reply to Re: LWP and LocalIP/LocalPort by jj808
in thread LWP and LocalIP/LocalPort by jk2addict

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.