windows problem with socket write

Discussion in 'Perl Misc' started by mastermagrath, Sep 25, 2006.

  1. Hi all,

    I've a problem with a simple socket client/server script which has bee
    described in several threads already but alas none of them have been
    able to help me.

    Its the problem wherein any text i write to the server socket doesn't
    get outpu until i close the client connection. All of the threads on
    the subject all suggest the same thing i.e. make sure you send a
    newline or carriage return or both with the string in question, i've
    tried everything, i've tried sending:

    "text to send\n"
    "text to send\r\n"
    "text to send\n\r"

    Nothing works, nothing gets processed from the servers end until i
    close down the client end, only then does the text get printed in the
    server. I even tried sending an explicit EOF character with the string
    but even that doesn't work!
    I've dumped the very simple code below if anyone could take a look. Has
    anyone got a very simple example script similar to this which works on
    XP acitve state v5.8.8???

    SERVER CODE:

    $server = IO::Socket::INET->new(
    Listen => 5,
    LocalAddr => 'localhost',
    LocalPort => 9000,
    Proto => 'tcp');
    die "ERROR: $!\n" unless $server;

    print "Waiting on connections...\n";
    while($client = $server->accept()){
    print "Connection made, reading data...\n";
    print <$client>;
    print "Connection closed...\n";
    }

    $server->close();

    CLIENT CODE:

    $sock = IO::Socket::INET->new(
    PeerAddr => 'localhost',
    PeerPort => 9000,
    Proto => 'tcp');
    die "Error: $!\n" unless $sock;
    $sock->autoflush(1);
    print $sock "take me to your leader\r\n";
    $answer=<$sock>;
    print $answer;

    close($sock);
    mastermagrath, Sep 25, 2006
    #1
    1. Advertising

  2. mastermagrath

    Sisyphus Guest

    "mastermagrath" <> wrote in message
    news:...
    > Hi all,
    >
    > I've a problem with a simple socket client/server script

    ..
    ..
    >
    > SERVER CODE:
    >
    > $server = IO::Socket::INET->new(
    > Listen => 5,
    > LocalAddr => 'localhost',
    > LocalPort => 9000,
    > Proto => 'tcp');
    > die "ERROR: $!\n" unless $server;
    >
    > print "Waiting on connections...\n";
    > while($client = $server->accept()){
    > print "Connection made, reading data...\n";
    > print <$client>;
    > print "Connection closed...\n";
    > }
    >
    > $server->close();
    >
    > CLIENT CODE:
    >
    > $sock = IO::Socket::INET->new(
    > PeerAddr => 'localhost',
    > PeerPort => 9000,
    > Proto => 'tcp');
    > die "Error: $!\n" unless $sock;
    > $sock->autoflush(1);
    > print $sock "take me to your leader\r\n";
    > $answer=<$sock>;
    > print $answer;
    >
    > close($sock);
    >


    Your 'print <$client>;' in the server script does not function as you
    expect, the specification of 'LocalAddr => 'localhost,' in the server script
    seems to stuff things up, and the client script is waiting for a reply from
    the server but is not getting one. This modified server script works ok for
    me:

    use IO::Socket;
    use warnings;

    $server = IO::Socket::INET->new(
    Listen => 5,
    LocalPort => 9000,
    Proto => 'tcp');
    die "ERROR: $!\n" unless $server;

    print "Waiting on connections...\n";
    while($client = $server->accept()){
    print "Connection made, reading data...\n";
    $data_read = <$client>;
    print $data_read, "\n";
    print $client "Message received ... bye\n";
    print "Connection closed...\n";
    }

    $server->close();

    (All I think I've done differently is have the server print a reply to
    $client, delete the 'LocalAddr' setting from the new() constructor, and
    assign '<client>' to $data_read, rather than try to print it out directly.)

    Cheers,
    Rob
    Sisyphus, Sep 25, 2006
    #2
    1. Advertising

  3. Hi Rob,

    Thanks so much...this works a treat!!!
    mastermagrath, Sep 26, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laszlo Nagy
    Replies:
    1
    Views:
    4,806
    Mark Wooding
    Jan 27, 2009
  2. Jean-Paul Calderone
    Replies:
    0
    Views:
    960
    Jean-Paul Calderone
    Jan 27, 2009
  3. Laszlo Nagy
    Replies:
    0
    Views:
    542
    Laszlo Nagy
    Feb 1, 2009
  4. Steve Holden
    Replies:
    0
    Views:
    655
    Steve Holden
    Feb 1, 2009
  5. Replies:
    19
    Views:
    1,268
    Nigel Wade
    Mar 15, 2012
Loading...

Share This Page