[英]Receiving and sending data in C#
我還在努力提高我之前寫的一點點。 現在我遇到了接收數據的問題。 我有一個程序,我用它來發送字符串使用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();
}
}
有時它工作正常,但更常見的是,我們稱之為接收器,無法得到我想發送的東西。 我真的不知道它有什么問題。 看起來send方法可能有問題。 這是接收器輸出的示例
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
,我會做類似的事情:
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);
}
}
}
在追逐鬼魂之前,確保數據實際存在於流上。 如果有ALWAYS數據我們可以解決問題,但是從邏輯上看它看起來好像流被淘汰或者它上面沒有數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.