in reply to Re^3: how to control segmented messages in TCP chat client
in thread how to control segmented messages in TCP chat client

Hello flexvault,

Thank you for your time and effort reading and replying to my question/commends. You are absolutely right about it, as I said this is a brief idea that needs experimentation to become generic and more solid.

My assumption was build upon the user reads the length of the string (e.g. 40 characters) creates a string for transmission containing this number.

For example $msg = $length . " " . $txt;. So when the receiver will get the message he knows that he needs to subtract the $length from the length($msg); in order to get the pure message which would be something like 40 character + 3 (length description with the space) = 43 characters. So if the user skips the first 3 characters of the message and reads the rest 40 can know that he got the whole message. Even in case the message was segmented.

Well this is only a brief idea that I was planning to implement.

Sample of code of my idea by using substr including the output:

#!/usr/bin/perl use strict; use warnings; my $msg = "This is just a sample of message."; my $length = length($msg); my $number = length($length); print "\$number: ".$number."\n"; my $transmit = $length . " " . $msg; print "\$transmit: ".$transmit."\n"; my $receive = substr $transmit, 3; print "\$receive: ".$receive."\n"; __END__ $number: 2 $transmit: 33 This is just a sample of message. $receive: This is just a sample of message.

The solution as it is right now, can not be generic, because the receiver needs to know how many digits need to subtract from the $msg. In case that someone wants to make a generic solution, I would recommend the user to read the string and get the actual length before subtracting. As I said, it needs some small experimentation to become generic and more solid solution, but I think it is possible as an idea to be implemented.

Again thank you for your time and effort reading and replying to my query.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^5: how to control segmented messages in TCP chat client
by flexvault (Monsignor) on Aug 26, 2014 at 14:09 UTC

    thanos1983,

    Take a look at this, especially the code that BrowserUk provides. This may help you understand the problem better. When reading from the socket you need first to know the size of the message. 'pack' with the 'N' parameter works great for this and then read the message. It requires 2 reads.

    Regards...Ed

    "Well done is better than well said." - Benjamin Franklin