private void NamePingerStart() { try { namePingerListener.Start(); try { while (Running) { TcpClient client = namePingerListener.AcceptTcpClient(); if (BannedIPs.Contains((client.Client.RemoteEndPoint as IPEndPoint).Address.ToString()) && Properties.Settings.Default.BlindBannedContacts) { client.Client.Send(Helpers.GetBytes(StaticPenises.BannedRefuseName, sizeof(char) * 128)); } else { client.Client.Send(Helpers.GetBytes(Name, sizeof(char) * 128)); } Thread.Sleep(100); client.Close(); } } catch (SocketException e) { if (Running) { Console.WriteLine(e.Message); Restart(); } } } catch (SocketException e) { MessageBox.Show(e.Message + "\nProgram may not be able to receive files.", "Unable to bind to port " + StaticPenises.NamePingPort); Console.WriteLine(e.Message + " port " + StaticPenises.NamePingPort); } }
private void PortListenerStart() { try { portCommListener.Start(); try { while (Running) { try { TcpClient client = portCommListener.AcceptTcpClient(); IPEndPoint remoteEndpoint = (client.Client.RemoteEndPoint as IPEndPoint); // Abort receiving if banned if (BannedIPs.Contains(remoteEndpoint.Address.ToString())) { Console.WriteLine("Refusing connection because IP " + client.Client.RemoteEndPoint.ToString() + " is banned"); client.Client.Send(BitConverter.GetBytes(-1)); throw new IPBannedException(remoteEndpoint.Address); } int receivedPort = 1; int finalPort = 0; while (receivedPort != 0) { byte[] portBuffer = new byte[sizeof(int)]; client.Client.Receive(portBuffer); receivedPort = BitConverter.ToInt32(portBuffer, 0); if (receivedPort == 0) { Tasks.Dispatcher.Invoke(() => StartNewTask(IPAddress.Any, finalPort)); } finalPort = 0; bool localFree = false; while (!localFree) { if (!usedPorts.Contains(receivedPort)) { client.Client.Send(BitConverter.GetBytes(receivedPort)); finalPort = receivedPort; localFree = true; } else { receivedPort++; } } } } catch (IPBannedException e) { Console.WriteLine(e.Message); } } } catch (SocketException e) { if (Running) { Console.WriteLine(e.Message); Restart(); } } } catch (SocketException e) { MessageBox.Show(e.Message + "\nProgram may not be able to receive files.", "Unable to bind to port " + StaticPenises.MainPort); Console.WriteLine(e.Message + " port " + StaticPenises.MainPort); } }