Hello Monks,

The answer to my question probably is really simple but I can not figure it out.

I am trying to make my server running on PeerAddr and PeerPort. Based on my current settings my server is running on localhost. I found my current allocated IP and assigned it to the server and also I chosen randomly 5000 port number for the file descriptors.

When the server is running on localhost the client can send and receive the messages. When I am assigning the servers PeerAddr an external IP not the localhost then I get the following error:

Client error while received: Connection refused

Since so far I have only been experimenting on localhost can someone help me understand where I am going wrong and the server can not operate on external addresses.

Sample of server.pl code:

#!/usr/bin/perl use strict; use warnings; use IO::Socket::INET; my $port = 5000; my $IP = "127.0.0.1"; # flush after every write $| = 1; my $server_socket = IO::Socket::INET->new( #PeerAddr => inet_ntoa(INADDR_BROADCAST), PeerPort => $port, PeerAddr => $IP, #LocalPort => $port, Proto => 'udp', Type => SOCK_DGRAM, LocalAddr => 'localhost', Broadcast => 1, ReuseAddr => 1, ReusePort => 1 ) or die "Can't bind: $@\n"; my $recieved_data; printf("\nServer is up, listens on PORT: ".$port." waiting for client. +..\n"); while (1) { # read operation on the socket $server_socket->recv( $recieved_data , 1024 ) or die "Server error received: $!\n"; #get the peerhost and peerport at which the recent data received. my $peer_address = $server_socket->peerhost(); my $peer_port = $server_socket->peerport(); print "\n($peer_address , $peer_port) said : $recieved_data"; #send the data to the client at which the read/write operations do +ne recently. my $data = "Echo: ".$recieved_data.""; $server_socket->send( $data ) or die "Server error send: $!\n"; } $server_socket->close();

Sample of client.pl code:

#!/usr/bin/perl use strict; use warnings; use IO::Socket::INET; my $port = 5000; my $IP = "127.0.0.1"; # flush after every write $| = 1; my $client_socket = new IO::Socket::INET ( PeerHost => $IP, Type => SOCK_DGRAM, PeerPort => $port, Proto => 'udp' ) or die "ERROR in Socket Creation: $@\n"; my $Peer_Port = $client_socket->peerport(); my $data_send = "Test!!!!!"; $client_socket->send( $data_send ) or die "Client error while sending: $!\n"; #read operation $client_socket->recv( my $data_rcv , 1024 ) or die "Client error while received: $!\n"; print "Received data: $data_rcv\n"; $client_socket->close();

Thanks in advance for your time and effort.

Seeking for Perl wisdom...on the process of learning...not there...yet!

In reply to UDP server/client Connection refused by thanos1983

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.