刚接触socket。很简单的一个问题求助

king19840811 king19840811 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
private const int portNum = 13;
private void Form1_Load(object sender, EventArgs e)
{
bool done = false;
IPAddress local = IPAddress.Parse("127.0.0.1");
TcpListener listener = new TcpListener(local,portNum);
listener.Start();
while (!done)
{
textBox1.Text += "waiting for connection...";
TcpClient client = listener.AcceptTcpClient();//在这一行就不动了。界面显示不了。也不抛出异常

textBox1.Text += "Connection accepted\r\n";
NetworkStream ns= client.GetStream();
byte[] byteTime = Encoding.ASCII.GetBytes(DateTime.Now.ToString());

try
{
ns.Write(byteTime, 0, byteTime.Length);
ns.Close();
client.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
listener.Stop();
}
...全文

相关推荐

评论 9
收藏 已收藏
分享
返回顶部
king19840811
C# 111,130 成员 642,539 内容