[問題] udp server

看板C_Sharp (C#)作者 (鴨)時間17年前 (2008/06/15 15:45), 編輯推噓1(105)
留言6則, 3人參與, 最新討論串1/2 (看更多)
為什麼執行時無法顯示表單 表單單純接收client資料並自動回傳 現在client端可以收到server回傳東西 以下是程式碼 using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System; //For Console, Int32,ArgumentException,Environment using System.Net; //IPEndPoint using System.Net.Sockets; //For UdpClient,SocketException namespace WindowsApplication1 { public partial class udp_test : Form { public udp_test() { InitializeComponent(); Open_udp(); } public void Open_udp() { int serverPort = 6666; UdpClient client = null; try { client = new UdpClient(serverPort); } catch (SocketException se) { ShowBox.AppendText(se.ErrorCode + ":" + se.Message); Environment.Exit(se.ErrorCode); } IPEndPoint reIPEndPoint = new IPEndPoint(IPAddress.Any, 0); for (; ; ) { try { byte[] byteBuffer = client.Receive(ref reIPEndPoint); ShowBox.AppendText("handling client at " + reIPEndPoint + "-" + "\n"); ShowBox.AppendText(Encoding.ASCII.GetString(byteBuffer, 0, byt\eBuffer.Length) + "\n"); client.Send(byteBuffer, byteBuffer.Length, reIPEndPoint); ShowBox.AppendText("{0}byte"+ byteBuffer.Length); } catch (SocketException se) { ShowBox.AppendText(se.ErrorCode + ":" + se.Message); } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.229.174.22

06/15 15:52, , 1F
如果把for(;;)那段附註的話便可正常執行,請問為什麼
06/15 15:52, 1F

06/15 16:30, , 2F
程式卡在表單的建構子中所以沒辦法顯示
06/15 16:30, 2F

06/15 16:31, , 3F
即使顯示了也無法操作,請考慮使用多執行緒實作
06/15 16:31, 3F

06/15 16:46, , 4F
謝謝
06/15 16:46, 4F

06/15 22:57, , 5F
逐行跑看是卡在那行? 另,收封包不一定一次就會收齊.
06/15 22:57, 5F

06/27 08:33, , 6F
要加執行緒.表單才會出現...謝謝
06/27 08:33, 6F
文章代碼(AID): #18LCYOMt (C_Sharp)
討論串 (同標題文章)
文章代碼(AID): #18LCYOMt (C_Sharp)