Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

UDP Server doesn't receive before newline

by nitin1704 (Sexton)
on Dec 04, 2012 at 03:54 UTC ( [id://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.

client.pl

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

server.pl

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 (Cardinal) on Dec 04, 2012 at 09:10 UTC
Re: UDP Server doesn't receive before newline
by zwon (Abbot) 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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-24 22:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found