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

In reply to Socket programm by hari9

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.