예제 #1
0
        private void UdpListenerThread()
        {
            try
            {
                var           localHost = SmartSocketClient.FindLocalHostName();
                List <string> addresses = SmartSocketClient.FindLocalIpAddresses();
                if (localHost == null || addresses.Count == 0)
                {
                    return; // no network.
                }

                IPEndPoint remoteEP = new IPEndPoint(this.GroupAddress, this.GroupPort);
                this.UdpListener = new UdpClient(this.GroupPort);
                this.UdpListener.JoinMulticastGroup(this.GroupAddress);
                while (true)
                {
                    byte[] data = this.UdpListener.Receive(ref remoteEP);
                    if (data != null)
                    {
                        BinaryReader reader = new BinaryReader(new MemoryStream(data));
                        int          len    = reader.ReadInt32();
                        string       msg    = reader.ReadString();
                        if (msg == this.ServiceName)
                        {
                            // send response back with info on how to connect to this server.
                            IPEndPoint   localEp = (IPEndPoint)this.Listener.LocalEndPoint;
                            string       addr    = localEp.ToString();
                            MemoryStream ms      = new MemoryStream();
                            BinaryWriter writer  = new BinaryWriter(ms);
                            writer.Write(addr.Length);
                            writer.Write(addr);
                            writer.Flush();
                            byte[] buffer = ms.ToArray();
                            this.UdpListener.Send(buffer, buffer.Length, remoteEP);
                        }
                    }
                }
            }
            catch (Exception)
            {
                // UdpListenerThread failed
            }
        }
예제 #2
0
        private void OnAccept(Socket client)
        {
            IPEndPoint        ep1   = client.RemoteEndPoint as IPEndPoint;
            SmartSocketClient proxy = new SmartSocketClient(this, client, this.Resolver)
            {
                Name       = ep1.ToString(),
                ServerName = SmartSocketClient.FindLocalHostName()
            };

            proxy.Disconnected += this.OnClientDisconnected;

            SmartSocketClient[] snapshot = null;

            lock (this.Clients)
            {
                snapshot = this.Clients.ToArray();
            }

            foreach (SmartSocketClient s in snapshot)
            {
                IPEndPoint ep2 = s.Socket.RemoteEndPoint as IPEndPoint;
                if (ep1 == ep2)
                {
                    // can only have one client using this end point.
                    this.RemoveClient(s);
                }
            }

            lock (this.Clients)
            {
                this.Clients.Add(proxy);
            }

            if (this.ClientConnected != null)
            {
                this.ClientConnected(this, proxy);
            }
        }