I am working on some socket code and unable to figure out why there is autoflush
used on socket. It is something like this
my $sock = IO::Socket::Unix(Peer => $socketfilename , Type => SOCK_STREAM)
autoflush $sock 1;
Also there are places with
autoflush STDERR 1
autoflush STDOUT 1
for general filehnadles.
What does it do? Also what happens or will happen if I don't use it? Please give some practical example so that I will understand rather than simple definition.
Data doesn't usually get sent right away on the socket, it is buffered up to a certain point and then sent all-at-once.
Auto-flushing means data goes right through the buffer and then flushed out, not kept in the buffer waiting for other data to arrive and accumulate.
As simple as that.
Without auto-flush:
Tick | DATA sent|Socket Buffer| DATA received
.....|..........|.............|..............
1 | XX | XX | (nothing)
2 | yy | yyXX | (nothing)
3 | ZZZ | ZZZyyXX | (nothing)
4 | t | (empty) | tZZZyyXX
With auto-flush:
Tick | DATA sent | Socket Buffer | DATA received
.....|...........|...............|..............
1 | XX | () | XX
2 | yy | () | yy
3 | ZZZ | () | ZZZ
4 | t | () | t