Guidance on use of Socket

Janko Mivšek janko.mivsek at eranova.si
Fri Dec 28 18:36:03 UTC 2007


Hi John,

John Thornborrow wrote:

> I'm trying to create a simple listening service on localhost port 3456,
> I assumed this would wait for 30 seconds, then proceed but it singals
> ConnectionClosed error at the #waitForDataFor: message:
> 
> | sock |
> sock := Socket new.
> sock listOn: 3456.
> sock waitForDataFor: 30.

You need to wait for a connection with an additional method 
waitForAcceptFor: So your code should be something like:

  sock := Socket new.
  sock listOn: 3456.
  connSock := sock waitForAcceptFor: 100. "100s"
  connSock notNil ifTrue: [connSock waitForDataFor: 30].

This will trigger once. If you want more, you need to put above in 
loops. Something like:

  sock := Socket new.
  sock listOn: 3456.
  [true] whileTrue:
     [connSock isNil whileTrue:
	[connSock := sock sock waitForAcceptFor: 100].
     data := connSock waitForDataFor: 30.
     "process here that data"]
     connSocket close].

But if you want simpler sockets, I recommend you to use SpSocket from 
Sport portability library. Look for it on SqueakMap, Universes or 
SqueakSource.

I hope this helps a bit.

Janko


-- 
Janko Mivšek
AIDA/Web
Smalltalk Web Application Server
http://www.aidaweb.si



More information about the Squeak-dev mailing list