in reply to Re^3: exchanging strings on the network
in thread exchanging strings on the network

here is my code this is for the client
print "Hello welcome to the fsclient\n"; my ($user, $passwd, $host, $port, $command, $numArgs); $numArgs = $#ARGV + 1; $command=$ARGV[7]; my $client = IO::Socket::INET->new( PeerAddr => 'charlie', PeerPort => '70000', Proto => 'tcp',) or die "cannot connect to $port at $host\n"; while(<$client>) { print $_; print $client "Username $user Pass $pass";} close($client) or die "$!\n"; print "connection closed\n";
this is the code for the server
#!/bin/perl -w use IO::Socket::INET; use strict; print "Hello welcome to the Fs server\n"; my $server = new IO::Socket::INET ( LocalHost => 'charlie', LocalPort => '70000', Proto => 'tcp', Listen => 1, Reuse => 1, ); die "Could not create socket: $!\n" unless $server; print "Great just created a socket\n"; my $new_sock = $server->accept(); print $new_sock "Fileshare Version 0.1\n"; while(<$new_sock>) { print "Here is what i received from you\n"; print "$_\n";} print "OK its time to close\n"; close($server);
UPDATE: ok the it is working with double quotes now plus right now the client is receiving the string "Fileshare Version 0.1" . but when it tries to send the user name password to the server it doesnt happen.

Replies are listed 'Best First'.
Re^5: exchanging strings on the network
by holli (Abbot) on May 10, 2005 at 10:57 UTC
    I think your code never enters this loop
    while(<$client>) { print $_; print $client "Username $user Pass $pass";}
    I think what you need is more like this:
    print $client "Username $user Pass $pass"; while(<$client>) { print $_; <STDIN>; #read input from user (changes $_!) print $client $_; }
    For the other part, I don't see why this (code below) would not work. again. if the variables contain what they should. Always check your input!
    $host = $ARGV[2]; #or whatever it is in your ARGV $port = $ARGV[4]; my $client = IO::Socket::INET->new( PeerAddr => $host, PeerPort => $port,


    holli, /regexed monk/
      ok the code is working now. there is a problem with it. the first string from the server reaches the client immediately. how ever the string from the client containing the user name password is not displayed on the server side. but if i control+c and exit than it displays the string. but if i terminate from the server side nothing is displayed.
        STDOUT is line buffered by default, meaning you won't see any text until you print a newline to it (or until perl exits and flushes its buffers on its way out):
        print "one two three"; sleep 5; print "four five six\n";
        This should stay blank for 5 seconds and then print out all of the text. If you want your prints to STDOUT to appear immediately, set the global variable $| to 1. This sets autoflushing on the currently selected filehandle, which by default is STDOUT:
        $|++; print "one two three"; sleep 5; print "four five six\n";
        This should print out the first line, sleep 5 seconds, and then print out the second. Easy! Keep in mind terminals buffer output for performance reasons, so don't do this if you're printing out a large amount of data. You probably won't hit this wall while you're still learning, though.

        Update: Ah, sorry, not quite. The problem is this: print $client "Username $user Pass $pass"; You don't send through a "\n" at the end. The client is done sending data so it tries to read from the socket again, but the server is still blocking, waiting for a newline that will never arrive. This is what's known as a deadlock.