作者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
1F:→ power272000:如果把for(;;)那段附注的话便可正常执行,请问为什麽 06/15 15:52
2F:→ hamsters:程式卡在表单的建构子中所以没办法显示 06/15 16:30
3F:→ hamsters:即使显示了也无法操作,请考虑使用多执行绪实作 06/15 16:31
4F:→ power272000:谢谢 06/15 16:46
5F:推 tomex:逐行跑看是卡在那行? 另,收封包不一定一次就会收齐. 06/15 22:57
6F:→ power272000:要加执行绪.表单才会出现...谢谢 06/27 08:33