gpt4 book ai didi

c# - 无法从 TCP 服务器获得完整响应

转载 作者:可可西里 更新时间:2023-11-01 02:52:36 26 4
gpt4 key购买 nike

我正在尝试使用端口 2628 连接到 dict.org 服务器,但无法从服务器获得完整响应。代码如下所示:

TcpClient client = new TcpClient("216.18.20.172", 2628);
try
{
Stream s = client.GetStream();
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
sw.AutoFlush = true;
Console.WriteLine(sr.ReadLine());
while (true)
{
Console.Write("Word: ");
string msg = Console.ReadLine();
sw.WriteLine("D wn {0}", msg);
if (msg == "") break;

Console.WriteLine(sr.ReadLine());
}
s.Close();
}
finally
{
client.Close();
Console.ReadLine();
}

当我输入“hello”这个词时,它只会得到 1 行响应,然后如果我输入任何内容并按回车键,它会显示下一行,依此类推。如何一次显示完整的响应?

最佳答案

这是我想出的:

        static void Main(string[] args)
{
TcpClient client = new TcpClient("216.18.20.172", 2628);
try
{
Stream s = client.GetStream();
StreamReader sr = new StreamReader(s);
StreamWriter sw = new StreamWriter(s);
sw.AutoFlush = true;
Console.WriteLine(sr.ReadLine());
while (true)
{
Console.Write("Word: ");
string msg = Console.ReadLine();
sw.WriteLine("D wn {0}", msg);
if (msg == "") break;

var line = sr.ReadLine();
while (line != ".") // The dot character is used as an indication that no more words are found
{
Console.WriteLine(line);
line = sr.ReadLine();
}
sr.ReadLine();
}
s.Close();
}
finally
{
client.Close();
Console.ReadLine();
}
}

您还需要解决其他响应类型。当找不到单词时,我的解决方案会挂起,但可以通过注意特定的响应类型数字而不是点字符来轻松解决这个问题。

编码愉快!

编辑:这绝不是一个优雅的解决方案,我只是想说明原理。

关于c# - 无法从 TCP 服务器获得完整响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13552183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com