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

Server code -- in linux machine1

#!/usr/bin/perl -w # server2way.pl - a server that reads from # and writes to a client use strict; use IO::Socket; use Sys::Hostname; my $port=shift||9000; my $sock = new IO::Socket::INET( LocalHost => 'localhost', LocalPort => $port, Proto => 'tcp', Listen => SOMAXCONN, Reuse => 2) or die "no socket :$!"; STDOUT->autoflush(1); my($new_sock, $buf); while ($new_sock = $sock->accept()) { # got a client connection, so read # line by line until end-of-file while (defined($buf = <$new_sock>)) { # respond to client request using # a cleverly disguised switch # statement foreach ($buf) { /^PORT$/ and print($new_sock "$port\n"), print "$port\n", last; /^HELLO$/ and print($new_sock "Hi\n"), last; /^NAME$/ and print($new_sock hostname(), "\n"), last; /^DATE$/ and print($new_sock scalar(localtime), "\n"), last; # default case: print $new_sock "DEFAULT\n"; } } close $new_sock; }

client1 --in linux machine1

#!/usr/bin/perl -w # client2way.pl - a client that writes to # and reads from a server use strict; use IO::Socket; require "server2way.pl"; my $host = shift || 'localhost'; my $port = shift || 9000; my $sock = new IO::Socket::INET( PeerAddr => $host, PeerPort => $port, Proto => 'tcp'); $sock or die "no socket :$!"; # send message to server print $sock "PORT\n"; #print port number print scalar <$sock>; print $sock "HELLO\n"; # print server response to STDOUT print scalar <$sock>; # send and print more stuff print $sock "NAME\n"; print scalar <$sock>; print $sock "DATE\n"; print scalar <$sock>; print $sock "NONE\n"; print scalar <$sock>; close $sock;

Client2 -- in windows machine

#!/usr/bin/perl -w # client2way.pl - a client that writes to # and reads from a server use strict; use IO::Socket; #require "server2way.pl"; my $host = shift || '(I'm entering the address of the server machine h +ere")'; my $port = shift || 9000; my $sock = new IO::Socket::INET( PeerAddr => $host, PeerPort => $port, Proto => 'tcp'); $sock or die "no socket :$!"; # send message to server print $sock "PORT\n"; #print port number print scalar <$sock>; print $sock "HELLO\n"; # print server response to STDOUT print scalar <$sock>; # send and print more stuff print $sock "NAME\n"; print scalar <$sock>; print $sock "DATE\n"; print scalar <$sock>; print $sock "NONE\n"; print scalar <$sock>; close $sock;

How will server recognise the ping from server2, which is on different machine and which it is not even connected to.

thanks

Replies are listed 'Best First'.
Re: Socket programm
by ikegami (Patriarch) on Jul 19, 2010 at 17:00 UTC

    which is on different machine and which it is not even connected to.

    What do you mean by "not even connected to"?

    accept accepts waits for a connection. It can come from any machine unless you tell it otherwise.

    As I've already explained, you are telling it otherwise with LocalHost => 'localhost'. You're telling your server to only listen for connections coming on the loopback adapter, and only clients on the local machine can reach that adapter.

    Get rid of LocalHost => 'localhost'.