c# 为什么我的form框不显示?必须等到客户端的连接才显示

来源:百度知道 编辑:UC知道 时间:2024/09/24 15:24:50
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace socketServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Visible = true;
}

private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "";
label2.Text = "";

try
{
int port = 2000;
string host = "127.0.0.1";
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket s = new Socket(Ad

阻塞问题,Socket是会阻塞线程的,你的Form_load事件中监听,Form_Load方法被阴塞,一直不能返回,必须有连接进入,formload事件才返回,当然显示不出来,办法是用多线程,或把你的监听移到别的地方去,如某个按钮点击事件里去