作者power272000 (鴨)
看板C_Sharp
標題[問題] udp server
時間Sun Jun 15 15:45:27 2008
為什麼執行時無法顯示表單
表單單純接收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
→ power272000:如果把for(;;)那段附註的話便可正常執行,請問為什麼 06/15 15:52
→ hamsters:程式卡在表單的建構子中所以沒辦法顯示 06/15 16:30
→ hamsters:即使顯示了也無法操作,請考慮使用多執行緒實作 06/15 16:31
→ power272000:謝謝 06/15 16:46
推 tomex:逐行跑看是卡在那行? 另,收封包不一定一次就會收齊. 06/15 22:57
→ power272000:要加執行緒.表單才會出現...謝謝 06/27 08:33