Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Sockets! Controlling Receiver part

by ali_kerem (Acolyte)
on Jun 23, 2013 at 12:41 UTC ( #1040323=perlquestion: print w/ replies, xml ) Need Help??
ali_kerem has asked for the wisdom of the Perl Monks concerning the following question:

Hello All,

I have a question about socket programming which is about controlling the data through Socket->Recv.

I have a script that sends and receives commands to system RCV from system SND according a protocol which uses hexadecimal codes. I send a command to RCV and according to its meaning it does its job and reply to SND with one or more commands, then it continues until SND sends deactivate command. Each message has a head and tail obviously. So when I receive a message I convert into hex to understand it, like;

... my $Sock= new IO::Socket::INET ( PeerAddr => $Dest # Open connection to destination ,PeerPort => $Port # Open connection through port ,Proto => 'tcp' # Open connection as TCP #,Blocking => 0 # Connection will be blocking m +ode or not. 0 is non-blocking ) or die "Could not open socket :$!"; ... $Sock->recv($DataToRecv,4096); ;; # Convert into hex string my $responseMSG = unpack 'H*', $DataToRecv; ...

The problem is, sometimes SND sends a long command and RCV replies many long commands and it doesnt fit in my defined length. So lets say it gets 10 messages with head and tail but it loses 11th tail and the next time it receives a message which continues 11th message, meaning no head but body and tail...

When I increase data length, its not 11th message but 21th message this time. So there is no limitations, sometimes I see three messages without proper head.

So due to this problem I want to control receiver part with head and tail packets, but I don't know how. Because I convert it to meaningful hex part after I receive it, not while receiving it...

Pleease guide me or at least a hint, thanks.

Comment on Sockets! Controlling Receiver part
Download Code
Re: Sockets! Controlling Receiver part
by flexvault (Parson) on Jun 23, 2013 at 16:08 UTC

    ali_kerem,

    Since you don't show the 'open', I'll guess your using 'UDP'. If so, you are responsible for putting the packets back together. If your origin packet is 10K in size, each router you pass through will break up the packet based upon it's 'maximum packet size', etc.

    So use 'TCP' and let the system dis/assemble the raw packets so you can concentrate on the Perl script.

    If you are using 'TCP' now, you may have hardware problems.

    Good Luck...Ed

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

      Thanks for the reply but it's TCP and I updated the code part.I changed 4096 to 8192 and now it works longer but still, it breaks after longer responses.

Re: Sockets! Controlling Receiver part
by flexvault (Parson) on Jun 25, 2013 at 09:36 UTC

    ali_kerem,

    I asked these 2 questions on PM and showed some code. Question 1 and Question 2.

    Other monks had interesting comments and code samples. Maybe some of the comments or samples will set you on the correct course.

    I finally used a technique where I packed the length into the first 4 bytes of the message and then did a 'recv' for that exact amount of data. I tested the technique up to about 64K without losing any information, but I didn't use partial reads.

    The application was a key/value database and I wanted to share the core engine between multiple clients. The final solution had the clients(my code) save the value into the database and then send the key to the DB engine. Performance was very good and worked great in multi-core environment. The client good could save a 4GB blob while the DB engine provided indexing for the key.

    Look at the comments about 'binmode' which may save you the conversion to/from hex.

    Good Luck and Regards...Ed

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

      Thanks for the reply.

      I started to test it out, I'll try to implement it on the working code if my tests succeed.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1040323]
Approved by Happy-the-monk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2014-11-29 08:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (204 votes), past polls