girishatreya2005 has asked for the wisdom of the Perl Monks concerning the following question:

Hello wise ones,

I have written 2 perl scripts , one to start a server and the other to start a client . If I run these two scripts on different machines , they are able to communicate between themselves as desired.

However I want to write a program which forks off the server process and the client processes on machines whose IP addresses I pass as arguments. But that doesn't seem to be working . That is the server process fails with the error "bind: Cannot assign requested address ". Could someone let me know what I'm missing .

I'm suspecting this piece of code in the server script to be the culprit. But cant seem to put what is wrong here . I have tried with pack_sockaddr_in as well. It is also not working.

I'm including only that code which is giving me trouble . The code is failing with the error "bind: Cannot assign requested address" . If I run the same script on a machine whose IP is 10.72.206.79 . It runs fine without any issues.

#!/usr/bin/perl -w use strict; use Socket; #Subroutines sub usage () { die "usage: sock_server.pl [port]"; } #Declarations my $argc; my ($servaddr, $server_port, $server_ip, $server_name, $inaddr); my $nconns; my ($buf, $bufsize); my ($bytesread, $byteswritten, $totalwritten); my @hostentry ; #initializations $server_port = 12000; $nconns = 1; $bufsize = 1024; $buf = undef; $argc = @ARGV; if ($argc > 1) { usage(); } if ($argc == 1) { $server_port = $ARGV[0]; } $server_name = '10.72.206.79' ; @hostentry = gethostbyname($server_name); $server_ip = $hostentry[0]; $inaddr = inet_aton($server_ip) or die("inet_aton: $_\n"); $servaddr = pack_sockaddr_in($server_port, $inaddr); socket(SERVERSOCK, AF_INET, SOCK_STREAM, getprotobyname('tcp')) or die("socket: $!\n"); setsockopt(SERVERSOCK, SOL_SOCKET, SO_REUSEADDR, 1) or die("setsockopt: $!\n"); bind(SERVERSOCK, $servaddr) or die("bind: $!\n");

Replies are listed 'Best First'.
Re: Starting a server on a machine whose IP is specified
by Corion (Patriarch) on Aug 21, 2010 at 08:40 UTC

    You posted far too much code to wade through, especially as the error occurs within the first 10 lines of your code. Please try to reduce your failing program to a very short program that still exhibits the same error before posting here.

    My guess is that there already is another server program using that same port on the machine, and so your new program can't bind to that socket.

Re: Starting a server on a machine whose IP is specified
by Crackers2 (Parson) on Aug 21, 2010 at 15:53 UTC

    Maybe I'm reading this wrong, but it sounds to me as if you're trying to run the server script on your local machine while passing it the IP address of the machine where you want to run it as a parameter, and are expecting the bind call to magically start listening on the remote machine.

    Things don't work that way. If you want your server to run on the remote machine, you have to run your code there. You can either copy it manually yourself before you run it from that machine, or you can have the script copy itself to the remote machine.