/// <summary> /// 新线程运行方法:开启监听本机ip及端口号 /// </summary> private void ListentingServerStart() { var ipep = new IPEndPoint(_ipAddress, _port); //IPV4地址、端口号 _socketServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); _socketServer.Bind(ipep); //将所创建的套接字与IPEndPoint绑定 _socketServer.Listen(10); try { //在套接字上接收接入的连接 _serverPresenter.GetControlMessage("等待TCP客户端响应"); _socketServerSub = _socketServer.Accept(); _threadServerSub = new Thread(ReceiveData); //新建接收数据线程 _threadServerSub.Start(); //启动线程 } catch (Exception ex) { _serverPresenter.GetControlMessage("服务端监听链接中断:" + ex.Message); _serverPresenter.StopListening(); } }
private void btnStart_Click(object sender, RoutedEventArgs e) { if (BtnStart.Content.ToString() == "开始监听") { if ((TextBoxPort.Text != string.Empty) && (ComboBoxIp.Text != string.Empty)) //检测IP地址框和端口框是否为空 { BtnStart.Content = "断开监听"; _accessPresenter.StartListening(_addresses[ComboBoxIp.SelectedIndex], int.Parse(TextBoxPort.Text)); ControlMessageShow("准备开启TCP服务"); } else { MessageBox.Show("请输入本地端口号,重新连接", "提示"); } } else { BtnStart.Content = "开始监听"; _accessPresenter.StopListening(); } }