On Sat, 15 Jan 2011, Adrien BARREAU wrote:
>
> Hi all,
>
> I'm trying to use the Sockets in Pharo but I don't understand some points.
> I tried that piece of code:
>
> | s c rcv |
> Socket initializeNetwork.
> rcv := String new: 400.
> s := Socket new.
> s listenOn: 12345.
The problem is here. You should use #listenOn:backlogSize: if you want to
accept (multiple) connections. #listenOn: is a "special" method that uses
the same socket for listening and receiveing data. See the method comments
for details. Here's a small example how to use #listenOn:. Copy it to a
workspace and print it:
server := Socket new.
semaphore := Semaphore new.
[ [
server listenOn: 12345.
server semaphore wait. "wait for the client to connect"
server waitForData.
data := server receiveData.
semaphore signal ]
ensure: [ server closeAndDestroy ] ] fork.
client := Socket new.
client connectTo: #[127 0 0 1] port: 12345.
client sendData: 'Hello!'.
client closeAndDestroy.
data
Levente
> c := s waitForAcceptFor: 30.
> c dataAvailable ifTrue: [ c receiveDataInto: rcv ] ifFalse: [ self halt ].
>
> waitForAcceptFor: waits a connection, so I opened a telnet client on localhost:12345. It works, but the "accept" primitive always failed.
> I tried to find some code where it's used: ConnectionQueue>>listenLoop.
> I assume that method works (but it has no sender), and I don't understand what is different between it and my code.
>
> Perhaps I don't try to use the Sockets the good way?
>
> Adrien.
>