private void Form1_Load(object sender, EventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; try { string ip = AccessIni.ReadIni("本地地址", "IP", "127.0.0.1", ".\\Config.ini"); string port = AccessIni.ReadIni("本地地址", "PORT", "12300", ".\\Config.ini"); Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socketWatch.Bind(new IPEndPoint(IPAddress.Parse(ip), int.Parse(port))); socketWatch.Listen(5); Thread th = new Thread(Listen); th.IsBackground = true; th.Start((object)socketWatch); } catch (Exception ex) { ShowMsg(ex.Message); } }
void Listen(object o) { Socket sockWatch = o as Socket; while (true) { try { remoteSocket = sockWatch.Accept(); //dicSocket.Add(remoteSocket.RemoteEndPoint.ToString(), remoteSocket); //cboUsers.Items.Add(remoteSocket.RemoteEndPoint.ToString()); //ShowMsg(remoteSocket.RemoteEndPoint.ToString() + ":" + "连接成功"); string tmpName = remoteSocket.RemoteEndPoint.ToString().Split(':')[0] == AccessIni.ReadIni("本地地址", "IP", "127.0.0.1", ".\\Config.ini") ? "基层1" : "基层2"; ShowMsg(tmpName + ":" + "连接成功"); Thread th = new Thread(Receice); th.IsBackground = true; th.Start(remoteSocket); } catch { } } }