OK I found the solution. Adding $|=1 did not change anything.
betterworld - you were right half way but that was enough to point me what to do - you mentioned that I used the readline operator (<$sock>) which waits for new line or EOF or error.
Using
read should be used in binmode. But I needed a way to know how many bytes I'm about to read so read func will not wait forever for additional input.
I am doing it by packing the length of the string at the begining of the string, and then send it. The receiving side unpack the length of the incoming string, and read func is issued with the specific length of string that is buffered.
So to be pragmatic, in the above code I changed the "$sock->send" (or "print $sock") to the following:
my $str_with_length = pack("N/a*", $some_data);
$sock->send($str_with_length);
And instead of reading line by line ("while (<$sock>)") I used the following code:
while (read ($sock, $buf, 4) != 0) {last;};
my $length = unpack("N", $buf);
read ($sock, $input_str, $length);
In addition, in both client and server I set to socket to binmode:
binmode $sock;
I was influenced by
IO::Socket::SIPC.
To sum it up
This is what those two programs do:
1. Client connect to server
2. Client calculate the length of string, pack it, and send to server
3. Server read the first 4 bytes of the string and unpack the length of the rest of the string
4. Server read the rest of string
5. Server process the string, pack the length into it, and send it back to client
6. Client read the first 4 bytes of the string and unpack the length of the rest of the string
7. Server read the rest of string
So, thank you for you help, and hopefully this code will help someone in the future.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.