private void ShowTCPServerView() { TCPServerSocket p1 = (TCPServerSocket)socketModels[status.index1]; TcpServerSocketObject p2 = (TcpServerSocketObject)p1.Children[status.index2]; TsocketStatus.Visibility = Visibility.Visible; TLlocalPort.Visibility = Visibility.Visible; TlocalPort.Visibility = Visibility.Visible; if (status.index3 == -1) { BTSStartListen.Visibility = Visibility.Visible; TsocketStatus.Text = p2.IsListening ? "正在监听" : "未监听"; BTSStartListen.Content = p2.IsListening ? "停止监听" : "启动监听"; TlocalPort.Text = ((IPEndPoint)p2.socket.LocalEndPoint).Port.ToString(); } else { RemoteSocketObject p3 = (RemoteSocketObject)p2.Children[status.index3]; BTCConnect.Visibility = Visibility.Visible; TsocketStatus.Text = p3.socket.Connected ? "已连接" : "未连接"; BTCConnect.Content = "断开连接"; BTCConnect.IsEnabled = p3.socket.Connected; TLremoteIP.Visibility = Visibility.Visible; TremoteIP.Visibility = Visibility.Visible; TLremotePort.Visibility = Visibility.Visible; TremotePort.Visibility = Visibility.Visible; TremoteIP.Text = ((IPEndPoint)p3.socket.RemoteEndPoint).Address.ToString(); TremotePort.Text = ((IPEndPoint)p3.socket.RemoteEndPoint).Port.ToString(); TlocalPort.Text = ((IPEndPoint)p3.socket.LocalEndPoint).Port.ToString(); } }
public void StopListening(int index2) { if (index2 >= 0 && index2 < socketObjs.Count) { TcpServerSocketObject p2 = (TcpServerSocketObject)socketObjs[index2]; p2.IsListening = false; DelAllRemoteSocketObj(index2); p2.socket.Close(); Socket socket = new Socket(IPAddress.Any.AddressFamily, SocketType.Stream, ProtocolType.Tcp); socket.Bind(p2.remoteIpEP); p2.socket = socket; } }
internal static void StartListening(object state) { TcpServerSocketObject socketObj = (TcpServerSocketObject)state; try { socketObj.socket.Listen(255); socketObj.IsListening = true; while (socketObj.IsListening) { socketObj.doneEvent.Reset(); socketObj.socket.BeginAccept( new AsyncCallback(acceptCallback), socketObj); socketObj.doneEvent.WaitOne(); } } catch (Exception) { } }
private void BTSStartListen_Click(object sender, RoutedEventArgs e) { TCPServerSocket p1 = socketModels[status.index1] as TCPServerSocket; if (p1 == null || status.index2 < 0) { return; } TcpServerSocketObject p2 = (TcpServerSocketObject)p1.Children[status.index2]; if (p2.IsListening) { p1.StopListening(status.index2); } else { p1.StartListening(status.index2); } RefreshDisplay(); }
internal static void acceptCallback(IAsyncResult ar) { TcpServerSocketObject socketObj = (TcpServerSocketObject)ar.AsyncState; try { Socket remoteSocket = socketObj.socket.EndAccept(ar); socketObj.doneEvent.Set(); RemoteSocketObject remoteSocketObj = new RemoteSocketObject(); remoteSocketObj.socket = remoteSocket; remoteSocketObj.Parent = socketObj; remoteSocketObj.remoteEP = remoteSocket.RemoteEndPoint; remoteSocketObj.DisplayName = SocketModel.GetDisplayName(remoteSocketObj.remoteEP); remoteSocketObj.Icon = IconResources.ICON_CLIENT; socketObj.remoteSocketObjs.Add(remoteSocketObj); OnAccept(new AcceptEventArgs(socketObj)); remoteSocket.BeginReceive(remoteSocketObj.buffer, 0, RemoteSocketObject.BufferSize, 0, new AsyncCallback(readCallback), remoteSocketObj); } catch (Exception) { } }
public int CreateSocket(int port) { IPEndPoint localEP = new IPEndPoint(IPAddress.Any, port); Socket socket = new Socket(localEP.Address.AddressFamily, SocketType.Stream, ProtocolType.Tcp); try { socket.Bind(localEP); } catch (Exception) { return(-1); } TcpServerSocketObject socketObj = new TcpServerSocketObject(); socketObj.socket = socket; socketObj.remoteIpEP = localEP; socketObj.Parent = this; socketObj.DisplayName = GetDisplayName(localEP); socketObjs.Add(socketObj); ThreadPool.QueueUserWorkItem(AsynchronousSocketListener.StartListening, socketObj); return(0); }