gpt4 book ai didi

c# - NetworkStream 从不写入数据

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

好吧,所以我理解 TCP 是基于流而不是基于消息的整个想法 - 我不关心这个。

我想做的是简单地序列化一些东西,然后通过网络将它发送到另一个使用相同的基于消息的协议(protocol)的应用程序。

问题是,每当我序列化数据(我正在序列化为 XML),然后将其写入网络流时,该死的东西永远不会写入它 - 永远不会。直到我关闭程序并关闭流,流才真正发送数据。到底发生了什么——TCP 是否在等待足够的数据发送?是不是在等我什么? (我怀疑前者,因为我可以写简单的行,而它做得很好。)

这是我的客户的代码:

        TcpClient client = new TcpClient();
client.Connect(IPAddress.Parse("127.0.0.1"), 10100);
NetworkStream ns = client.GetStream();
StreamWriter writer = new StreamWriter(ns);

ListenerThread th = new ListenerThread(new StreamReader(ns));
new Thread(th.run).Start();

XmlSerializer serializer = new XmlSerializer(typeof(Message), new Type[] {typeof(AuthenticationMessage), typeof(ChangeChatRoomMessage), typeof(ChangePasswordMessage), typeof(ConnectionStatusMessage), typeof(InitializeMessage), typeof(StatusMessage), typeof(SuccessMessage), typeof(TextMessage)});

string file = "<some test xml file>";
while(true)
{
FileStream stream = new FileStream(file, FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(stream);
Message newmsg = (Message)serializer.Deserialize(reader);
stream.Close();

serializer.Serialize(writer, newmsg);
writer.Flush();

file = Console.ReadLine();
}

服务器代码如下:

    public void HandleMessage()
{
Message msg = (Message)serializer.Deserialize(reader);
Console.WriteLine("Read Message " + msg.GetType());
}

public void Start()
{
while(true)
{
Socket socket = listener.AcceptSocket();
NetworkStream stream = new NetworkStream(socket);
reader = new StreamReader(stream);
HandleMessage();
stream.Close();
}
}

我的程序现在已经运行了 20 分钟,但没有向服务器发送任何内容。我尝试了所有我能想到的方法,刷新我的写入器缓冲区(TextWriter,显然不是 NetworkStream),并将客户端设置为 NoDelay,等等……发生了什么,为什么?

最佳答案

Alright, so I understand the whole idea that TCP is stream based and not message based - I don't care about that.

其实这很重要,你应该关心它。向服务器发送数据时,您不仅应该尝试发送一些消息,还应该定义一些协议(protocol)(或使用现有协议(protocol)之一),客户端在其中向服务器指示例如它打算发送多少数据。

下面是您如何继续的示例。在此示例中,客户端将总消息的长度作为前 4 个字节发送。

服务器:

class Program
{
static void Main()
{
var listener = new TcpListener(new IPEndPoint(IPAddress.Loopback, 10100));
listener.Start();
while (true)
{
using (var client = listener.AcceptTcpClient())
using (var stream = client.GetStream())
using (var reader = new BinaryReader(stream))
{
// The first 4 bytes of the message will indicate
// the total message length
var length = reader.ReadInt32();
var buffer = reader.ReadBytes(length);
Console.WriteLine("Received {0} bytes from client:", length);
Console.WriteLine("{0}", Encoding.UTF8.GetString(buffer));
}
}
}
}

客户:

class Program
{
static void Main()
{
using (var client = new TcpClient("127.0.0.1", 10100))
using (var stream = client.GetStream())
using (var writer = new BinaryWriter(stream))
{
var message = "<some test xml file>";
var buffer = Encoding.UTF8.GetBytes(message);
// Send the total message length in the first 4 bytes
// so that the server knows how much it has to read
writer.Write(buffer.Length);
writer.Write(buffer);
Console.WriteLine("Successfully sent {0} bytes to server", buffer.Length);
}
}
}

关于c# - NetworkStream 从不写入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7212918/

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