Re: TCL multiple sockets communication
- From: "slebetman@xxxxxxxxx" <slebetman@xxxxxxxxx>
- Date: 26 Mar 2006 16:12:33 -0800
Ulrich Schöbel wrote:
Am Sun, 26 Mar 2006 08:04:34 -0800 schrieb slebetman@xxxxxxxxx:
bogdan wrote:
hey!
can anybody tell me if it is possible (in tcl) for a server to open
two(or more) sockets and to talk simultaneousley with two clients on
these two sockets?...and if yes...do you know any links with such
examples?
i tried to do it like this but it dosen't work simultaneousley...
server:
#!/usr/bin/tclsh8.4
<snipped non-concurrent code>
Use fileevents my friend.
Here's an example how to:
Server code:
... server code elided
proc accept {channel client port} {
puts "Accepted connection $channel from $client"
fconfigure $channel -translation auto -blocking 0
fileevent $channel readable "server $channel"
}
socket -server accept 1234
vwait forever
Test client code:
... client code elided
In the example above the server only opens a single port. You can
easily modify it to accept multiple ports by adding another [socket
-server accept $portnumber].
Hi Bogdan,
for mor sockets to accept connections just add a server id
to the socket and accept commands:
socket -server {accept Server1} 1234
socket -server {accept Server2} 1235
socket -server {accept Server3} 1236
proc accept {srv channel client port} {
...
}
srv will contain the server id.
Server ID's are only necessary if each port does different things
(which is what the TCP/IP model intended). But from the OPs original
example I had a feeling that he wanted all opened ports to do the same
thing. Kind of like bittorrent opening multiple listening ports but
they are all bittorent anyway. For a single service running on multiple
ports you can simply do:
socket -server accept 1234
socket -server accept 1235
socket -server accept 1236
proc accept {channel client port} {
...
}
Ulrich's example is useful for doing things like:
socket -server {accept http_server} 80
socket -server {accept pop3_server} 110
socket -server {accept custom_server} 1234
proc accept {server channel client port} {
fconfigure $channel -blocking 0
switch -exact $server {
"http_server" {
fileevent $channel readable "httpProc $channel"
}
"pop3_server" {
fileevent $channel readable "pop3Proc $channel"
}
"custom_server" {
fileevent $channel readable "myServerProc $channel"
}
}
}
.
- Follow-Ups:
- Re: TCL multiple sockets communication
- From: Donal K. Fellows
- Re: TCL multiple sockets communication
- From: bogdan
- Re: TCL multiple sockets communication
- References:
- TCL multiple sockets communication
- From: bogdan
- Re: TCL multiple sockets communication
- From: slebetman@xxxxxxxxx
- Re: TCL multiple sockets communication
- From: Ulrich Schöbel
- TCL multiple sockets communication
- Prev by Date: Re: Conversing with a tcl application...
- Next by Date: Re: Conversing with a tcl application...
- Previous by thread: Re: TCL multiple sockets communication
- Next by thread: Re: TCL multiple sockets communication
- Index(es):
Relevant Pages
|