Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

UDP Server doesn't receive before newline

by nitin1704 (Sexton)
on Dec 04, 2012 at 03:54 UTC ( #1006987=perlquestion: print w/replies, xml ) Need Help??
nitin1704 has asked for the wisdom of the Perl Monks concerning the following question:

So I made a simple UDP client and server using IO::Socket. The code is given below. The problem is that the server prints the message from client only when it ends with a newline. Is there a way to change this? I tried disabling output buffering on the client side, but that didn't work.

use IO::Socket; use strict; use warnings; my $sock = IO::Socket::INET->new( Proto => 'udp', PeerPort => 23456, PeerAddr => '', ) or die "Could not create socket: $!\n"; select($sock); $|=1; $sock->send("message from client") or die "Send error: $!\n";

use IO::Socket; use strict; use warnings; my $server = IO::Socket::INET->new(LocalPort => 23456, Proto => 'udp') or die "Couldn't be a udp server on port 23456 +: $@\n"; my $MAX_TO_READ = 1024; my $datagram = ''; while($server->recv($datagram, $MAX_TO_READ)){ print $datagram; }

Replies are listed 'Best First'.
Re: UDP Server doesn't receive before newline
by choroba (Chancellor) on Dec 04, 2012 at 09:10 UTC
Re: UDP Server doesn't receive before newline
by zwon (Monsignor) on Dec 05, 2012 at 02:17 UTC

    UDP doesn't have buffering. Buffering happens on the server side -- stdio library buffers output. Try:

    print "$datagram\n";
      That worked. Thanks! Why doesn't the stdio library buffer output like this when I do a normal print statement (instead of doing it like this in a socket-recv() scenario)?
        Socket is irrelevant. Try:
        perl -e'while(1) { print "Datagram"; sleep 1; }'
        check man stdio for your system. Mine (GNU/Linux) says:
        Output streams that refer to terminal devices are always line buffered by default; pending output to such streams is written automatically whenever an input stream that refers to a terminal device is read. In cases where a large amount of computation is done after printing part of a line on an output terminal, it is necessary to fflush(3) the standard output before going off and computing so that the output will appear.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1006987]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2016-10-23 06:41 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (300 votes). Check out past polls.