[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-smalltalk] Re: Squeak SocketStream equivalent

From: Paolo Bonzini
Subject: [Help-smalltalk] Re: Squeak SocketStream equivalent
Date: Mon, 27 Oct 2008 09:02:12 +0100
User-agent: Thunderbird (Macintosh/20080914)

Stephen Woolerton wrote:
> Hi Paolo,
> I'm having a go at porting LDAPLayer from Squeak to GST. LDAPLayer uses
> "SocketStream" which (I expect you know already) is a library to make it
> easy to program to network sockets using a stream interface.
> GST doesn't have SocketStream and I'm wondering if there is already
> something equivalent before I look at copying the SocketStream library
> across to GST.

Hi Stephen, I guess you do not mind if I send the message to the mailing

There are equivalents in the Sockets package (TCP up to 3.0.x).

The simplest example is as follows, from packages/sockets/

    sendTest: host [
        "Send data to the 'discard' socket of the given host. Tests the
speed of
         one-way data transfers across the network to the given host.
Note that
         many hosts do not run a discard server."

        "Sockets.Socket sendTest: 'localhost'"

        <category: 'tests'>
        | sock bytesToSend sendBuf bytesSent t |
            show: 'starting send test';
        sock := Sockets.Socket remote: host port: 9.
            show: 'connection established';
        bytesToSend := 5000000.
        sendBuf := String new: 4000 withAll: $x.
        bytesSent := 0.
        t := Time millisecondsToRun:
                        [[bytesSent < bytesToSend] whileTrue:
                                    nextPutAll: sendBuf;
                                bytesSent := bytesSent + sendBuf size]].
            show: 'closing connection';
        sock close.
            show: 'send test done; time = ' , (t / 1000.0) printString,
' seconds';
            show: (bytesToSend asFloat / t) printString;
            showCr: ' kBytes/sec'

You can start a discard server using "sudo nc -l -p discard > /dev/null"
if you want to try it.

There are three classes:

- UnbufferedSocket does no buffering at all;

- StreamSocket does read buffering only;

- Socket does read and write buffering only; use #flush to send data
down the network.

Usually you want to use StreamSocket if requests are created in a byte
array and then sent to the network (common when porting from Squeak),
otherwise use Socket.  I'd use Socket only for interactive stuff,
because buffering *is* expensive: I get 56 MB/sec. throughput with
StreamSocket and 4 MB/sec. throughput with Socket.  The reason is that
Sockets were especially optimized in 3.1 to limit the number of copy
operations, and the #nextPutAll: in the above code becomes directly a
"send" with StreamSocket, with no intermediate copies.

GST's API is more Smalltalk-ish (based on Streams), while Squeak's is
more similar to BSD sockets.  The equivalent of Squeak's receive method
is #nextAvailable:into:startingAt: (there is also #next:into:startingAt:
which loops if the requested number of bytes is not available, and
#nextAvailable:/#next: which return a new Collection; however the latter
two may stress the GC much more).



reply via email to

[Prev in Thread] Current Thread [Next in Thread]