Is this socket code correct?

Lex Spoon lex at cc.gatech.edu
Mon Nov 22 10:39:57 UTC 1999


Do you get to choose the protocol?  If so, you might look at using
SimpleClientSocket, which has handy commands for sending/receiving a
line at a time, and for sending/receiving multi-line messages that end
with a "." on a line by itself.  It will take care of all the
waitForData's, etc, for you.

Otherwise, just remember that a read might return 0 bytes, so you need
to program for it anyway.  It's not hard to do, you probably need to do
it anyway, and it asks less of the networking primitives.

Lex



Juan Cires Martinez <jcm at mat.upm.es> wrote:
> The context:
> 
> I am trying to control a mobile robot from squeak.  I wrote the code
for
> squeak 2.3 and I'm updating it for 2.6.  The mobile robot (or the
> simulator) receive commands via sockets with a command/reply type
> protocol.  This code tries to read the robot's reply after sending a
> command.  The socket does not close.  When the error notifier comes
up,
> I open the debugger and inspect the value of 'read' and find that it
is
> zero.  I can then manually do a "socket receiveDataInto: buffer" and
the
> data is read, or I can restart the method and everything continues
> working until a while later, when the error notifier comes up again.
> 
> I am connecting to a robot simulator running on the same computer as
the
> squeak system.
> 
> Thanks for your help,
> 						Juan.
> 
> >   [socket dataAvailable] whileFalse: 
> >     [(socket waitForDataUntil: Socket standardDeadline) ifFalse: 
> >        [self error: 'Timeout reading reply']].
> >   buffer _ ByteArray new: 10000.
> >   read _ socket receiveDataInto: buffer.
> >   read < 4
> >     ifTrue: [self error: 'Too few data read'].





More information about the Squeak-dev mailing list