Loading ...
Sorry, an error occurred while loading the content.

IO::Socket:INET Client problem

Expand Messages
  • diarmuid_obriain <diarmuid_obriain@3com.
    I have a problem with a TCP Client I am working with. It works fine if the data on the server that is being sent to the socket has a r n at the end of the
    Message 1 of 4 , Dec 23, 2002
    • 0 Attachment
      I have a problem with a TCP Client I am working with. It works fine
      if the data on the server that is being sent to the socket has
      a '\r\n' at the end of the line however if it does not then that line
      is not printed until after the next \r\n action. This basically makes
      it impossible for the client to see a command prompt from the server
      which would naturally not have the \r\n at the end of the line. Is
      there any way in Perl of forcing the client to print the contents of
      the buffer without waiting for the \r\n sequence.

      Here is the client I am working with.


      Any help appreciated.

      Diarmuid

      ---------------------- tcpClient.pl -------------------------

      #!/usr/bin/perl -w

      use strict;
      use IO::Socket;
      my ($program, $server, $port, $forkchild, $socket, $element);

      if ($0 =~ /.*\/(\S+?)$/){
      $program = $1;
      }else{
      $program = $0;
      }

      unless (@ARGV == 2){
      die "\r\nusage: $0 <host> <port>\r\n\r\n";
      }else{
      ($server, $port) = @ARGV;
      }

      $socket = IO::Socket::INET->new(Proto => 'tcp',
      PeerAddr => $server,
      PeerPort => $port)
      or die "cannot connect : $|";

      print "\r\n[Connected to $server:$port]\r\n";

      print "\r\nFork failed: $!\r\n\r\n" unless (defined ($forkchild = fork
      ()));

      if ($forkchild){
      while ($element = <$socket>) {
      print STDOUT "$element";
      }
      kill("TERM", $forkchild);

      }else{
      while ($element = <STDIN>) {
      print $socket "$element";
      }
      }

      ---------------------------------------------------------------------
    • Rob Nagler
      ... This isn t the right list for this type of question, but I ll respond anyway: while (sysread($socket, $element, 8192)) { print STDOUT $element; } The
      Message 2 of 4 , Dec 23, 2002
      • 0 Attachment
        "diarmuid_obriain <diarmuid_obriain@...>" <diarmuid_obriain@...> writes:
        > while ($element = <$socket>) {
        > print STDOUT "$element";
        > }

        This isn't the right list for this type of question, but I'll respond
        anyway:

        while (sysread($socket, $element, 8192)) {
        print STDOUT $element;
        }

        The readline (<>) function stops reading a \n. There's no way around
        this by definition.

        Try http://www.perlmonks.org for questions like this in the future.

        Cheers,
        Rob
      • diarmuid_obriain <diarmuid_obriain@3com.
        Rob, Thanks, I tried this but it still doesn t work. Diarmuid
        Message 3 of 4 , Dec 23, 2002
        • 0 Attachment
          Rob,

          Thanks,

          I tried this but it still doesn't work.

          Diarmuid
        • Tom Brown
          I think Rob s answer was backwards... e.g. the read side of things, not the write (print) side of things, but it isn t very clear where the problem lies. His
          Message 4 of 4 , Dec 23, 2002
          • 0 Attachment
            I think Rob's answer was backwards... e.g. the read side of things, not
            the write (print) side of things, but it isn't very clear where
            the problem lies. His answer is quite correct <> isn't the normal
            way to grab data from a socket... it is meant for line oriented
            input...

            I think you're just looking for

            my $old = select $socket;
            $| = 1;
            select $old;

            which changes the filehandle to no longer be buffered. You may
            need to run it for STDOUT too.

            -Tom



            On Mon, 23 Dec 2002, diarmuid_obriain <diarmuid_obriain@...> wrote:

            > I have a problem with a TCP Client I am working with. It works fine
            > if the data on the server that is being sent to the socket has
            > a '\r\n' at the end of the line however if it does not then that line
            > is not printed until after the next \r\n action. This basically makes
            > it impossible for the client to see a command prompt from the server
            > which would naturally not have the \r\n at the end of the line. Is
            > there any way in Perl of forcing the client to print the contents of
            > the buffer without waiting for the \r\n sequence.
          Your message has been successfully submitted and would be delivered to recipients shortly.