Problems with sockets

David Angier dangier at dcs-ltd.co.uk
Tue Jan 12 14:13:58 UTC 1999


I am trying to write a TCP/IP server using Squeak.  The connections are
long term, and the bi-directional
communications are asynchronous.  The eventual server will run in a
HEADLESS mode.

I have two problems with Sockets as implemented.

1.  There is only one semaphore in the socket for all actions.  I intend
to have two processes per active socket,
one to read and one to write, since the server must be able to start
processing a new request, while a previous
response is still being sent.  I have managed to bodge it as follows:

watchInput
	| gotData |
	[ true ] whileTrue: [
		gotData _ socket waitForDataUntil: (Socket deadlineSecs:
20).
		gotData ifFalse: [
			socket isConnected ifFalse: [
				self shutdownFrom: #input.
			]]
			ifTrue: [
				self processIn: socket getData.
			]
	]

and:

watchOutput
	| dataToSend |
	[ true ] whileTrue: [
		dataSemaphore wait.
		critSemaphore critical: [dataToSend _ buffer contents.
buffer _ nil ].
		[ socket waitForSendDoneUntil: (Socket deadlineSecs: 1)
] whileFalse: [
			 socket isConnected ifFalse: [ self
shutdownFrom: #output. ]
		].
		socket sendData: dataToSend.
		socket dataAvailable. "BODGE"
	]

Can anyone see any problems with this approach - and is there a better
way??

I would much prefer two semaphores!

2.  When a socket is open the processor load both under Windows and
Linux goes through the roof. Looking at the Unix source, I see that the
on dataToSend, the asynch code sets up the semaphore if the socket is
either readable
or writable.  Under Linux the select function call selects the socket
immediately, since write isn't blocked, and the
waitForDataUntil code loops as fast as it can.

I've patched my Linux copy to setup asynch io under dataAvailable as
AIO_RD anstead of AIO_RW, will I regret
doing this?

I haven't looked at the Windows source, and I probably wouldn't
understand it if I did!

TIA,

David Angier





More information about the Squeak-dev mailing list