我在itcl中创建了这个客户端程序,以便与python服务器进行交互。我的问题是,当我运行此客户端时,它有时会从服务器接收响应,有时不会。虽然没有收到回复,即使它没有产生任何错误。服务器端没有错误。
创建套接字有什么错误吗?请帮我。
package require itcl
package require json
package require ncgi
namespace import ::itcl::*
class StaffSocketClient123 {
variable clientSocket
constructor {port} {
set host localhost
set clientSocket [socket $host $port]
fconfigure $clientSocket -buffering line
}
method send_message {message} {
#fconfigure $clientSocket -buffering line
set jsonMessage [json::dict2json $message]
set stringMessage [format "%s" $jsonMessage]
puts "request is: $stringMessage"
puts $clientSocket $stringMessage
set replyFromServer [ncgi::decode [gets $clientSocket]]
puts " Response from server : $replyFromServer"
#set intermediateReply $replyFromServer
set resultInDict [json::json2dict $replyFromServer]
puts "from server in dict format: $resultInDict"
return $resultInDict
}
method create_component {varrequest_type varcommand varcomponent_type varhost varrequest_timeout varport} {
set message [dict create request_type $varrequest_type command $varcommand params [dict create component_type $varcomponent_type host $varhost request_timeout $varrequest_timeout port $varport]]
puts "request in dictionary format: $message"
set response [send_message $message]
set component_id [dict get $response component_id]
#puts "component id: $component_id"
return $component_id
}
method perform_action {varrequest_type varcommand varcomponent_id varurl} {
set message [dict create request_type $varrequest_type command $varcommand params [dict create component_id $varcomponent_id url $varurl]]
puts "Action message is: $message"
set response [send_message $message]
return $response
}
}
StaffSocketClient123 ssc 9999
set component_id "[ssc create_component action create_component WebComponent localhost 5 9999]"
答案 0 :(得分:1)
尝试以不同方式放置或配置缓冲后刷新客户端。所以:
puts $sock "message"
flush $sock