activeperl@listserv.ActiveState.com
[Top] [All Lists]

Re: Is there a standard for sending and receiving between applications u

Subject: Re: Is there a standard for sending and receiving between applications using sockets?
From: "$Bill Luebkert"
Date: Sat, 11 Dec 2004 19:22:40 -0800
John V. Pataki wrote:

>  
> Intro...
>  
> I am writing sort of a bridge out of an API for our software - I managed to
> get some help on the JAVA side (our API is JAVA) to put together a small app
> that will listen to and send messages through a socket. So far it works
> great - I wrote a little test app in Tk to emulate sending and receiving
> messages betweeen the app - I am realizing how nice it is to work with
> sockets - I love 'em! The are fast!!!  - they don't get stuck or seem to
> lock up the machine like reading and writing to files - they work across the
> network - they are non-platform specific. 
> 
> Here is my code so far - this works - questions follow the code ...
> 
> ----------------------begin test_socket_client.pl -----------------------
> 
> $sock = IO::Socket::INET-> new(PeerAddr => "$host:$port"); 
> die "Cannot connect" unless defined $sock;
> 
> $sel = IO::Select-> new;
> $sel-> add($sock);
> 
> &build_gui; # I left out this routine - not relavent here 
> 
> $mw-> repeat(50 => \&read_sock);
>  
> sub write_sock {
>       
>   $text->insert('end',"Client: @_\n");
>   syswrite $sock, "@_\n";
> 
> } # end sub 
>  
> sub read_sock {
> 
>     my $line;
>     my $buf;
> 
>     my(@ready) = $sel-> can_read(0);
>     return if $#ready == -1;
>     my $hand = $ready[0];
> 
>     sysread $hand, $buf, 80;
>     $line .= $buf;
>     $text->insert('end',"Server: $line\n");
> 
> } # end sub
> 
> 
> ---------------------- end test_socket_client.pl -----------------------
> 
> Questions:
> 
> 
> Since I am on Win32 and fileevent does not work reliably I must use the
> repeat every 50 milliseconds approach from Tk ... 
> 
> As I test this I can see that if the code on the other spits out a ton of
> data - it all ends up in my text widget - with the newlines in the correct
> place. But I am not sure if it is getting written line by line - or in
> chunks of arbitray size of what is was able to read in that burst it read
> ... or in chunks of exactly 80 characters? 

TCP/IP is stream oriented - not record oriented.  It is a buffered stream
though and you may or may not want to turn buffering off.

> Also - how can tell when the data stream is over for the current message? 
> Is there a built in EOF-like check I can make? 

You could put a message header on each message that contains say a msg hdr
code and a length word.  The receiver can then tell how much data is going
to be part of the msg and use the code to sync back up in case of a failure
or just to verify if everything is in sync and where the length word is.

> What I really want is to check the socket for a message - then - be able to
> read until the message is over - but to know when the message is really over
> and not just delayed due to network traffic of if the sending application is
> slow or something. And .. If it reads X characters on this call .. and then
> it ends because at that moment there is nothing there - how will I know it
> is really over .. and another call 50 milliseconds later is supposed to be a
> continuation or is it really a new message all together sent not by request
> by me - but because the other end initiated it?

You don't want to keep reading a socket after your select says there is no
data or a read returns less bytes than what you ask for unless you only have
that one circuit to deal with and don't care if your task gets blocked on a
read.

> So - are there already known standards for indicating begin this message,
> end message here --- or I am going to send you a message and it is X
> characters or lines long?

You could look at a UDP header structure for an example of how records
are handled vs streams.

> Before I sit down and create my own set of standards for this - it would be
> nice to know if one already exists or if it is even built in.
> 
> Also - 
> 
> 1) a standard for sending a call to a function on the other end 
> 2) a standard for handling the format for output from the function
> 3) a standard for handling error messages from the function just called - is
> there some parallel handle to check for errors? Like STDERR? 

The only thing I would suggest about the above questions is that if the
data is fairly short coming back, that you return everything in ASCII
rather than binary so there are no platform specific problems dealing
with integers and floats etc.

> One thought I had was to open socket just for handling the function call
> messages & limits & arguments, one socket for data chunks (multi-line) to go
> back and forth and another for error messages - but this seems like a waste
> of resources - standard seems like a better approach.

You should be able to use msg hdrs to handle that.  Example:

message start :
        MSG1    (or whatever you like - could be binary also, the 1
                could be the message type or you could use another field)

message length :
        1024    (ASCII - again is easier for cross-platform, but could be
                 binary using network byte order [MS byte first] - make sure
                it's obvious if the length doesn't include the header length)

-- 
  ,-/-  __      _  _         $Bill Luebkert    Mailto:dbecoll@xxxxxxxxxxxx
 (_/   /  )    // //       DBE Collectibles    Mailto:dbe@xxxxxxxxx
  / ) /--<  o // //      Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_    http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
ActivePerl mailing list
ActivePerl@xxxxxxxxxxxxxxxxxxxxxxxx
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

<Prev in Thread] Current Thread [Next in Thread>