我有这段代码可以将String
消息发送到服务器。这些消息应该单独发送,但是Stream
的{{1}}在TcpClient
关闭后,正在一个bug块中发送消息。
如何使用此代码发送分隔的消息:
Stream
使用此代码,服务器最终在一条消息中接收:public void sendData()
{
const int PORT_NO = 12900;
const string SERVER_IP = "127.0.0.1";
TcpClient client = new TcpClient(SERVER_IP, PORT_NO);
try
{
client.NoDelay = true;
NetworkStream nwStream = client.GetStream();
sendText(nwStream, "Text-1"); // Here is it supposed to send "Text-1"
sendText(nwStream, "Text-2");
sendText(nwStream, "Text-3");
}
finally
{
client.Close(); // But, all messages are sent here!
}
}
private void sendText(NetworkStream nwStream, String text)
{
byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes(text);
nwStream.Write(bytesToSend, 0, bytesToSend.Length);
nwStream.Flush();
}
,并且应该接收3条不同的消息。怎么了?