Sample code for 30+ languages & platforms
Tcl

Bind and Listen for Incoming Connections

See more Socket/SSL/TLS Examples

Demonstrates Socket.BindAndListen, which binds a TCP listener to a port and places it into listening mode, then accepts an incoming connection.

Background. The backlog argument bounds the queue of pending connections. When ClientIpAddress is empty, the listener binds to all interfaces. After listening, call AcceptNext to accept connections.

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

set socket [new_CkSocket]

#  Bind a TCP listener to a port and place it into listening mode.  The 2nd argument is the backlog,
#  the maximum number of pending connections allowed to queue.
set success [CkSocket_BindAndListen $socket 5000 25]
if {$success == 0} then {
    puts [CkSocket_lastErrorText $socket]
    delete_CkSocket $socket
    exit
}

#  After the listener is established, accept the next incoming connection into a new Socket.
set connectedSock [new_CkSocket]

set success [CkSocket_AcceptNext $socket 20000 $connectedSock]
if {$success == 0} then {
    puts [CkSocket_lastErrorText $socket]
    delete_CkSocket $socket
    delete_CkSocket $connectedSock
    exit
}

puts "Accepted a connection."

delete_CkSocket $socket
delete_CkSocket $connectedSock