in reply to Reading and writing sockets

Simple, develop a protocol, preferably a simple one ;D

Example, each transmission looks like:

type:length:message
so yer server can say action:0008:fly away or better yet, create a hash like
my %ACTIOn = ( '000' => 'not an action', '001' => 'jump', '002' => 'swim',....
that way, you can say "001:0008:fly away" parse your protocol by reading in the first 3 + 1 + 4 characters, doing something like my($action,$length)=split/:/ and the reading $length bytes... I hope you get the picture
 

____________________________________________________
** The Third rule of perl club is a statement of fact: pod is sexy.

Replies are listed 'Best First'.
Re: (podmaster) Re: Reading and writing sockets
by Marcello (Hermit) on Dec 11, 2001 at 14:27 UTC
    Hi,

    The protocol is already fixed, where I am stuck is the logic I need to use to send AND receive commands and the same time. I need to be client AND server.

    What loop do I need to create to handle both situations? Like:

    if (!read_server_command && something_to_send) { send_command read_response } else { # handle server command send_response }
    But how do I stop reading from a socket when I have to send something?

    I hope you get the picture ;-)
      The protocol is already fixed... - who "fixed" it? who designed it? why? trying to remedy design flaws is a bad way to write programs.

      I need to be client AND server. - that's possible, but first you need to clearlyl define your protocol, as I haven't a clue as to what it is(send command, get response is not specific enough). How do you distinguish a command from a response?

      But how do I stop reading from a socket when I have to send something? - as far as I know, you can't. you wait till the socket times out. That is why you're supposed to read data in chunks, like in a while loop.
      read FILEHANDLE,SCALAR,LENGTH,OFFSET
      read FILEHANDLE,SCALAR,LENGTHwhile(read(FH,$rr,1024,$off){...

      I need to know more about what the actual protocol looks like.
       

      ____________________________________________________
      ** The Third rule of perl club is a statement of fact: pod is sexy.