Groovy tcp 读取线
我最近在 groovy 中创建了一个 tcp 服务器,并且想知道如何让它的行为类似于 telnet。
例如,我运行 tcp 服务器并拉起 cmd 提示符以 telnet 脚本的端口并向其发送其查找的命令。大多数命令发送回一行/字的信息。然而,有一些会发回一个大字符串(类似于一段信息)。与 telnet 配合使用效果很好。
但是,当我创建 tcp 客户端时,我无法让它接受除第一行信息之外的任何内容。
我使用 readLine() 而不是 readLines() 因为如果我使用 readLines() 它会挂在那里并且不允许我发送下一个命令。
我还尝试了类似的方法: (psuedocode)
while((r.readLine()) !=null) {
def a = r.readLine()
}
它也像 readLines() 一样挂在那里
如果您需要查看代码的样子,请检查此处: Groovy Tcp 客户端/服务器发送映射
我的 sendMessage 函数与这些函数不同,但我将其更改为相同的,并且正在使用客户端上的函数。
编辑
作为对提供的第一个答案的回应...
很好地执行您提到的操作以及类似的操作:
while((line = r.readLine()) !=null) {
println line
}
两者都有效,但我发送命令的方式在某种程度上干扰了它的功能。
如果您在我的来源(在上面提供的链接上)查看峰值,也许您可以发现为什么它不能与这些解决方案正常工作。
I recently created a tcp server in groovy and am wondering how to get it to behave similar to telnet.
For example, I run my tcp server and pull up the cmd prompt to telnet the port of the script and send it commands that its looking for. Most of the commands send back one line/word of information. However there are a few that send back a large string (similar to a paragraph of information). It works fine with telnet.
However, when I create my tcp client, I can't get it to accept anything more than the first line of information.
I am using readLine() instead of readLines() because if I use readLines() it hangs there and doesn't allow me to send the next command.
I also tried something like: (psuedocode)
while((r.readLine()) !=null) {
def a = r.readLine()
}
Which also just hangs there like readLines()
If you need to see what the code looks like, check here: Groovy Tcp client/server sending maps
My sendMessage function is different on those, but I changed it to be the same and am using the one I have on the client.
EDIT
In response to the first answer provided...
Well doing what you mentioned as well as something like:
while((line = r.readLine()) !=null) {
println line
}
Both work, but the way I am sending commands is somehow interfering with the functionality of it.
If you take a peak at my source (on the link provided above) perhaps you can spot why it isn't working properly with those solutions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的伪代码是错误的。在每次循环迭代中,您将读取一行来检查何时结束循环,然后将另一行读取到变量 a 中。这可能就是问题所在。如果您的变量
r
是一个Reader
,请尝试如下操作:Your pseudocode is wrong. On each loop iteration, you are reading a line to check when to end the loop, then reading another line into the variable a. This might be the problem. If your variable
r
is aReader
, try something like this: