D6 Socket2

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

D6 Socket2

Glenn Swanlund-3
I'm trying to create a simple web server using D6 Socket2 and I'm
having little success. I would like to have one server serving only one
port.

So far I've found that ServerSocket2 doesn't always seem to respond to
browser requests even though I have forked processes for each
connection attempt (similar to HTTPServer). When a instance of Socket2
is created I'm unclear as to how to manage the socket. How should the
socket be monitored for new data (have got some results from
socketReadStream hasInput)? When should the socket be closed?

Can anyone provide skeleton code on how to proceed?

Regards,
Glenn


Reply | Threaded
Open this post in threaded view
|

Re: D6 Socket2

Chris Uppal-3
Glenn,

> I'm trying to create a simple web server using D6 Socket2 and I'm
> having little success. I would like to have one server serving only one
> port.

I don't know whether this will help any, you may have got beyond this stage
already and be hitting some other problem.  But here is workspace code for a
simple, non-robust, and not too standards-compliant, HTTP 1.0 server.

If I wanted to make it multi-threaded then I might fork a new Process whenever
ServerSocket>>accept: returned, and service that request in that thread.  More
likely I would (for a real application) use some sort of limiting/pooling to
ensure that not too many threads were created (so newly arrived requests might
have to wait for a worker thread to become free).

If I were attempting to squeeze the last possible ounce of performance out of
the thing (but /why/ ?) I might use the low-level asynchronous socket API[*],
and use only a couple of threads.  Note that the asynchronous interface in the
original Sockets package is /not/ (at all) related to the real low-level async
stuff, and is relatively slow.

([*] I'd have to create wrappers for them first, and somehow integrate them
into Sockets2.)

    -- chris

=============

"open the port where we will listen for incoming connections"
serverSocket := ServerSocket2 port: 8765.

"start a thread which will loop waiting for requests, and serving
each one as it arrives.  NB: the loop is single-threaded"
[
 [| clientSocket in out line |

 "wait for the next incoming requiest.
 This will fail with a SocketError when the server socket is closed"
 clientSocket := serverSocket accept.

 "handle the request from the client"
 in := clientSocket readStream.
 in isText: true.
 Transcript display: '===== Request: ======'; cr.
 [(line := in nextLine) notEmpty] whileTrue:
  [Transcript nextPutAll: line; cr].
 Transcript display: '==========='; cr.
 out := clientSocket writeStream.
 out
  isText: true;
  display: 'HTTP 1.0 200';
  cr; cr;
  nextPutAll: '<html><p>The time is <strong>';
  print: Date today; space; print: Time now;
  nextPutAll: '</strong></p></html>';
  close. "will also close clientSocket"
 ] repeat.
] fork.


"now attempt a few HTTP requests to:
    http://127.0.0.1:8765/index.html
you should see the requests in the Transcript"

"lastly, close the server socket, which will terminate (with predjuduce)
the server loop"
serverSocket close.

================