thanos1983 has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: UDP server/client Connection refused
by NetWallah (Canon) on Sep 26, 2014 at 04:29 UTC | |
by thanos1983 (Parson) on Sep 26, 2014 at 13:44 UTC | |
by NetWallah (Canon) on Sep 29, 2014 at 17:50 UTC | |
by thanos1983 (Parson) on Sep 29, 2014 at 23:47 UTC | |
by NetWallah (Canon) on Sep 30, 2014 at 00:35 UTC |