help-smalltalk
[Top][All Lists]
Advanced

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

Re: [Help-smalltalk] [Q] Any simple example or documentation on NetServe


From: Robin Redeker
Subject: Re: [Help-smalltalk] [Q] Any simple example or documentation on NetServer code?
Date: Thu, 26 Oct 2006 11:01:48 +0200
User-agent: Mutt/1.5.11+cvs20060403

On Thu, Oct 26, 2006 at 04:28:18PM +0900, Sungjin Chun wrote:
> Hi,
> 
> 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
   PackageLoader
   st> FileStream fileIn: 'SocketTest.st'!
   FileStream
   st> Smalltalk at: #x put: (SocketTest new)!
   SocketTest new "<0x2afbf7bb1ee0>"
   st> x start!
   Starting server...
   Got connection!
   ...

Connect to local port 12345:

   $ nc localhost 12345
   test
   [
   test

   ]

The code isn't very elegant, it's what i hacked up with my 5 days
smalltalk experience.

Which creates a process around the main loop of the server.

This is the code of SocketTest>>#start :
   start
      'Starting server...' displayNl.
      socket := TCP.ServerSocket port: 12345.
      socket isNil ifTrue: [ self error: 'couldn''t create ServerSocket!' ].
      [
         socket waitForConnection.
         'Got connection!' displayNl.
         self handle: (socket accept)
      ] repeat
   !

The error reporting is likely not correct. 'socket waitForConnection' blocks the
current process and waits until someone connected. Then (socket accept) will 
return
a new socket with the client connection.

Now to SocketTest>>#handle :
   handle: sock
      [
         [
            [ sock atEnd ] whileFalse: [
                buffer := buffer, (sock nextHunk).
                sock nextPutAll: '['.
                sock nl.
                sock nextPutAll: buffer.
                sock nl.
                sock nextPutAll: ']'.
                sock nl.
                sock flush
            ].
         ] ensure: [ sock close ]
      ] fork
   !

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
in tcp/Sockets.st.

cu,
Robin




reply via email to

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