當我運行我的應用程式的兩個實體時,它們應該連接并能夠向其他應用程式發送訊息 hello。我知道它們連接是因為客戶端會顯示訊息已經發送甚至從服務器接收到訊息。但是只有客戶端是可見的。一旦我單擊開始,服務器就會“凍結”。我用錯了嗎? 凍結和可見應用程式的影像
private void Startbutton_Click(object sender, RoutedEventArgs e)
{
try
{
listener = new TcpListener(localAddr, port);
ChatScreentextBox.AppendText("waiting for connection..." "\n");
listener.Start();
// Buffer for reading data
Byte[] bytes = new Byte[256];
String data = null;
while (true)
{
TcpClient client = listener.AcceptTcpClient();
data = null;
// Get a stream object for reading and writing
NetworkStream stream = client.GetStream();
int i;
// Loop to receive all the data sent by the client.
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
// Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
ChatScreentextBox.AppendText(data);
// Process the data sent by the client.
data = data.ToUpper();
byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);
// Send back a response.
stream.Write(msg, 0, msg.Length);
ChatScreentextBox.AppendText(data "connection");
}
// Shutdown and end connection
client.Close();
}
}
catch (SocketException ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
// Stop listening for new clients.
listener.Stop();
}
}
private void Connectbutton_Click(object sender, RoutedEventArgs e)
{
try
{
// Create a TcpClient.
// Note, for this client to work you need to have a TcpServer
// connected to the same address as specified by the server, port
// combination.
Int32 port = 88;
TcpClient client = new TcpClient("127.0.0.1", port);
// Translate the passed message into ASCII and store it as a Byte array.
Byte[] data = System.Text.Encoding.ASCII.GetBytes("hello");
// Get a client stream for reading and writing.
// Stream stream = client.GetStream();
NetworkStream stream = client.GetStream();
// Send the message to the connected TcpServer.
stream.Write(data, 0, data.Length);
//Console.WriteLine("Sent: {0}", message);
ChatScreentextBox.AppendText(data "\n");
// Receive the TcpServer.response.
// Buffer to store the response bytes.
data = new Byte[256];
// String to store the response ASCII representation.
String responseData = String.Empty;
// Read the first batch of the TcpServer response bytes.
Int32 bytes = stream.Read(data, 0, data.Length);
responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
ChatScreentextBox.AppendText("you:" responseData "this\n");
// Close everything.
stream.Close();
client.Close();
}
catch (ArgumentNullException ex)
{
MessageBox.Show(ex.Message.ToString());
}
catch (SocketException ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
uj5u.com熱心網友回復:
您正在TCPClient無限while回圈中接受按鈕的 Click 事件。這是應用程式無回應的原因。
您需要做的是在單獨的執行緒上運行它。BackgroundWorker是我的首選方式。Task也是一個相對較新和流行的。
有大量關于如何使用兩者的示例。簡而言之,如果您使用BackgroundWorker,則需要創建一個 type 欄位,BackgroundWorker在DoWork事件中擁有所有“智能” ,然后從按鈕 Click 事件中呼叫該.RunWorkerAsync()方法。
如果這不清楚并且您需要更多幫助,請在此處發表評論。
uj5u.com熱心網友回復:
當您接受連接并開始(while 回圈)從連接讀取資料時,它正在阻塞操作,并且您正在主執行緒(這是更新 UI 例程運行的地方)上運行此操作。當您接受連接時,您正在讀取資料,但 UI 永遠不會更新,因為執行緒仍在讀取/等待資料。
要解決此問題,您需要在單獨的執行緒中運行此讀取操作來處理讀取和處理。您可以在此處查看示例。
要了解有關 C# 執行緒的更多資訊,請參閱此處的示例或閱讀Microsoft 檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/319006.html
上一篇:WPF資料網格用戶控制元件系結
