private void CheckAsClient(IPAddress ip, string[] tcpPorts, string[] udpPorts) { EnableTcpUI(false); EnableUdpUI(false); checkBoxTcp.Enabled = false; checkBoxUdp.Enabled = false; ipTextBox.Enabled = false; modeComboBox.Enabled = false; string showText = null; if (checkBoxTcp.Checked) { ThreadSafePushNewMessage("Start as Tcp Client, Peer IP " + ip.ToString() + " Tcp ports " + String.Join(" ", tcpPorts)); TcpClients tclis = new TcpClients(ip); foreach (string item in tcpPorts) { tclis.AddPort(Convert.ToInt32(item)); } tclis.StartConnect(); List <int> exceptionalTcpPorts; while (!tclis.IsConnectFinished(out exceptionalTcpPorts)) { Thread.Sleep(500); } showText += "TCP Ports "; if (null != exceptionalTcpPorts && exceptionalTcpPorts.Count > 0) { foreach (int item in exceptionalTcpPorts) { showText += item.ToString(); showText += " "; } showText += " invalid!"; //MessageBox.Show(showText); } else { //MessageBox.Show(" valid."); showText += "ALL valid."; } } if (checkBoxUdp.Checked) { ThreadSafePushNewMessage("Start as Udp Client, Peer IP " + ip.ToString() + " Udp ports " + String.Join(" ", udpPorts)); showText += "\nUDP Ports "; UdpClients tclis = new UdpClients(ip); foreach (string item in udpPorts) { tclis.AddPort(Convert.ToInt32(item)); } tclis.StartConnect(); List <int> exceptionalPorts; while (!tclis.IsConnectFinished(out exceptionalPorts)) { Thread.Sleep(500); } if (null != exceptionalPorts && exceptionalPorts.Count > 0) { foreach (int item in exceptionalPorts) { showText += item.ToString(); showText += " "; } showText += " invalid!"; //MessageBox.Show(showText); } else { //MessageBox.Show("Ports valid."); showText += "ALL valid."; } } MessageBox.Show(showText); EnableTcpUI(true); EnableUdpUI(true); checkBoxTcp.Enabled = true; checkBoxUdp.Enabled = true; ipTextBox.Enabled = true; modeComboBox.Enabled = true; }
private void CheckAsServer(string[] tcpPorts, string[] udpPorts) { if (startStopButton.Text.Equals("Start")) { //goto start ThreadSafePushNewMessage("Start as Server, Tcp ports " + String.Join(" ", tcpPorts) + " Udp ports " + String.Join(" ", udpPorts)); startStopButton.Text = "Stop"; EnableTcpUI(false); EnableUdpUI(false); checkBoxTcp.Enabled = false; checkBoxUdp.Enabled = false; modeComboBox.Enabled = false; if (checkBoxTcp.Checked) { Debug.Assert(null == m_tcpListeners); m_tcpListeners = new TcpListeners(); m_tcpListeners.Attach(this); foreach (string item in tcpPorts) { m_tcpListeners.AddPort(Convert.ToInt32(item)); } m_tcpListeners.StartListen(); } if (checkBoxUdp.Checked) { Debug.Assert(null == m_udpServers); m_udpServers = new UdpServers(); m_udpServers.Attach(this); foreach (string item in udpPorts) { m_udpServers.AddPort(Convert.ToInt32(item)); } m_udpServers.StartListen(); } m_udpServersExceptionPortList = null; timer2CheckServerException.Enabled = true; } else { //goto stop ThreadSafePushNewMessage("Stop Server."); timer2CheckServerException.Enabled = false; m_udpServersExceptionPortList = null; if (checkBoxTcp.Checked) { m_tcpListeners.StopListen(); TcpClients tclis = new TcpClients(IPAddress.Loopback); TcpClients tclisv6 = new TcpClients(IPAddress.IPv6Loopback); foreach (string item in tcpPorts) { tclis.AddPort(Convert.ToInt32(item)); tclisv6.AddPort(Convert.ToInt32(item)); } tclis.StartConnect(); tclisv6.StartConnect(); List <int> exceptionalTcpPorts, exceptionalTcpPortsv6; while (!tclis.IsConnectFinished(out exceptionalTcpPorts) || !tclisv6.IsConnectFinished(out exceptionalTcpPortsv6)) { Thread.Sleep(500); } m_tcpListeners.WaitListensStop(); m_tcpListeners = null; } if (checkBoxUdp.Checked) { m_udpServers.StopListen(); UdpClients uclis = new UdpClients(IPAddress.Loopback); UdpClients uclisv6 = new UdpClients(IPAddress.IPv6Loopback); foreach (string item in udpPorts) { uclis.AddPort(Convert.ToInt32(item)); uclisv6.AddPort(Convert.ToInt32(item)); } uclis.StartConnect(); uclisv6.StartConnect(); List <int> exceptionalUdpPorts, exceptionalUdpPortsv6; while (!uclis.IsConnectFinished(out exceptionalUdpPorts) || !uclisv6.IsConnectFinished(out exceptionalUdpPortsv6)) { Thread.Sleep(500); } m_udpServers.WaitListensStop(); m_udpServers = null; } EnableTcpUI(true); EnableUdpUI(true); checkBoxTcp.Enabled = true; checkBoxUdp.Enabled = true; modeComboBox.Enabled = true; startStopButton.Text = "Start"; } }
static private void UdpConnectThreadProc(object data) { UdpClients udpClients = (UdpClients)data; IPEndPoint peerEndpoint = udpClients.PopEndpoint(); if (peerEndpoint.AddressFamily != udpClients.TargetIP.AddressFamily) { //ip stack not match udpClients.DoDecrease(); return; } UdpClient ucli = new UdpClient(udpClients.TargetIP.AddressFamily); Socket uSocket = ucli.Client; uSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 3000); Byte[] sendBytes = Encoding.ASCII.GetBytes(UdpClients.kAskQuestion); //Creates an IPEndPoint to record the IP Address and port number of the sender. // The IPEndPoint will allow you to read datagrams sent from any source. IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); try { //Send ucli.Send(sendBytes, sendBytes.Length, new IPEndPoint(udpClients.TargetIP, peerEndpoint.Port)); // Blocks until a message returns on this socket from a remote host. Byte[] receiveBytes = ucli.Receive(ref RemoteIpEndPoint); string returnData = Encoding.ASCII.GetString(receiveBytes); string msg = "(UDP Client Received) LocalEndPoint {" + ucli.Client.LocalEndPoint.ToString() + "}, RemoteEndPoint {" + RemoteIpEndPoint.ToString() + "}, msg-->{" + returnData.ToString() + "}"; Trace.WriteLine(msg); if (returnData.ToString().Contains(UdpClients.kAnswer)) { } else { udpClients.AddExceptionalPort(peerEndpoint.Port); } } catch (SocketException ex) { Trace.WriteLine("SocketErrorcode " + ex.ErrorCode + ", " + ex.ToString()); switch (ex.SocketErrorCode) { case SocketError.TimedOut: case SocketError.ConnectionReset: break; default: break; } udpClients.AddExceptionalPort(peerEndpoint.Port); } catch (Exception e) { Trace.WriteLine(e.ToString()); Debug.Assert(false); } finally { ucli.Close(); } udpClients.DoDecrease(); }