Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Reading and writing sockets

by Marcello (Hermit)
on Dec 11, 2001 at 14:03 UTC ( [id://130894]=perlquestion: print w/replies, xml ) Need Help??

Marcello has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm trying to find the best way to do the following:

On a UNIX machine using sockets I am sending commands to a server. This server responds with a (positive or negative) command. Therefore I now send the command and then read the response. But occasionally the server also sends out a command, to which my application has to respond.

What is the best logic to implement this loop? Always read, until I have to send something I guess. But how can I stop a read using sockets?

I use the system call read() and IO::Socket->print() to read from and write to the socket.

TIA

Replies are listed 'Best First'.
Re: Reading and writing sockets
by gbarr (Monk) on Dec 11, 2001 at 17:59 UTC
    First off, don't use read() and print(), they are stdio calls and are therefore subject to buffering. Instead use sysread() and syswrite().

    To determine if there is anything to read use select() (see IO::Select)

(podmaster) Re: Reading and writing sockets
by PodMaster (Abbot) on Dec 11, 2001 at 14:19 UTC
    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.

      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.

Re: Reading and writing sockets
by wileykt (Acolyte) on Dec 11, 2001 at 21:37 UTC
    To be both the client and the server, set up the program as a server. This way you're always ready to accept a transmission. First you establish the connection:
    $server = IO::Socket::INET->new( Listen => 5, LocalPort => 4000, Proto => 'tcp', Reuse => 1, Type => SOCK_STREAM ) or die "Can't create server socket: $!";
    Then you wait for transmissions:
    while ($client = $server->accept()) {
    Within this while loop, you can both accept transmissions, and respond to $client with something like:
    print $client $ack;

    Hope this helps.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-04-23 11:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found