コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="ip"></param>
        public async Task ConnectRemoteServer(string ip)
        {
            bool connected = false;

            try
            {
                CommClient = new TcpClient();
                await CommClient.ConnectAsync(IPAddress.Parse(ip), 10080);

                connected = true;
            }
            catch
            {
                throw;
            }
            finally
            {
                if (!connected)
                {
                    CommClient.Close();
                    CommClient = null;
                }
                else
                {
                    SetUpReceivingTask();
                    CommunicationEstablished?.Invoke(false);
                }
            }
        }
コード例 #2
0
        /// <summary>
        ///
        /// </summary>
        public async Task StartServer(CancellationToken token)
        {
            bool accepted       = false;
            bool listenerReturn = false;

            try
            {
                var commEndPoint = new IPEndPoint(IPAddress.Any, 10080);
                Listener        = new TcpListener(commEndPoint);
                BroadcastSender = new UdpClient();

                Listener.Start();

                var taskAccept = Task.Run(() =>
                {
                    try
                    {
                        CommClient = Listener.AcceptTcpClient();
                        accepted   = true;
                    }
                    finally
                    {
                        listenerReturn = true;
                    }
                });

                var taskBroadcast = Task.Run(async() =>
                {
                    while (true)
                    {
                        if (listenerReturn)
                        {
                            break;
                        }

                        try
                        {
                            var remoteEndPoint = new IPEndPoint(IPAddress.Broadcast, 10080);
                            BroadcastSender.Send(new byte[] { 0xcc, 0xdd }, 2, remoteEndPoint);
                        }
                        catch { }

                        await Task.Delay(2000);
                    }
                });

                var cancellationCheck = Task.Run(async() =>
                {
                    try
                    {
                        while (true)
                        {
                            await Task.Delay(500);

                            if (token.IsCancellationRequested)
                            {
                                if (!accepted)
                                {
                                    Listener.Stop();
                                }

                                break;
                            }

                            if (listenerReturn)
                            {
                                break;
                            }
                        }
                    }
                    catch { }
                });

                await taskAccept;
            }
            catch
            {
                throw;
            }
            finally
            {
                Listener.Stop();
                BroadcastSender.Close();

                if ((!accepted) && (CommClient != null))
                {
                    CommClient.Close();
                    CommClient = null;
                }

                if (accepted)
                {
                    SetUpReceivingTask();
                    CommunicationEstablished?.Invoke(true);
                }
            }
        }