Socket listenOn: 0 & accept

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

Socket listenOn: 0 & accept

Sven Van Caekenberghe
Most socket API's allow for the creation of a server socket on the next available port, often by specifying 0 instead of a port. When the socket is bound, one can retrieve the local port and let the client(s) know. I tried to do that in Pharo today, and these steps seem to work, by accepting an incoming connection gives a primitive failed.

Anyone tried this ?

| socket |
socket := Socket newTCP.
socket listenOn: 0.
[ [
        Transcript crShow: 'Port is ', socket localPort printString.
        (socket waitForAcceptFor: 60)
                ifNotNil: [ :client | | data |
                        data := client receiveDataTimeout: 30.
                        Transcript crShow: 'Received ', data asString.
                        data ifNotNil: [ client sendData: data reverse; close ]
                        ].
        ] ensure: [ socket close ] ] fork.

I am running the Pharo Cog VM on Mac OS X using Pharo 1.4.

Thx,

Sven


smime.p7s (5K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Socket listenOn: 0 & accept

Philippe Marschall-2
On 03/09/2012 04:44 PM, Sven Van Caekenberghe wrote:
> Most socket API's allow for the creation of a server socket on the next available port, often by specifying 0 instead of a port. When the socket is bound, one can retrieve the local port and let the client(s) know. I tried to do that in Pharo today, and these steps seem to work, by accepting an incoming connection gives a primitive failed.

Stupid n00b question, isn't 0 a valid port number?

Cheers
Philippe



Reply | Threaded
Open this post in threaded view
|

Re: Socket listenOn: 0 & accept

Sven Van Caekenberghe

On 09 Mar 2012, at 16:56, Philippe Marschall wrote:

> On 03/09/2012 04:44 PM, Sven Van Caekenberghe wrote:
>> Most socket API's allow for the creation of a server socket on the next available port, often by specifying 0 instead of a port. When the socket is bound, one can retrieve the local port and let the client(s) know. I tried to do that in Pharo today, and these steps seem to work, by accepting an incoming connection gives a primitive failed.
>
> Stupid n00b question, isn't 0 a valid port number?

I don't think so, in Java:

http://docs.oracle.com/javase/1.4.2/docs/api/java/net/ServerSocket.html#ServerSocket()

ServerSocket

public ServerSocket (int port) throws IOException

Creates a server socket, bound to the specified port. A port of 0 creates a socket on any free port.

[…]

smime.p7s (5K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Socket listenOn: 0 & accept

NorbertHartl

Am 09.03.2012 um 18:44 schrieb Sven Van Caekenberghe:

>
> On 09 Mar 2012, at 16:56, Philippe Marschall wrote:
>
>> On 03/09/2012 04:44 PM, Sven Van Caekenberghe wrote:
>>> Most socket API's allow for the creation of a server socket on the next available port, often by specifying 0 instead of a port. When the socket is bound, one can retrieve the local port and let the client(s) know. I tried to do that in Pharo today, and these steps seem to work, by accepting an incoming connection gives a primitive failed.
>>
>> Stupid n00b question, isn't 0 a valid port number?
>
> I don't think so, in Java:
>
> http://docs.oracle.com/javase/1.4.2/docs/api/java/net/ServerSocket.html#ServerSocket()
>
> ServerSocket
>
> public ServerSocket (int port) throws IOException
>
> Creates a server socket, bound to the specified port. A port of 0 creates a socket on any free port.
>
Well, I think it is sort of a definition thing. From the protocol perspective 0 is a valid port number in the priviledged segment (0-1023). But reservation of services is done via IANA and there port 0 is "reserved" for UDP and TCP. For your own services you usually wouldn't choose a reserved port numbe like 80,22,etc. That makes 0 kind of free for intermediate use :)

Norbert


Reply | Threaded
Open this post in threaded view
|

Re: Socket listenOn: 0 & accept

Sven Van Caekenberghe
In reply to this post by Sven Van Caekenberghe
Can anyone please help me and show me where I should look if I wanted to see the exact C code implementing the socket primitives (for the Pharo built Cog VMs) ?

On 09 Mar 2012, at 16:44, Sven Van Caekenberghe wrote:

> Most socket API's allow for the creation of a server socket on the next available port, often by specifying 0 instead of a port. When the socket is bound, one can retrieve the local port and let the client(s) know. I tried to do that in Pharo today, and these steps seem to work, by accepting an incoming connection gives a primitive failed.
>
> Anyone tried this ?
>
> | socket |
> socket := Socket newTCP.
> socket listenOn: 0.
> [ [
> Transcript crShow: 'Port is ', socket localPort printString.
> (socket waitForAcceptFor: 60)
> ifNotNil: [ :client | | data |
> data := client receiveDataTimeout: 30.
> Transcript crShow: 'Received ', data asString.
> data ifNotNil: [ client sendData: data reverse; close ]
> ].
> ] ensure: [ socket close ] ] fork.
>
> I am running the Pharo Cog VM on Mac OS X using Pharo 1.4.
>
> Thx,
>
> Sven
>


smime.p7s (5K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Socket listenOn: 0 & accept

Eliot Miranda-2


On Tue, Mar 13, 2012 at 4:43 AM, Sven Van Caekenberghe <[hidden email]> wrote:
Can anyone please help me and show me where I should look if I wanted to see the exact C code implementing the socket primitives (for the Pharo built Cog VMs) ?

In trunk (and in my Cog vm source) they are in the directories

<a href="http://squeakvm.org/svn/squeak/trunk/platforms/{Cross,Mac">http://squeakvm.org/svn/squeak/trunk/platforms/{Cross,Mac OS,unix,win32}/plugins/SocketPlugin

i.e. in all VMs locate the relevant platforms hierarchy and look in platforms/{Cross,Mac OS,unix,win32}/plugins/SocketPlugin.


On 09 Mar 2012, at 16:44, Sven Van Caekenberghe wrote:

> Most socket API's allow for the creation of a server socket on the next available port, often by specifying 0 instead of a port. When the socket is bound, one can retrieve the local port and let the client(s) know. I tried to do that in Pharo today, and these steps seem to work, by accepting an incoming connection gives a primitive failed.
>
> Anyone tried this ?
>
> | socket |
> socket := Socket newTCP.
> socket listenOn: 0.
> [ [
>       Transcript crShow: 'Port is ', socket localPort printString.
>       (socket waitForAcceptFor: 60)
>               ifNotNil: [ :client | | data |
>                       data := client receiveDataTimeout: 30.
>                       Transcript crShow: 'Received ', data asString.
>                       data ifNotNil: [ client sendData: data reverse; close ]
>                       ].
>       ] ensure: [ socket close ] ] fork.
>
> I am running the Pharo Cog VM on Mac OS X using Pharo 1.4.
>
> Thx,
>
> Sven
>




--
best,
Eliot

Reply | Threaded
Open this post in threaded view
|

Re: Socket listenOn: 0 & accept

Sven Van Caekenberghe

On 15 Mar 2012, at 18:58, Eliot Miranda wrote:

> In trunk (and in my Cog vm source) they are in the directories
>
> http://squeakvm.org/svn/squeak/trunk/platforms/{Cross,Mac OS,unix,win32}/plugins/SocketPlugin
>
> i.e. in all VMs locate the relevant platforms hierarchy and look in platforms/{Cross,Mac OS,unix,win32}/plugins/SocketPlugin.

Thank you, Eliot.

Sven

> On 09 Mar 2012, at 16:44, Sven Van Caekenberghe wrote:
>
> > Most socket API's allow for the creation of a server socket on the next available port, often by specifying 0 instead of a port. When the socket is bound, one can retrieve the local port and let the client(s) know. I tried to do that in Pharo today, and these steps seem to work, by accepting an incoming connection gives a primitive failed.
> >
> > Anyone tried this ?
> >
> > | socket |
> > socket := Socket newTCP.
> > socket listenOn: 0.
> > [ [
> >       Transcript crShow: 'Port is ', socket localPort printString.
> >       (socket waitForAcceptFor: 60)
> >               ifNotNil: [ :client | | data |
> >                       data := client receiveDataTimeout: 30.
> >                       Transcript crShow: 'Received ', data asString.
> >                       data ifNotNil: [ client sendData: data reverse; close ]
> >                       ].
> >       ] ensure: [ socket close ] ] fork.
> >
> > I am running the Pharo Cog VM on Mac OS X using Pharo 1.4.
> >
> > Thx,
> >
> > Sven
> >
>
>
>
>
>


smime.p7s (5K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Socket listenOn: 0 & accept

drush66
In reply to this post by Sven Van Caekenberghe
Sven Van Caekenberghe wrote
Most socket API's allow for the creation of a server socket on the next available port, often by specifying 0 instead of a port. When the socket is bound, one can retrieve the local port and let the client(s) know. I tried to do that in Pharo today, and these steps seem to work, by accepting an incoming connection gives a primitive failed.

socket listenOn: 0.
I would say that:

socket listen.

would be nicer interface than giving special meaning to number 0 in this context.

Davorin Rusevljan