[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 2.0.0.17 (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
list.
There are equivalents in the Sockets package (TCP up to 3.0.x).
The simplest example is as follows, from packages/sockets/Tests.st
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 |
Transcript
cr;
show: 'starting send test';
cr.
sock := Sockets.Socket remote: host port: 9.
Transcript
show: 'connection established';
cr.
bytesToSend := 5000000.
sendBuf := String new: 4000 withAll: $x.
bytesSent := 0.
t := Time millisecondsToRun:
[[bytesSent < bytesToSend] whileTrue:
[sock
nextPutAll: sendBuf;
flush.
bytesSent := bytesSent + sendBuf size]].
Transcript
show: 'closing connection';
cr.
sock close.
Transcript
show: 'send test done; time = ' , (t / 1000.0) printString,
' seconds';
cr;
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).
HTH,
Paolo
- [Help-smalltalk] Re: Squeak SocketStream equivalent,
Paolo Bonzini <=