Stuck On Socket Interaction 
Author Message
 Stuck On Socket Interaction

I have been using sockets for a little while and have no trouble having
two way conversations where only one line is involved. However, if
 either the client or server wants to send multiple lines, the other
dosen't seem to know when (if ever) the incoming data will finish. I'm
reading and writing using print and <> (not argv) both autoflushed.

Eg:

<Client Code>
chop($in=<S>);
print S "hello\n";
chop($in=<s>);
...

<Server Code>
print C "Hello There\n";
chop($in=<C>);
print C "How's the Weather etc..\n";
...

This all works fine.
As soon as I put something like this into the conversation it hangs

<Client Code>
while (<S>) {
        $x .= $_;

Quote:
}

print S "I got it\n";

<Server Code>
print C <<MULTILINE;
here is the
stuff I am sending you
MULTILINE
chop($well=<C>)

I've tried sending a ctrl-D at the end of the string but that didn't
work. How do I tell the client to "stop waiting for info and get on with
the conversation, all has been said!". All the usual filehandle tricks
don't seem to be working (such as undefing $/).

Thanks

Matthew



Tue, 11 Jan 2000 03:00:00 GMT  
 Stuck On Socket Interaction

Quote:

> As soon as I put something like this into the conversation it hangs

> <Client Code>
> while (<S>) {
>    $x .= $_;
> }
> print S "I got it\n";

> <Server Code>
> print C <<MULTILINE;
> here is the
> stuff I am sending you
> MULTILINE
> chop($well=<C>)

> I've tried sending a ctrl-D at the end of the string but that didn't
> work. How do I tell the client to "stop waiting for info and get on with
> the conversation, all has been said!". All the usual filehandle tricks
> don't seem to be working (such as undefing $/).

Remember that <> only returns undef when it sees EOF. EOF, in the case
of a socket, means that the socket has been closed or "shutdown".

So, you ask, how do I know when my data "send" is done?! Well, this is
where you get to design your very own protocol! The simple way would
be to have some special string that you send as a line by itself (that
would otherwise not occur in your data) that tells the reader to stop
reading.

For example, in your data, you could send "\0\n", and have the other
side look for the nul:

        while(<S>) {
                last if $_ eq "\0\n";
                $x .= $_;
        }

The more complex way to go would be to develop a header/data style of
transmission where the reader would get a header that tells it how
much data to read (e.g. HTTP's Content-length). Here's a simple
example:

        # Server:
        print S length($string),"\n";
        print S $string;

        # Client:
        $len = <S>;
        read(S,$string,$len);

That will always work no matter what is in your data. The other way,
you have to know in advance that your data will not contain some
character or string.

                        -AJS



Tue, 11 Jan 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Socket interactions

2. cgi stuck in loop

3. CGI.pm forms not "sticking"

4. Sybperl: dblogin gets stuck on bad input

5. Newb stuck on simple code....

6. kid's output stuck in a TTY?!

7. STUCK WITH Macperl

8. err...stuck

9. Stuck on merging files

10. Novice perl user stuck on page 19 of the camel book

11. Simple regexp gots me stuck...

12. preambulate: stick NIH header on perl programs

 

 
Powered by phpBB® Forum Software