// callback函数 internal static void UDPServerReadCallback(IAsyncResult ar) { RemoteSocketObject listenSocketObj = (RemoteSocketObject)ar.AsyncState; Socket handler = listenSocketObj.socket; EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0); try { int recvCnt = handler.EndReceiveFrom(ar, ref remoteEndPoint); if (recvCnt > 0) { SocketData recvData = new SocketData(); recvData.time = DateTime.Now; recvData.type = 0; recvData.data = listenSocketObj.buffer.GetBytes(recvCnt); UdpServerSocketObject parent = (UdpServerSocketObject)listenSocketObj.Parent; RemoteSocketObject dstObj = null; foreach (var c in parent.remoteSocketObjs) { if (((RemoteSocketObject)c).remoteEP.ToString() == remoteEndPoint.ToString()) { dstObj = (RemoteSocketObject)c; break; } } if (dstObj == null) { dstObj = new RemoteSocketObject(); dstObj.socket = handler; dstObj.Parent = listenSocketObj.Parent; dstObj.remoteEP = remoteEndPoint; dstObj.DisplayName = SocketModel.GetDisplayName(dstObj.remoteEP); dstObj.Icon = IconResources.ICON_CLIENT; ((SocketObject)dstObj.Parent).remoteSocketObjs.Add(dstObj); } dstObj.dataList.Add(recvData); dstObj.sb.Append(dstObj.genRecvString(recvData)); OnRecv(new RecvEventArgs(dstObj)); EndPoint tempEP = (EndPoint)(new IPEndPoint(IPAddress.Any, 0)); handler.BeginReceiveFrom(listenSocketObj.buffer, 0, RemoteSocketObject.BufferSize, 0, ref tempEP, new AsyncCallback(UDPServerReadCallback), listenSocketObj); } else { } } catch (Exception) { } }
/// <summary> /// 切换不同服务的页面 /// </summary> private void SwitchView() { if (index1 < 0 && index2 < 0) { return; } if (index2 >= 0) { status.index1 = index1; status.index2 = index2; status.index3 = index3; } else { if (status.index2 < 0) { return; } } HideAllCtls(); SocketModel p1 = (SocketModel)socketModels[status.index1]; if (p1 is TCPServerSocket) { ShowTCPServerView(); } else if (p1 is TCPClientModel) { ShowTCPClientView(); } else if (p1 is UDPServerModel) { ShowUDPServerView(); } else if (p1 is UDPClientModel) { ShowUDPClientView(); } else if (p1 is UDPGroupSocket) { ShowUDPGroupView(); } else { status.index1 = status.index2 = status.index3 = -1; } }
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) { } }
private void BTCConnect_Click(object sender, RoutedEventArgs e) { SocketModel p1 = (SocketModel)socketModels[status.index1]; if (p1 is TCPServerSocket) { DelNode(status.index1, status.index2, status.index3); } else if (p1 is TCPClientModel) { TcpClientSocketObject p2 = (TcpClientSocketObject)p1.Children[status.index2]; if (p2.socket != null && p2.socket.Connected) { ((TCPClientModel)p1).Disconnect(status.index2); } else { ((TCPClientModel)p1).Connect(status.index2); } } RefreshDisplay(); }