Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Dear Monks, I have created a simple client and server program which will just consecutively write and read data from a single socket connection. I have enabled non blocking socket using ioctl to ensure that in case i dont get anything to read from the socket then sysread will not wait there indefinitely. If ioctl is disabled then i am able to send and receive the appropriate data on both the client and server. With ioctl enabled i am not able to receive the data on the client side consistently though the sysread no longer blocks when there is no data to read. I have done 2 syswrites and 2 sysreads on both the client and server sides. On the client side i find that sometimes i get the data of the first sysread operation when i do the second sysread. The same problem happens with sysread on the server side as well. It looks like the socket buffer is not getting flushed. I have tried a number of methods to flush the buffer including autoflush, flush, $| and print LOG "". In all cases still i find that the buffer does not get properly flushed and that the problem persists. I am new to socket programming in Perl. Please help.

Client code :
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: $!"; } $nonblocking = 1; ioctl($sock, 0x8004667e, \$nonblocking); $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";
Server code:
use IO::Socket; $| = 1; my $sock = new IO::Socket::INET( LocalHost => "localhost", LocalPort => 7890, Proto => "tcp", Listen => SOMAXCONN, Reuse => 1, Timeout => 20 ); if ($sock) { print "A $Proto socket created on $LocalHost listening on $LocalPor +t\n"; } else { die "Error - no listening socket created : $!"; } $flush = 1; while (($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"; $nonblocking = 1; ioctl($new_sock, 0x8004667e, \$nonblocking); 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); }

In reply to IO Socket buffer flushing by baldeep8

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?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (1)
As of 2023-09-27 05:29 GMT
Find Nodes?
    Voting Booth?

    No recent polls found