コード例 #1
0
        public KescoUdpClient ReservePort()
        {
            Exit();
            IPAddress ipListen;

            IPAddress[] ipads = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
            if (ipads.Where(x => x.AddressFamily == AddressFamily.InterNetwork).Count() == 1)
            {
                ipListen = ipads.First(x => x.AddressFamily == AddressFamily.InterNetwork);
            }
            else
            {
                string ip = "";
                if (GetIpToServer.TestConnection(connectionString, out ip))
                {
                    try
                    {
                        ipListen = IPAddress.Parse(ip);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(ip, ex);
                    }
                }
                else
                {
                    return(null);
                }
            }
            extPoint = new IPEndPoint(ipListen, 10300);
            int i = 10300;

            while (!Trying && (i != 11300))
            {
                extPoint.Port = i;
                try
                {
                    client = new KescoUdpClient(extPoint);
                    Trying = true;
                }
                catch
                {
                    i++;
                }
            }
            if (i == 11300)
            {
                return(null);
            }
            return(client);
        }
コード例 #2
0
        private void DataReceived(IAsyncResult ar)
        {
            if (!Started)
            {
                return;
            }
            var            objs       = (object[])ar.AsyncState;
            KescoUdpClient tmpClient  = (KescoUdpClient)objs[1];
            var            ipEndPoint = (IPEndPoint)objs[0];
            IPAddress      IpListen   = extPoint.Address;
            int            port       = extPoint.Port;

            try
            {
                getByte = tmpClient.EndReceive(ar, ref ipEndPoint);
            }
            catch
            {
                return;
            }
            if (tmpClient != client)
            {
                tmpClient.MyClose();
                tmpClient = null;
            }
            extPoint.Address = IpListen;
            extPoint.Port    = port;
            UpdateSubscription();
            if (getByte != null)
            {
                OnRecived(Encoding.Default.GetString(getByte));
            }
            try
            {
                if (tmpClient != null && client != null)
                {
                    client.BeginReceive(DataReceived, ar.AsyncState);
                }
            }
            catch
            {
            }
        }
コード例 #3
0
 public void Exit()
 {
     try
     {
         Started = false;
         if (client != null)
         {
             client.MyClose();
             client = null;
         }
         if (strID > 0)
         {
             using (var cn = new SqlConnection(connectionString))
                 using (SqlCommand scm = new SqlCommand("DELETE FROM " + dataTable + " WHERE (КодРегистрацииРассылки = @id)", cn))
                 {
                     scm.Parameters.Add("@id", SqlDbType.Int, 4).Value = strID;
                     try
                     {
                         scm.Connection.Open();
                         scm.ExecuteNonQuery();
                     }
                     catch (SqlException sqlEx)
                     {
                         sqlErrorShow(sqlEx);
                     }
                     finally
                     {
                         scm.Connection.Close();
                     }
                 }
         }
     }
     catch
     {
     }
 }