private void Window_Loaded(object sender, RoutedEventArgs e) { // Получение конфигурационной информации из app.config string port = ConfigurationManager.AppSettings["port"]; string username = ConfigurationManager.AppSettings["username"]; string machineName = Environment.MachineName; string serviceUrl = null; // Установка заголовка окна this.Title = string.Format("P2P приложение - {0}", username); // Получение URL-адреса службы с использованием адресаIPv4 // и порта из конфигурационного файла foreach (IPAddress address in Dns.GetHostAddresses(Dns.GetHostName())) { if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { serviceUrl = string.Format("net.tcp://{0}:{1}/P2PService", address, port); break; } } // Выполнение проверки, не является ли адрес null if (serviceUrl == null) { // Отображение ошибки и завершение работы приложения MessageBox.Show(this, "Не удается определить адрес конечной точки WCF.", "Networking Error", MessageBoxButton.OK, MessageBoxImage.Stop); Application.Current.Shutdown(); } // Регистрация и запуск службы WCF localService = new P2PService(this, username); host = new ServiceHost(localService, new Uri(serviceUrl)); NetTcpBinding binding = new NetTcpBinding(); binding.Security.Mode = SecurityMode.None; host.AddServiceEndpoint(typeof(IP2PService), binding, serviceUrl); try { host.Open(); } catch (AddressAlreadyInUseException) { // Отображение ошибки и завершение работы приложения MessageBox.Show(this, "Не удается начать прослушивание, порт занят.", "WCF Error", MessageBoxButton.OK, MessageBoxImage.Stop); Application.Current.Shutdown(); } // Создание имени равноправного участника (пира) peerName = new PeerName("P2P Sample", PeerNameType.Unsecured); // Подготовка процесса регистрации имени равноправного участника в локальном облаке peerNameRegistration = new PeerNameRegistration(peerName, int.Parse(port)); peerNameRegistration.Cloud = Cloud.AllLinkLocal; // Запуск процесса регистрации peerNameRegistration.Start(); }
private void Window_Loaded(object sender, RoutedEventArgs e) { try { var port = ConfigurationManager.AppSettings["port"]; var username = ConfigurationManager.AppSettings["username"]; Title = $"P2P приложение - {username}"; var dns = Dns.GetHostAddresses(Dns.GetHostName()); var url = (from address in dns where address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork select $"net.tcp://{address}:{port}/P2PService").FirstOrDefault(); MessageBox.Show(url); if (url == null) { MessageBox.Show(this, "Не удается определить адрес конечной точки WCF.", "Networking Error", MessageBoxButton.OK, MessageBoxImage.Stop); Application.Current.Shutdown(); } _service = new P2PService(this, username); _host = new ServiceHost(_service, new Uri(url)); var binding = new NetTcpBinding { Security = { Mode = SecurityMode.None } }; _host.AddServiceEndpoint(typeof(IP2PService), binding, url); try { _host.Open(); } catch (AddressAlreadyInUseException) { MessageBox.Show(this, "Не удается начать прослушивание, порт занят.", "WCF Error", MessageBoxButton.OK, MessageBoxImage.Stop); Application.Current.Shutdown(); } _pn = new PeerName("0.P2P Sample"); _pnr = new PeerNameRegistration(_pn, int.Parse(port)) { Cloud = Cloud.Available, Comment = "Simple p2p app untitled" }; _pnr.Start(); MessageBox.Show("Started!"); } catch (Exception exception) { MessageBox.Show(exception.Message); MessageBox.Show(exception.StackTrace); } }
public Messager() { peerList = new List <PeerEntry>(); // Получение конфигурационной информации из app.config string port = ConfigurationManager.AppSettings["port"]; string username = ConfigurationManager.AppSettings["username"]; string machineName = Environment.MachineName; string serviceUrl = null; // Получение URL-адреса службы с использованием адресаIPv4 // и порта из конфигурационного файла foreach (IPAddress address in Dns.GetHostAddresses(Dns.GetHostName())) { if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { serviceUrl = string.Format("net.tcp://{0}:{1}/P2PService", address, port); break; } } // Выполнение проверки, не является ли адрес null if (serviceUrl == null) { // Отображение ошибки и завершение работы приложения throw new Exception("Networking Error - Не удается определить адрес конечной точки WCF"); } // Регистрация и запуск службы WCF localService = new P2PService(this, username); host = new ServiceHost(localService, new Uri(serviceUrl)); NetTcpBinding binding = new NetTcpBinding(); binding.Security.Mode = SecurityMode.None; host.AddServiceEndpoint(typeof(IP2PService), binding, serviceUrl); host.Open(); Console.WriteLine("Service {0} is opened", serviceUrl); // Создание имени равноправного участника (пира) string name = "P2P " + ConfigurationManager.AppSettings["username"]; peerName = new PeerName(name, PeerNameType.Unsecured); // Подготовка процесса регистрации имени равноправного участника в локальном облаке peerNameRegistration = new PeerNameRegistration(peerName, int.Parse(port)); peerNameRegistration.Cloud = Cloud.AllLinkLocal; // Запуск процесса регистрации peerNameRegistration.Start(); Console.WriteLine("Registration process is started"); }
private void Window_Loaded(object sender, RoutedEventArgs e) { // Получение конфигурационной информации из app.config string port = ConfigurationManager.AppSettings["port"]; string username = ConfigurationManager.AppSettings["username"]; string machineName = Environment.MachineName; string serviceUrl = null; statusPort.Content = "Порт: " + port; statusUsername.Content = "Имя пользователя: " + username; // Установка заголовка окна this.Title = string.Format("P2P приложение - {0}", username); // Получение URL-адреса службы с использованием адресаIPv4 // и порта из конфигурационного файла foreach (IPAddress address in Dns.GetHostAddresses(Dns.GetHostName())) { if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { serviceUrl = string.Format("net.tcp://{0}:{1}/P2PService", address, port); break; } } // Выполнение проверки, не является ли адрес null if (serviceUrl == null) { // Отображение ошибки и завершение работы приложения MessageBox.Show(this, "Не удается определить адрес конечной точки WCF.", "Networking Error", MessageBoxButton.OK, MessageBoxImage.Stop); Application.Current.Shutdown(); } // Регистрация и запуск службы WCF localService = new P2PService(this, username); host = new ServiceHost(localService, new Uri(serviceUrl)); NetTcpBinding binding = new NetTcpBinding(); binding.Security.Mode = SecurityMode.None; host.AddServiceEndpoint(typeof(IP2PService), binding, serviceUrl); try { host.Open(); hostopen = true; } catch (AddressAlreadyInUseException) { // Отображение ошибки и завершение работы приложения MessageBox.Show(this, "Не удается начать прослушивание, порт занят.", "WCF Error", MessageBoxButton.OK, MessageBoxImage.Stop); hostopen = false; Application.Current.Shutdown(); } if (hostopen) { // Создание имени равноправного участника (пира) peerName = new PeerName("P2P Sample" + new Guid().ToString(), PeerNameType.Unsecured); // Подготовка процесса регистрации имени равноправного участника в локальном облаке peerNameRegistration = new PeerNameRegistration(peerName, int.Parse(port)); peerNameRegistration.Cloud = Cloud.AllLinkLocal; // Запуск процесса регистрации peerNameRegistration.Start(); } }