예제 #1
0
        public int CreateSocket(IPEndPoint ipEP, int index1, int index2)
        {
            NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
            UnicastIPAddressInformationCollection ipInf = interfaces[index1].GetIPProperties().UnicastAddresses;
            IPAddress localAddress = ipInf[index2].Address;

            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            try {
                socket.Bind(new IPEndPoint(localAddress, ipEP.Port));
            } catch (Exception) { return(-1); }

            MulticastOption mcastOpt = new MulticastOption(ipEP.Address, localAddress);

            socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, mcastOpt);
            socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.MulticastInterface, 0);
            UdpGroupSocketObject socketObj = new UdpGroupSocketObject();

            socketObj.socket      = socket;
            socketObj.remoteIpEP  = ipEP;
            socketObj.DisplayName = GetDisplayName(ipEP);
            socketObj.Parent      = this;
            socketObj.Children    = null;
            socketObjs.Add(socketObj);

            AsynchronousSocketListener.StartRead(socketObj);
            return(0);
        }
예제 #2
0
        public int CreateSocket(IPEndPoint remoteIpEP, int localPort)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            try {
                socket.Bind(new IPEndPoint(IPAddress.Any, localPort));
            } catch (Exception) { return(-1); }
            socket.EnableBroadcast = true;

            UdpClientSocketObject socketObj = new UdpClientSocketObject();

            socketObj.socket      = socket;
            socketObj.Children    = null;          // 不显示隐藏的3级节点
            socketObj.remoteIpEP  = remoteIpEP;
            socketObj.DisplayName = GetDisplayName(socketObj.remoteIpEP);
            socketObj.Parent      = this;
            socketObjs.Add(socketObj);

            AsynchronousSocketListener.StartRead(socketObj);

            return(0);
        }
예제 #3
0
        private void Connect(TcpClientSocketObject socketObj)
        {
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socketObj.socket = socket;
            socketObj.connectDone.Reset();
            var ar = socketObj.socket.BeginConnect(socketObj.remoteIpEP, AsynchronousSocketListener.connectCallback, socketObj);

            try {
                if (socketObj.connectDone.WaitOne(400))
                {
                    if (socketObj.socket.Connected)
                    {
                        AsynchronousSocketListener.StartRead(socketObj);
                    }
                }
                else
                {
                    socketObj.socket.EndConnect(ar);
                }
            } catch (Exception) {
            }
        }