Exemplo n.º 1
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();
            }
        }
Exemplo n.º 2
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);
        }
        // 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) {
            }
        }
Exemplo n.º 4
0
        public int CreateSocket(int port)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            try {
                socket.Bind(new IPEndPoint(IPAddress.Any, port));
            } catch (Exception) { return(-1); }

            UdpServerSocketObject socketObj = new UdpServerSocketObject();

            socketObj.socket      = socket;
            socketObj.remoteIpEP  = new IPEndPoint(IPAddress.Any, port);
            socketObj.DisplayName = GetDisplayName(socketObj.remoteIpEP);
            socketObj.Parent      = this;
            socketObjs.Add(socketObj);

            StartRead(socketObj);

            return(0);
        }