示例#1
0
        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();
            }
        }
示例#2
0
        private void ShowUDPServerView()
        {
            UDPServerModel        p1 = (UDPServerModel)socketModels[status.index1];
            UdpServerSocketObject p2 = (UdpServerSocketObject)p1.Children[status.index2];

            TLlocalPort.Visibility = Visibility.Visible;
            TlocalPort.Visibility  = Visibility.Visible;
            TlocalPort.Text        = ((IPEndPoint)p2.socket.LocalEndPoint).Port.ToString();
            if (status.index3 != -1)
            {
                BTCConnect.Visibility = Visibility.Visible;
                BTCConnect.IsEnabled  = true;
                BTCConnect.Content    = "删除";

                TLremoteIP.Visibility   = Visibility.Visible;
                TremoteIP.Visibility    = Visibility.Visible;
                TLremotePort.Visibility = Visibility.Visible;
                TremotePort.Visibility  = Visibility.Visible;

                RemoteSocketObject p3 = (RemoteSocketObject)p2.Children[status.index3];

                TremoteIP.Text   = ((IPEndPoint)p3.remoteEP).Address.ToString();
                TremotePort.Text = ((IPEndPoint)p3.remoteEP).Port.ToString();
            }
        }
        internal static void readCallback(IAsyncResult ar)
        {
            RemoteSocketObject remoteSocketObj = (RemoteSocketObject)ar.AsyncState;
            Socket             handler         = remoteSocketObj.socket;

            try {
                int recvCnt = handler.EndReceive(ar);

                if (recvCnt > 0)
                {
                    SocketData recvData = new SocketData();
                    recvData.time = DateTime.Now;
                    recvData.type = 0;
                    recvData.data = remoteSocketObj.buffer.GetBytes(recvCnt);
                    remoteSocketObj.dataList.Add(recvData);
                    string typeStr = recvData.type == 0 ? "[接收]" : "[发送]";
                    remoteSocketObj.sb.Append(recvData.time.ToShortTimeString() + " " + typeStr + ":" + Encoding.ASCII.GetString(recvData.data, 0, recvCnt) + "\n");
                    OnRecv(new RecvEventArgs(remoteSocketObj));

                    handler.BeginReceive(remoteSocketObj.buffer, 0, RemoteSocketObject.BufferSize, 0,
                                         new AsyncCallback(readCallback), remoteSocketObj);
                }
                else
                {
                    if (remoteSocketObj.Parent as TcpServerSocketObject != null)
                    {
                        remoteSocketObj.Parent.Children.Remove(remoteSocketObj);
                    }
                    remoteSocketObj.socket.Disconnect(true);
                    OnRecv(new RecvEventArgs(remoteSocketObj, true));
                }
            } catch (Exception) {
            }
        }
示例#4
0
        protected void StartRead(UdpServerSocketObject socketObj)
        {
            RemoteSocketObject listenSocketObj = new RemoteSocketObject();

            listenSocketObj.socket    = socketObj.socket;
            listenSocketObj.Parent    = socketObj;
            socketObj.listenSocketObj = listenSocketObj;
            EndPoint tempEP = (EndPoint)(new IPEndPoint(IPAddress.Any, 0));
            var      ar     = listenSocketObj.socket.BeginReceiveFrom(listenSocketObj.buffer, 0, RemoteSocketObject.BufferSize, 0,
                                                                      ref tempEP, new AsyncCallback(AsynchronousSocketListener.UDPServerReadCallback), listenSocketObj);
        }
        internal static void StartRead(SocketObject socketObj)
        {
            RemoteSocketObject remoteSocketObj = new RemoteSocketObject();

            remoteSocketObj.socket   = socketObj.socket;            // 除了TCP服务器,都是这样
            remoteSocketObj.Parent   = socketObj;
            remoteSocketObj.remoteEP = socketObj.remoteIpEP;
            socketObj.remoteSocketObjs.Add(remoteSocketObj);
            remoteSocketObj.socket.BeginReceive(remoteSocketObj.buffer, 0, RemoteSocketObject.BufferSize, 0,
                                                new AsyncCallback(AsynchronousSocketListener.readCallback), remoteSocketObj);
        }
示例#6
0
 protected override void DelAllRemoteSocketObj(int index2)
 {
     foreach (var c in ((SocketObject)socketObjs[index2]).remoteSocketObjs)
     {
         RemoteSocketObject obj = (RemoteSocketObject)c;
         obj.socket.Shutdown(SocketShutdown.Both);
         obj.socket.Disconnect(false);
         obj.socket.Close();
     }
     ((SocketObject)socketObjs[index2]).remoteSocketObjs.Clear();
 }
        // 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) {
            }
        }
示例#8
0
        private void GetIndex(RemoteSocketObject obj, out int index2, out int index3)
        {
            IFPropertyNodeItem p2 = obj.Parent;
            IFPropertyNodeItem p1 = p2.Parent;

            if (p2.Children != null)
            {
                index3 = p2.Children.IndexOf(obj);
            }
            else
            {
                index3 = -1;
            }
            index2 = p1.Children.IndexOf(p2);
        }
示例#9
0
        // 删除3级节点
        protected virtual bool DelRemoteSocketObj(int index2, int index3)
        {
            try {
                RemoteSocketObject obj = (RemoteSocketObject)((SocketObject)socketObjs[index2]).remoteSocketObjs[index3];
                obj.socket.Shutdown(SocketShutdown.Both);
                obj.socket.Disconnect(false);
                obj.socket.Close();
            } catch (Exception) {
                return(false);
            } finally {
                ((SocketObject)socketObjs[index2]).remoteSocketObjs.RemoveAt(index3);
            }

            return(true);
        }
示例#10
0
        // 发送数据
        public virtual void SendPacket(int index2, int index3, string text)
        {
            if (index2 != -1 && index2 < socketObjs.Count &&
                index3 != -1 && index3 < ((SocketObject)socketObjs[index2]).remoteSocketObjs.Count)
            {
                byte[]             sendBytes       = Encoding.ASCII.GetBytes(text);
                RemoteSocketObject remoteSocketObj = (RemoteSocketObject)((SocketObject)socketObjs[index2]).remoteSocketObjs[index3];
                remoteSocketObj.socket.SendTo(sendBytes, remoteSocketObj.remoteEP);

                SocketData sendData = new SocketData();
                sendData.time = DateTime.Now;
                sendData.type = 1;
                sendData.data = sendBytes.GetBytes();
                remoteSocketObj.dataList.Add(sendData);
                remoteSocketObj.sb.Append(remoteSocketObj.genSendString(sendData));

                SetSendText(index2, index3, "");                 // 清空待发数据
            }
        }
        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) { }
        }
示例#12
0
 private void RecvMsg(object sender, RecvEventArgs e)
 {
     if (!e.IsErr)
     {
         if (e.RemoteSocketObj.Parent is UdpServerSocketObject)
         {
             SocketObject obj        = (SocketObject)e.RemoteSocketObj.Parent;
             bool         isSelected = false;
             if (socketModels[index1] == (SocketModel)obj.Parent && obj.Parent.Children[index2] == obj)
             {
                 isSelected = true;
             }
             ((SocketModel)obj.Parent).UpdateSubTreeView(obj, isSelected);
         }
         Dispatcher.Invoke(Refresh);
         Dispatcher.Invoke(RefreshTree);
     }
     else
     {
         ObjNeedRm = e.RemoteSocketObj;
         Dispatcher.Invoke(RemoveRemoteItem);
     }
 }
 public RecvEventArgs(RemoteSocketObject socketObj, bool isErr = false)
 {
     remoteSocketObj = socketObj;
     this.isErr      = isErr;
 }