Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

socket -server Server xxxx Behavior

0 views
Skip to first unread message

Joe Simon

unread,
Aug 30, 2004, 5:11:00 PM8/30/04
to

I am VERY new to tcl (like I started playing with it this morning), so
please bear with me.


For a call to socket for a server like the following :

socket -server Server 21000

Create a new thread or process for the Server procedure ?


When I create multiple clients, I get the messages "puts $line" in order
for all of client one, and then client 2, etc. See the output below. If
I start all of the clients together, the first messages come out every 2
seconds (approx) and after the first client ends the subsequent clients'
messages spew out with no delays, which makes it appear to me that their
might be separate threads running but their IO is not interleaved.

Any Ideas ?

Thanks !
Joe Simon


Server code :

proc Server {channel clientaddr clientport } {
puts "Connection from $clientaddr port $clientport registered"
set line "test"
while { $line != "Done" } {
gets $channel line
puts $line
}
}

puts "Waiting For socket Connections"
socket -server Server 9902
vwait forever

Client Code :

set server localhost
set line "Test"
set count 0
set sockChan [socket $server 9902]
while { $line != "Done" && $count<10 } {
puts $sockChan "You Stink !! --> $count"
flush $sockChan
incr count
after 2000
}
puts $sockChan "Test"
puts $sockChan "Done"
close $sockChan


Output with multiple clients running is : (I expected the messages to be
interleaved).

Waiting For socket Connections
Connection from 127.0.0.1 port 1444 registered
You Stink !! --> 0
You Stink !! --> 1
You Stink !! --> 2
You Stink !! --> 3
You Stink !! --> 4
You Stink !! --> 5
You Stink !! --> 6
You Stink !! --> 7
You Stink !! --> 8
You Stink !! --> 9
Test
Done
Connection from 127.0.0.1 port 1445 registered
You Stink !! --> 0
You Stink !! --> 1
You Stink !! --> 2
You Stink !! --> 3
You Stink !! --> 4
You Stink !! --> 5
You Stink !! --> 6
You Stink !! --> 7
You Stink !! --> 8
You Stink !! --> 9
Test
Done
Connection from 127.0.0.1 port 1446 registered
You Stink !! --> 0
You Stink !! --> 1
You Stink !! --> 2
You Stink !! --> 3
You Stink !! --> 4
You Stink !! --> 5
You Stink !! --> 6
You Stink !! --> 7
You Stink !! --> 8
You Stink !! --> 9
Test
Done


--
Joe Simon
Owego, NY
Remove .NOSPAM to send e-mail


Michael Schlenker

unread,
Aug 30, 2004, 5:41:04 PM8/30/04
to
Joe Simon wrote:

> I am VERY new to tcl (like I started playing with it this morning), so
> please bear with me.
>
>
> For a call to socket for a server like the following :
>
> socket -server Server 21000
>
> Create a new thread or process for the Server procedure ?

No, Tcl has a nice feature called an "Event Loop" that is far better for
this kind of things in most situations.


>
>
> When I create multiple clients, I get the messages "puts $line" in order
> for all of client one, and then client 2, etc. See the output below. If
> I start all of the clients together, the first messages come out every 2
> seconds (approx) and after the first client ends the subsequent clients'
> messages spew out with no delays, which makes it appear to me that their
> might be separate threads running but their IO is not interleaved.

Your code just does blocking I/O. You have to look up the documentation
for the commands "socket", "fconfigure", "fileevent" to create a really
responsive server.

Take a look at:
http://wiki.tcl.tk/9414
http://wiki.tcl.tk/8301
http://wiki.tcl.tk/fileevent
http://wiki.tcl.tk/socket

The code snippets in the first two links should point you in the right
direction.

Some pointers to the event loop:
http://wiki.tcl.tk/1527
http://wiki.tcl.tk/1526

Michael

Joe Simon

unread,
Aug 31, 2004, 4:11:02 PM8/31/04
to

Michael Schlenker wrote:

> <snip>

>
> Your code just does blocking I/O. You have to look up the documentation
> for the commands "socket", "fconfigure", "fileevent" to create a really
> responsive server.
>
> Take a look at:
> http://wiki.tcl.tk/9414
> http://wiki.tcl.tk/8301
> http://wiki.tcl.tk/fileevent
> http://wiki.tcl.tk/socket
>
> The code snippets in the first two links should point you in the right
> direction.
>
> Some pointers to the event loop:
> http://wiki.tcl.tk/1527
> http://wiki.tcl.tk/1526
>
> Michael

Thanks Michael,

I'll check these out.

0 new messages