Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
1. use strict; use warnings; They are your friends, and will save you many many many hours of troubleshooting and head scratching. They aren't perfect, you'll still have to do some work, but certainly a lot less...

2. Keep your code layout tidy paying particular attention to indentation. It may feel awkward, even unnessesary at first, but it'll get easier and easier until you find yourself writing well layed out code without even thinking about it... In short, well layed out code will make your life easier.

3. Well done for posting a short example of your code that demonstrates the problem. In this particuar case it would also be worth posting an example of the output you are seeing. Often its easy to just stick __END__ on the end of your code post and paste your output below it.

4. I tried your code (modified slightly to conform to strict rules) and it seems to work fine. No weird buffering issues are apparent... If the revised code below don't solve the problem your having, please clarify with further details and what output your seeing. Cheers!


Your code slightly modified:
#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" +; ioctl($new_sock, 0x8004667e, \$nonblocking); my $buf; sysread($new_sock, $buf, 10); print "$buf\n"; syswrite($new_sock, $buf, 10); $new_sock->flush; sysread($new_sock, $buf, 1); print "$buf\n"; $new_sock->flush; syswrite($new_sock, "12", 2); }

#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 $nonblocking = 1; ioctl($sock, 0x8004667e, \$nonblocking); my $buf = "1234567890"; syswrite($sock, $buf, 10); sysread($sock, $buf, 10); $sock->flush; print "Bytes 10 = $buf\n"; syswrite($sock, "1", 1); print "\n"; $sock->flush; sysread($sock, $buf, 2); print "Bytes 2 = $buf\n"; __END__ C:\Temp>perl client.pl A tcp socket on localhost connected to 7890 Bytes 10 = 1234567890 Bytes 2 = 1234567890 C:\Temp>perl client.pl A tcp socket on localhost connected to 7890 Bytes 10 = 1234567890 Bytes 2 = 1234567890 C:\Temp>perl client.pl A tcp socket on localhost connected to 7890 Bytes 10 = 1234567890 Bytes 2 = 1234567890 C:\Temp>

In reply to Re: IO Socket buffer flushing by desemondo
in thread IO Socket buffer flushing by baldeep8

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-29 00:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found