嘿,我正在写一个服务器-客户端程序 但是当我的客户端发送一些东西时,它永远不会到达我的服务器!
我是这样发送的:
public void Send(string s)
{
char[] chars = s.ToCharArray();
byte[] bytes = chars.CharToByte();
nstream.Write(bytes, 0, bytes.Length);
nstream.Flush();
}
并像这样在后台线程中接收
void CheckIncoming(object dd)
{
RecievedDelegate d = (RecievedDelegate)dd;
try
{
while (true)
{
List<byte> bytelist = new List<byte>();
System.Threading.Thread.Sleep(1000);
int ssss;
ssss = nstream.ReadByte();
if (ssss > 1)
{
System.Diagnostics.Debugger.Break();
}
if (bytelist.Count != 0)
{
d.Invoke(bytelist.ToArray());
}
}
}
catch (Exception exp)
{
MSGBOX("ERROR:\n" + exp.Message);
}
}
ssss int 永远不会 > 1 这里发生了什么???
最佳答案
NetworkStream.Flush()
其实没有效果:
The Flush method implements the Stream..::.Flush method; however, because NetworkStream is not buffered, it has no affect [sic] on network streams. Calling the Flush method does not throw an exception
正在发送多少数据?
如果您没有发送足够的数据,它可能会在网络级别保持缓冲,直到您关闭流或写入更多数据。
参见 TcpClient.NoDelay
如果您只发送小块数据并需要低延迟,请使用属性来禁用它。
关于c# - 网络流总是空的!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2507929/