[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-smalltalk] [Q] Any simple example or documentation on NetServe
Re: [Help-smalltalk] [Q] Any simple example or documentation on NetServer code?
Thu, 26 Oct 2006 11:01:48 +0200
On Thu, Oct 26, 2006 at 04:28:18PM +0900, Sungjin Chun wrote:
> I'd like to test GNU Smalltalk's performance as a Network server. My
> intention is like this: create simple echo like server and test it with
> my previous C based version. Then compare the numbers - I have test
> client in C :-).
> Thanks in advance.
I can give you the pieces of code i made up when playing with the TCP
package some days ago. The attachment of this mail contains SocketTest.st
which should be used and loaded like this:
st> PackageLoader fileInPackage: 'TCP'!
Loading package TCP
st> FileStream fileIn: 'SocketTest.st'!
st> Smalltalk at: #x put: (SocketTest new)!
SocketTest new "<0x2afbf7bb1ee0>"
st> x start!
Connect to local port 12345:
$ nc localhost 12345
The code isn't very elegant, it's what i hacked up with my 5 days
Which creates a process around the main loop of the server.
This is the code of SocketTest>>#start :
'Starting server...' displayNl.
socket := TCP.ServerSocket port: 12345.
socket isNil ifTrue: [ self error: 'couldn''t create ServerSocket!' ].
'Got connection!' displayNl.
self handle: (socket accept)
The error reporting is likely not correct. 'socket waitForConnection' blocks the
current process and waits until someone connected. Then (socket accept) will
a new socket with the client connection.
Now to SocketTest>>#handle :
[ sock atEnd ] whileFalse: [
buffer := buffer, (sock nextHunk).
sock nextPutAll: '['.
sock nextPutAll: buffer.
sock nextPutAll: ']'.
] ensure: [ sock close ]
This forks off a new process to handle the connection socket and reads
from it until EOF is seen (i don't know if 'atEnd' is the correct way to
do this here!). Also error reporting is missing, i still wonder how to do
it right :)
Note: (sock nextHunk) is blocking the process until data arrived.
I'm not sure whether the application of fork and the sockets
is correct this way. Most of the ideas i got from browsing the code
with the blox browser and looking at the existing networking code