Guidance on use of Socket

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

Guidance on use of Socket

John Thornborrow
Hello list,

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.

Could somebody please advise?

Many thanks.

John.


Pinesoft Computers are registered in England, Registered number: 2914825. Registered office: 266-268 High Street, Waltham Cross, Herts, EN8 7EA



This message has been scanned for viruses by BlackSpider MailControl - www.blackspider.com


Reply | Threaded
Open this post in threaded view
|

Re: Guidance on use of Socket

Janko Mivšek
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

Reply | Threaded
Open this post in threaded view
|

Re: Guidance on use of Socket

Janko Mivšek
And I managed to make a mistake or two in below code. But you'll
probably find it by yourself :)

Janko

Janko Mivšek wrote:

> 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

Reply | Threaded
Open this post in threaded view
|

Re: Guidance on use of Socket

johnmci

On Dec 28, 2007, at 10:44 AM, Janko Mivšek wrote:

> And I managed to make a mistake or two in below code. But you'll  
> probably find it by yourself :)



Well and a few other hints...

(a) when someone sends you say 10 bytes of data, when those 10 bytes  
of data will arrive occurs
sometime between now and the future. A classic mistake is doing a read  
and thinking you've gotten
all the data... However you might find that last byte (well frame  
actually) is wandering about in some router still...

(b) when you send data, then close the socket before checking to see  
if all the data is sent, the logic if it's
set to Socket Linger will attempt to send the data, but after 2  
seconds will abort and not actually have sent all
the data.  Usually this is *much* more apparent when you have an  
application that does a send of 64k of
data then close the socket and you are using a dialup connection.



--
=
=
=
========================================================================
John M. McIntosh <[hidden email]>
Corporate Smalltalk Consulting Ltd.  http://www.smalltalkconsulting.com
=
=
=
========================================================================



Reply | Threaded
Open this post in threaded view
|

Re: Guidance on use of Socket

John Thornborrow
Thanks, it makes sense to wait for a connection before waiting for
data..

John.

John M McIntosh wrote:

>
> On Dec 28, 2007, at 10:44 AM, Janko Mivšek wrote:
>
>> And I managed to make a mistake or two in below code. But you'll
>> probably find it by yourself :)
>
>
>
> Well and a few other hints...
>
> (a) when someone sends you say 10 bytes of data, when those 10 bytes of
> data will arrive occurs
> sometime between now and the future. A classic mistake is doing a read
> and thinking you've gotten
> all the data... However you might find that last byte (well frame
> actually) is wandering about in some router still...
>
> (b) when you send data, then close the socket before checking to see if
> all the data is sent, the logic if it's
> set to Socket Linger will attempt to send the data, but after 2 seconds
> will abort and not actually have sent all
> the data.  Usually this is *much* more apparent when you have an
> application that does a send of 64k of
> data then close the socket and you are using a dialup connection.
>
>
>
> --
> ===========================================================================
> John M. McIntosh <[hidden email]>
> Corporate Smalltalk Consulting Ltd.  http://www.smalltalkconsulting.com
> ===========================================================================
>
>
>
>


Pinesoft Computers are registered in England, Registered number: 2914825. Registered office: 266-268 High Street, Waltham Cross, Herts, EN8 7EA



This message has been scanned for viruses by BlackSpider MailControl - www.blackspider.com