Example #1
0
        private async void btn_TCPclient_Click(object sender, RoutedEventArgs e)
        {
            btn_TCPserver.IsEnabled = false;

            if (String.IsNullOrWhiteSpace(txt_Port.Text) || String.IsNullOrWhiteSpace(txt_IPAddress.Text))
            {
                if (String.IsNullOrEmpty(txt_Port.Text) || String.IsNullOrEmpty(txt_IPAddress.Text))
                {
                    txt_GeneralChat.Text = "You didn`t specify IP and Port.Default settings are used:\n" +
                                           "port: 25000\n" +
                                           "IP: 192.168.0.100-103";
                    var message = txt_Send.Text;
                    //var progress = new Progress<string>(s => label.Text = s);
                    await Task.Factory.StartNew(() => NetworkCooperation.TcpConnectionClient("192.168.0.102", 25000, message),
                                                TaskCreationOptions.LongRunning);

                    //var ip = txt_IPAddress.Text;
                    //await Task.Factory.StartNew(() => NetworkCooperation.SocketClientTCP(25000, ip),
                    //                                 TaskCreationOptions.LongRunning);
                }
            }

            else
            {
                var _port   = Int32.Parse(txt_Port.Text);
                var _ip     = txt_IPAddress.Text;
                var message = txt_Send.Text;
                //var progress = new Progress<string>(s => label.Text = s);
                await Task.Factory.StartNew(() => NetworkCooperation.TcpConnectionClient(_ip, _port, message),
                                            TaskCreationOptions.LongRunning);

                //await Task.Factory.StartNew(() => NetworkCooperation.SocketClientTCP(_port,_ip),
                //                                 TaskCreationOptions.LongRunning);
            }
        }
Example #2
0
 private async void btn_DistrFile_Click(object sender, RoutedEventArgs e)
 {
     IPAddress ip   = IPAddress.Parse(txt_IPAddress.Text);
     string    path = txt_Send.Text;
     await Task.Factory.StartNew(() => NetworkCooperation.SendFile(path, 25000, ip),
                                 TaskCreationOptions.LongRunning);
 }
Example #3
0
        public MainWindow()
        {
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            InitializeComponent();
            string _networkName = "No";
            int    _pcNumber    = 0;

            lbl_YourIp.Content         = $"Your IP: {NetworkCooperation.GetMyLocalIPv4().ToString()}";
            lbl_Info.Content           = $"You are part of the \"{_networkName}\" network";
            lbl_PCaround.Content       = $"You have {_pcNumber}  PC active around";
            lbl_ThisPCHostName.Content = $"My Host Name \"{Dns.GetHostName()}\"";
            Console.WriteLine(System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName);
            //Console.WriteLine(System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString());
        }
Example #4
0
        private async void btn_TCPserver_Click(object sender, RoutedEventArgs e)
        {
            btn_TCPclient.IsEnabled = false;
            btn_TCPserver.IsEnabled = false;
            lbl_Info.Content        = "You are server now";

            if (String.IsNullOrWhiteSpace(txt_Port.Text))
            {
                if (String.IsNullOrEmpty(txt_Port.Text))
                {
                    txt_GeneralChat.Text = "You didn`t specify the port. Used default port for app: 25000";
                    //await Task.Factory.StartNew(() => NetworkCooperation.TcpConnectionServer(25000/*, NetworkCooperation.GetMyLocalIPv4()*/),
                    //                             TaskCreationOptions.LongRunning);
                    //await Task.Factory.StartNew(() => NetworkCooperation.SocketServerTCP(25000, NetworkCooperation.GetMyLocalIPv4().ToString()),
                    //                             TaskCreationOptions.LongRunning);
                }
            }

            else
            {
                #region Some Thread explanation
                //This code:
                // Thread t_PerthOut = new Thread(new ThreadStart(ReadCentralOutQueue("test"));
                //tries to call ReadCentralOutQueue and then create a delegate from the result.That isn't going to work, because it's a void method. Normally you'd use a method group to create a delegate, or an anonymous function such as a lambda expression. In this case a lambda expression will be easiest:
                //Thread t_PerthOut = new Thread(() => ReadCentralOutQueue("test"));
                //You can't just use new Thread(ReadCentralOutQueue) as the ReadCentralOutQueue doesn't match the signature for either ThreadStart or ParameterizedThreadStart.
                //******************************
                //In WPF, only the thread that created a DispatcherObject may access that object.For example,
                //a background thread that is spun off from the main UI thread cannot update the contents of
                //a Button that was created on the UI thread.In order for the background thread to access
                //the Content property of the Button, the background thread must delegate the work to the
                //Dispatcher associated with the UI thread.This is accomplished by using either Invoke or
                //BeginInvoke. Invoke is synchronous and BeginInvoke is asynchronous.The operation is added
                //to the event queue of the Dispatcher at the specified DispatcherPriority.
                //******************************
                // that is why this won`t work in WPF
                //Thread myThread = new Thread(()=>NetworkCooperation.TcpConnectionServer(Int32.Parse(txt_Port.Text), NetworkCooperation.GetMyLocalIPv4()));
                //myThread.Start(); // запускаем поток
                //******************************
                //Since.NET 4.5 and C# 5.0 you should use Task-based Asynchronous Pattern (TAP) along with async-await keywords in all areas (including the GUI):
                //TAP is the recommended asynchronous design pattern for new development


                #endregion

                #region эксперименты
                //Thread myThread = new Thread(()=>NetworkCooperation.TcpConnectionServer(Int32.Parse(txt_Port.Text), NetworkCooperation.GetMyLocalIPv4()));
                //myThread.Start(); // запускаем поток

                //this.Dispatcher.Invoke((Action)(() =>
                //{
                //    NetworkCooperation.TcpConnectionServer(Int32.Parse(txt_Port.Text), NetworkCooperation.GetMyLocalIPv4());
                //}));


                //Dispatcher.BeginInvoke(DispatcherPriority.Normal, new ThreadStart(() =>
                //{
                //    NetworkCooperation.TcpConnectionServer(Int32.Parse(txt_Port.Text), NetworkCooperation.GetMyLocalIPv4());
                //}));

                //await Dispatcher.BeginInvoke(DispatcherPriority.Normal, new ThreadStart(() =>
                //{
                //    NetworkCooperation.TcpConnectionServer(Int32.Parse(txt_Port.Text), NetworkCooperation.GetMyLocalIPv4());

                //}));

                #endregion

                var port = Int32.Parse(txt_Port.Text);
                ////var progress = new Progress<string>(s => label.Text = s);
                //await Task.Factory.StartNew(() => NetworkCooperation.TcpConnectionServer(port/*, NetworkCooperation.GetMyLocalIPv4()*/),
                //                                  TaskCreationOptions.LongRunning);

                await Task.Factory.StartNew(() => NetworkCooperation.SocketServerTCP(port, NetworkCooperation.GetMyLocalIPv4().ToString()),
                                            TaskCreationOptions.LongRunning);

                //Console.WriteLine();

                //await Task.Run(()=> Dispatcher.Invoke(() =>
                //{
                //    NetworkCooperation.SocketServerTCP(Int32.Parse(txt_Port.Text), NetworkCooperation.GetMyLocalIPv4().ToString());
                //}));

                //var task = Task.Run(() =>
                //{

                //}); // решил проблему с вводом данных сразу в функцию но потерял ассинхронность, форма виснет
            }
        }
Example #5
0
 public Peer(string Id, string name) // на случай получения из БД
 {
     this.Id       = Id;
     this.UserName = name;
     IpAddress     = NetworkCooperation.GetMyLocalIPv4();
 }
Example #6
0
 public Peer(string name)
 {
     Id        = Guid.NewGuid().ToString();
     IpAddress = NetworkCooperation.GetMyLocalIPv4();
     UserName  = name;
 }
Example #7
0
 public MainWindow()
 {
     InitializeComponent();
     lbl_Yourip.Content = NetworkCooperation.GetMyLocalIPv4();
 }