读取套接字被阻止

问题描述 投票:1回答:1

我正在编写一个套接字实用程序来将客户端传送到服务器。当从客户端输入到套接字时,服务器正在接收它。但是,当从服务器输入套接字时,客户端无法读取。检查fblocked $频道时。它是1.我已经尝试了一切,包括添加新行,...请帮忙。

以下是我的代码

proc read_command { sock } {
  variable self
  global connected

  set len [gets $sock line]
  set bl  [fblocked $sock]
  puts "Characters Read: $len Fblocked: $bl"

  if {$len < 0} {
     if {$bl} {
        puts "Input is blocked"
     } else {
        set connected 1
        puts "The socket was closed - closing my end"
        close $sock
     }
  } else {
     if {!$bl} {
        puts "Read $len characters: $line"
        catch {uplevel #0 $line} output
        puts "1==>$output<=="
        puts $sock "$output"
        puts $sock "\n"
        flush $sock
     }
  }
}

proc client { host port } {
  variable self
  set s [socket $host $port]
  set self(csock) $s
  set self($s,addr) $host
  set self($s,port) $port
  fconfigure $s -buffering line -blocking 0
  return $s
}

proc prun { sock args} {
  variable self
  set result [list]

  set cmd $args
  set cmd [regsub -all {(^\s*\{)||(\}\s*$)} $cmd ""]
  set cmd [string trimleft $cmd]
  set o1 [eval $cmd]
  #catch {uplevel #0 $cmd} o1

  puts "1_$sock ==> $o1"

  lappend result $o1
  #--------------
  puts $sock $cmd
  flush $sock

  set bl [fblocked $sock]

  set file [read $sock]
  set bl [fblocked $sock]
  puts "Fblocked: $bl"
  puts "Output: $file"

  puts "2_$Comm::self(csock) ==> $file ==> $bl"

  lappend result $file
  return $result
}

这是我如何运行它。

  1. 我在终端的1上呼叫服务器。它将回显IP地址和端口。
  2. 然后我使用上面的地址和端口调用客户端来获取客户端套接字
  3. 然后我在客户端shell上调用prun以获取一对值,一个值具有客户端上cmd调用的值,另一个值是服务器上cmd调用的值。基本上我想得到这对值,所以我可以用它们来处理2组数据之间的相关性。

下面是代码:1。在服务器shell上

$ server

2.在客户端shell上

$ set s [client $addr $port]

3.调用proc从客户端shell获取值,然后将命令发送到服务器以从服务器shell获取值,并将该值返回给客户端。

$ set res [prun $s {set val [get_attribute [get_nets mynet] pin_capacitance_max]}]
tcl
1个回答
1
投票

你写了:

puts "2_$Comm::self(csock) ==> $file ==> $bl"

并用变量定义了self。你在用包吗?可能是你忘了与之相关的东西。

对于测试,您可以使用全局但使用数组会稍微复杂一些。

© www.soinside.com 2019 - 2024. All rights reserved.