04-27-2017, 22:36 +0200
Forum: Overview#tclcode snippets
Simple Server and Client socket in plain TCL
AuthorPost
thommey  09-17-2006, 22:34   | [TCL] Socket examples Jabber
(Administrator)
Member since 03/2004
62 Posts
A s[ai]mple server socket                                                                                                                               
01
02
03
04
05
06
07
08
09
10
11
12
13
14
set listenport 8749
socket -server accept_connection $listenport
proc accept_connection {cid ip port} {
    puts "Incoming Connection from $ip:$port"
    fconfigure $cid -buffering line
    fileevent $cid readable "readdata $cid"
}
proc readdata {cid} {
    if {[gets $cid data] < 0} {
        puts "Socket closed by remote host."; close $cid; return
    }
    puts "Incoming data: $data"
}


This post was edited 3 times, last on 09-07-2009, 20:49 by thommey
thommey  09-25-2006, 22:10   Jabber
(Administrator)
Member since 03/2004
62 Posts
Simple Client-Socket (connection to an IRC-Server here)                                                                                                                               
01
02
03
04
05
06
07
08
09
10
11
12
13
14
proc readdata {cid} {
    if {[gets $cid data] < 0} {
        puts "Socket closed by remote host."; close $cid; return
    }
    puts "Incoming data: $data"
    if {[lindex [split $data] 0] == "PING"} {
        puts $cid "PONG [lindex [split $data] 1]"
    }
}
set sid [socket irc.quakenet.org 6667]
fconfigure $sid -buffering line
fileevent $sid readable "readdata $sid"
puts $sid "NICK hauth"
puts $sid "USER hauth * * :hauth"

This post was edited 1 times, last on 09-07-2009, 20:51 by thommey
Advanced options for this topic:

Ignore this topic (Do not list this topic in the "unread topics" search. You are currently not ignoring this topic.)
Hide this topic (Hidden topics are not displayed in the topics list. This topic is currently not hidden.)
Go to forum

Unclassified NewsBoard 1.5.3-d | © 2003-4 by Yves Goergen