我仍在努力改进我之前写的东西。
现在我在接收数据时遇到了问题。我有一个程序,用于使用 tcpClient 将字符串发送到正在侦听指定端口的程序。它工作正常,所以我决定再发送一次数据
public static void receiveThread()
{
while (true)
{
TcpListener tcpListener = new TcpListener(IPAddress.Any, port);
tcpListener.Start();
Console.WriteLine("Waiting for connection...");
TcpClient tcpClient = tcpListener.AcceptTcpClient();
Console.WriteLine("Connected with {0}", tcpClient.Client.RemoteEndPoint);
while (!(tcpClient.Client.Poll(20, SelectMode.SelectRead)))
{
NetworkStream networkStream = tcpClient.GetStream();
StreamReader streamReader = new StreamReader(networkStream);
data = streamReader.ReadLine();
if (data != null)
{
Console.WriteLine("Received data: {0}", data);
send(data); // Here Im using send Method
}
}
Console.WriteLine("Dissconnected...\n");
tcpListener.Stop();
}
}
/// <summary>
/// Sending data
/// </summary>
/// <param name="data">Data to send</param>
public static void send(string data)
{
TcpClient tcpClient = new TcpClient();
try
{
tcpClient.Connect(ipAddress, sendPort);
Console.WriteLine("Connected with {0}", tcpClient.Client.RemoteEndPoint);
}
catch (Exception e)
{
Console.WriteLine(e);
}
if (tcpClient.Connected)
{
NetworkStream networkStream = tcpClient.GetStream();
StreamWriter streamWriter = new StreamWriter(networkStream);
Console.WriteLine("Messege {0} to {1}", data, tcpClient.Client.RemoteEndPoint);
streamWriter.WriteLine(data);
streamWriter.Flush();
tcpClient.Close();
}
}
有时它工作正常,但更常见的是,我们称其为接收器,无法获取我试图发送的内容。我真的不知道这有什么问题。看起来发送方法可能有问题。这是接收器输出的示例
Waiting for connection...
Connected with 127.0.0.1:52449
Dissconnected...
Waiting for connection...
Connected with 127.0.0.1:52450
Received data: qweqwe
Dissconnected...
Waiting for connection...
Connected with 127.0.0.1:52451
Dissconnected...
Waiting for connection...
Connected with 127.0.0.1:52452
Dissconnected...
Waiting for connection...
Connected with 127.0.0.1:52453
Received data: zxczx
Dissconnected...
Waiting for connection...
Connected with 127.0.0.1:52454
Dissconnected...
Waiting for connection...
Connected with 127.0.0.1:52455
Received data: aasd
Dissconnected...
Waiting for connection...
Connected with 127.0.0.1:52457
Received data: www
Dissconnected...
这里有几个问题:
-
StreamReader
有一个 4kB 缓冲区,并且会在第一次调用时尝试尽可能多地读取ReadLine()
。结果是,您可能在 StreamReader 中有数据,并在没有更多可用数据时进入 Poll(),因为数据已被读取。
-
Poll()
需要微秒。等待0.02ms对于传入的数据可能会返回 false,除非数据在您调用之前就已经存在Poll()
.
- 您在每次迭代中创建一个新的 StreamReader,这可能会丢弃在上一次迭代中已读取的数据。
如果您只是要读取行并想要超时和StreamReader
,我会做类似的事情:
delegate string ReadLineDelegate ();
...
using (NetworkStream networkStream = tcpClient.GetStream()) {
StreamReader reader = new StreamReader(networkStream);
ReadLineDelegate rl = new ReadLineDelegate (reader.ReadLine);
while (true) {
IAsyncResult ares = rl.BeginInvoke (null, null);
if (ares.AsyncWaitHandle.WaitOne (100) == false)
break; // stop after waiting 100ms
string str = rl.EndInvoke (ares);
if (str != null) {
Console.WriteLine ("Received: {0}", str);
send (str);
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)