Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: IO Socket buffer flushing

by eric256 (Parson)
on Jan 29, 2010 at 16:55 UTC ( #820365=note: print w/ replies, xml ) Need Help??


in reply to Re: IO Socket buffer flushing
in thread IO Socket buffer flushing

Just playing around with sockets (cause its fun) I worked up the following code. Its not better realy, just different, and doesn't count on send a specific amount of info each time, intead it uses ->say and ->getline to send whole lines of data. I had to use chomp a couple times and i'm wondering if that could be avoided, but here you have it.

#!/usr/bin/perl #server use strict; use warnings; use IO::Socket; $| = 1; my $nonblocking = 1; my $sock = new IO::Socket::INET( LocalHost => "localhost", LocalPort => 7890, Proto => "tcp", Listen => SOMAXCONN, Reuse => 1, Timeout => 20, ); if ($sock) { print "A socket created on LocalHost listening on LocalPort\n"; } else { die "Error - no listening socket created : $!"; } my $flush = 1; while (my ($new_sock,$c_addr) = $sock->accept()) { my ($client_port, $c_ip) = sockaddr_in($c_addr); my $client_ipnum = inet_ntoa($c_ip); my $client_host =gethostbyaddr($c_ip, AF_INET); print "Got a connection from: $client_host"," [$client_ipnum] \n"; print "Created new socket for reading or writing data to Client\n" +; $new_sock->blocking(1); my $buf; $buf = $new_sock->getline(); chomp($buf); print "Recieved 1 '$buf'\n"; $new_sock->say($buf); $buf = $new_sock->getline(); chomp($buf); print "Recieved 2 '$buf'\n"; $new_sock->say("12"); }
#!/usr/bin/perl #client use strict; use warnings; use IO::Socket; #To flush the buffer print statements $| = 1; my $sock = new IO::Socket::INET( PeerAddr => 'localhost', PeerPort => +7890, Proto => 'tcp'); if ($sock) { print "A tcp socket on localhost connected to 7890\n"; } else { die "Error: $!"; } my $buf = "1234567890"; $sock->say($buf); print "Sent '$buf'\n"; $buf = $sock->getline(); chomp($buf); print "Bytes 10 = '$buf'\n"; $sock->say("1"); $buf = $sock->getline(); chomp($buf); print "Bytes 2 = '$buf'\n";

___________
Eric Hodges


Comment on Re^2: IO Socket buffer flushing
Select or Download Code
Replies are listed 'Best First'.
Re^3: IO Socket buffer flushing
by ikegami (Pope) on Jan 29, 2010 at 17:23 UTC

    TCP provides a stream of bytes. You need to do your own record delimiting. readline works because it waits until a delimiter is found.

    If you need a sysread version (perhaps so you can use select), see Re^3: Can a socket listen for multiple hosts? for an example.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (12)
As of 2015-07-08 07:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (96 votes), past polls