I've just started on socket programming. I want to have the client as TCL and server as Python. I tried the following few scripts, but I'm unable to make any progress.
Here are my scripts:
server.py:
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((socket.gethostname(), 45000))
s.listen(5)
while True:
clt, adr=s.accept()
print(adr)
clt.send(bytes("Socket programming in Python"))
client.tcl:
set host "127.0.0.1"
set port 45000
set mysock [socket -myaddr $host -myport $port $host $port]
fconfigure $mysock -buffering none
gets $mysock line
close $mysock
puts "Message is $line"
However, the above TCL script just hangs and doesn't go beyond the gets command.
This is the equivalent Python client script which works:
client.py:
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print(s)
s.connect((socket.gethostname(), 45000))
msg=s.recv(1024)
print(msg.decode("utf-8"))
Now based on few suggestions, to mimic the above python client script in TCL, I tried the following:
client_modified.tcl:
set s [socket [info hostname] 45000]
puts $s
fconfigure $s -translation binary
puts [encoding convertfrom utf-8 [read $s 28]]
I'm still not able to proceed beyond: "socket localhost 45000".
It gives me the error:
> tclsh client.tcl
couldn't open socket: connection refused
while executing
"socket [info hostname] 45000"
invoked from within
"set s [socket [info hostname] 45000]"
Can anyone please guide here.
-myaddrand-myport(or their equivalent in other languages) is really rare. It's very unusual to set the local address of a client socket, and it adds failure modes. - Donal Fellows