static void Main(string[] args) { IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties(); Console.WriteLine("Domain Name : " + properties.DomainName); Console.WriteLine("Host Name : " + properties.HostName); //Get Active TCP Connections TcpConnectionInformation[] connections = properties.GetActiveTcpConnections(); //Get Active TCP Listener IPEndPoint[] endPointsTCP = properties.GetActiveTcpListeners(); //Get Active UDP Listener IPEndPoint[] endPointsUDP = properties.GetActiveUdpListeners(); //Get IP statistics information IPGlobalStatistics ipstat = properties.GetIPv4GlobalStatistics(); //Get TCP statistical information TcpStatistics tcpstat = properties.GetTcpIPv4Statistics(); //Get UDP statistical information UdpStatistics udpStat = properties.GetUdpIPv4Statistics(); }
//</Snippet2> //<Snippet3> public static void ShowUdpStatistics(NetworkInterfaceComponent version) { IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties(); UdpStatistics udpStat = null; switch (version) { case NetworkInterfaceComponent.IPv4: udpStat = properties.GetUdpIPv4Statistics(); Console.WriteLine("UDP IPv4 Statistics"); break; case NetworkInterfaceComponent.IPv6: udpStat = properties.GetUdpIPv6Statistics(); Console.WriteLine("UDP IPv6 Statistics"); break; default: throw new ArgumentException("version"); // break; } Console.WriteLine(" Datagrams Received ...................... : {0}", udpStat.DatagramsReceived); Console.WriteLine(" Datagrams Sent .......................... : {0}", udpStat.DatagramsSent); Console.WriteLine(" Incoming Datagrams Discarded ............ : {0}", udpStat.IncomingDatagramsDiscarded); Console.WriteLine(" Incoming Datagrams With Errors .......... : {0}", udpStat.IncomingDatagramsWithErrors); Console.WriteLine(" UDP Listeners ........................... : {0}", udpStat.UdpListeners); Console.WriteLine(""); }
/// <summary> /// Set the TCP statistics fields /// </summary> private void DumpStats() { IPGlobalProperties stats = IPGlobalProperties.GetIPGlobalProperties(); TcpStatistics tcpstat = stats.GetTcpIPv4Statistics(); UdpStatistics udpstat = stats.GetUdpIPv4Statistics(); // set labels // this doesn't need to be done each time setLabels(); // set the TCP field labels connAcceptField.Text = tcpstat.ConnectionsAccepted.ToString(); connInitiatedField.Text = tcpstat.ConnectionsInitiated.ToString(); cumulativeConnectionsField.Text = tcpstat.CumulativeConnections.ToString(); errorsReceivedField.Text = tcpstat.ErrorsReceived.ToString(); failedConAttField.Text = tcpstat.FailedConnectionAttempts.ToString(); maxConnField.Text = tcpstat.MaximumConnections.ToString(); resetConnField.Text = tcpstat.ResetConnections.ToString(); resetsSentField.Text = tcpstat.ResetsSent.ToString(); segsReceivedField.Text = tcpstat.SegmentsReceived.ToString(); segsResentField.Text = tcpstat.SegmentsResent.ToString(); segsSentField.Text = tcpstat.SegmentsSent.ToString(); // set the UDP field labels dataReceivedField.Text = udpstat.DatagramsReceived.ToString(); dataSentField.Text = udpstat.DatagramsSent.ToString(); incDataDiscField.Text = udpstat.IncomingDatagramsDiscarded.ToString(); incDataErrField.Text = udpstat.IncomingDatagramsWithErrors.ToString(); }
/// <summary> /// Reset statistics counters /// </summary> public void Reset() { Start = NetTime.Now; MessagesSent = 0; UserMessagesSent = 0; AckMessagesSent = 0; MessagesReceived = 0; UserMessagesReceived = 0; AckMessagesReceived = 0; PacketsSent = 0; PacketsReceived = 0; BytesSent = 0; BytesReceived = 0; MessagesResent = 0; try { IPGlobalProperties ipProps = IPGlobalProperties.GetIPGlobalProperties(); UdpStatistics udpStats = ipProps.GetUdpIPv4Statistics(); m_sysReceived = udpStats.DatagramsReceived; m_sysSent = udpStats.DatagramsSent; } catch (NotSupportedException) { // IPGlobalProperties is not support on non-Windows platforms! } }
private static void PrintUdp6Statistics() { UdpStatistics stats = IPGlobalProperties.GetIPGlobalProperties().GetUdpIPv6Statistics(); Console.WriteLine("--UDPv6 Statistics--"); PrintProperties(stats); }
private static List <PortInfo> GetOpenPort() { IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties(); IPEndPoint[] tcpEndPoints = properties.GetActiveTcpListeners(); IPEndPoint[] udpEndPoints = properties.GetActiveUdpListeners(); TcpConnectionInformation[] tcpConnections = properties.GetActiveTcpConnections(); UdpStatistics udpStatistics = properties.GetUdpIPv4Statistics(); List <PortInfo> portInfos = tcpConnections.Select(p => { return(new TcpPortInfo( p.LocalEndPoint.Port, p.LocalEndPoint.Address.ToString(), p.RemoteEndPoint.Address + ":" + p.RemoteEndPoint.Port, p.State.ToString())); }).Cast <PortInfo>().ToList(); portInfos.AddRange(udpEndPoints.Select(p => { return(new UdpPortInfo(p.Port)); })); return(portInfos); }
public async Task GetNetworkStatsAsync() { IUserMessage response; EmbedBuilder initialBuilder; initialBuilder = EmbedHelper.GetDefaultEmbed(Context); initialBuilder.AddField(":computer: Gathering Network Statistics", "Please wait..."); response = await ReplyAsync("", embed : initialBuilder.Build()); try { EmbedBuilder embed; IPGlobalProperties properties; UdpStatistics udpStat = null; embed = EmbedHelper.GetDefaultEmbed(Context); properties = IPGlobalProperties.GetIPGlobalProperties(); udpStat = properties.GetUdpIPv4Statistics(); embed.AddField("Datagrams Received", udpStat.DatagramsReceived.ToString("#,##0"), inline: true); embed.AddField("Datagrams Sent", udpStat.DatagramsSent.ToString("#,##0"), inline: true); embed.AddField("Datagrams Discarded", udpStat.IncomingDatagramsDiscarded.ToString("#,##0")); embed.AddField("Datagrams With Errors", udpStat.IncomingDatagramsWithErrors.ToString("#,##0")); embed.AddField("UDP Listeners", udpStat.UdpListeners.ToString("#,##0")); await response.ModifyAsync(msg => { msg.Embed = embed.Build(); msg.Content = ""; }); } catch (Exception e) { await response.ModifyAsync(msg => msg = ErrorHandler.GetDefaultErrorMessageEmbed(e, msg, Context.Message)); } }
private string GetUdpStatistics( NetworkInterfaceComponent version, IPGlobalProperties properties) { UdpStatistics udpStat = null; StringBuilder udpStatBuilder = new StringBuilder(); try { switch (version) { case NetworkInterfaceComponent.IPv4: udpStat = properties.GetUdpIPv4Statistics(); udpStatBuilder.Append("UDP IPv4 Statistics\n"); break; case NetworkInterfaceComponent.IPv6: udpStat = properties.GetUdpIPv6Statistics(); udpStatBuilder.Append("UDP IPv6 Statistics\n"); break; default: throw new ArgumentException("No such IP version"); } } catch (NetworkInformationException) { return(String.Empty); } udpStatBuilder.Append("=================================================\n"); udpStatBuilder.AppendFormat(" Datagrams Received ............ : {0}\n", udpStat.DatagramsReceived); udpStatBuilder.AppendFormat(" Datagrams Sent ................ : {0}\n", udpStat.DatagramsSent); udpStatBuilder.AppendFormat(" Incoming Datagrams Discarded .. : {0}\n", udpStat.IncomingDatagramsDiscarded); udpStatBuilder.AppendFormat(" Incoming Datagrams With Errors : {0}\n", udpStat.IncomingDatagramsWithErrors); udpStatBuilder.AppendFormat(" UDP Listeners ................. : {0}\n", udpStat.UdpListeners); IPEndPoint[] endPoints = properties.GetActiveUdpListeners(); if (endPoints.Length > 0) { udpStatBuilder.Append(" Local Address Port\n"); } foreach (IPEndPoint endPoint in endPoints) { udpStatBuilder.AppendFormat(" {0, -15}:{1}\n", endPoint.Address, endPoint.Port); } return(udpStatBuilder.ToString()); }
public UDPNetworkStatistics() { IPGlobalProperties IPproperties = IPGlobalProperties.GetIPGlobalProperties(); UdpStatistics udpNetworkStats = IPproperties.GetUdpIPv4Statistics(); this.DatagramsReceived = udpNetworkStats.DatagramsReceived; this.DatagramsSent = udpNetworkStats.DatagramsSent; this.IncomingDatagramsDiscarded = udpNetworkStats.IncomingDatagramsDiscarded; this.IncomingDatagramsWithErrors = udpNetworkStats.IncomingDatagramsWithErrors; this.UDPListeners = udpNetworkStats.UdpListeners; }
/// <summary> /// Dump all statistics to log /// </summary> public void DumpToLog(NetLog log) { if (log == null) { return; } double timespan; if (End == 0.0f) { timespan = NetTime.Now - Start; } else { timespan = End - Start; } log.Debug("--- Statistics for " + m_forConnection + " @ " + NetTime.Now + "--------------"); log.Debug("Timespan: " + timespan + " seconds"); log.Debug("Sent " + PacketsSent + " packets (" + FPS(PacketsSent, timespan) + " per second)"); log.Debug("Sent " + BytesSent + " bytes (" + FPS(BytesSent, timespan) + " per second, " + FPS(BytesSent, (double)PacketsSent) + " per packet, " + FPS(BytesSent, (double)MessagesSent) + " per message)"); log.Debug("Sent " + MessagesSent + " messages (" + FPS(MessagesSent, timespan) + " per second, " + FPS(MessagesSent, (double)PacketsSent) + " per packet)"); int libMessagesSent = MessagesSent - (UserMessagesSent + AckMessagesSent); log.Debug(" ... of which " + UserMessagesSent + " were user messages (" + FPS(UserMessagesSent, timespan) + " per second)"); log.Debug(" ... of which " + AckMessagesSent + " were acknowledge messages (" + FPS(AckMessagesSent, timespan) + " per second)"); log.Debug(" ... of which " + libMessagesSent + " were other system messages (" + FPS(libMessagesSent, timespan) + " per second)"); log.Debug("Received " + PacketsReceived + " packets (" + FPS(PacketsReceived, timespan) + " per second)"); log.Debug("Received " + BytesReceived + " bytes (" + FPS(BytesReceived, timespan) + " per second, " + FPS(BytesReceived, (double)PacketsReceived) + " per packet, " + FPS(BytesReceived, (double)MessagesReceived) + " per message)"); log.Debug("Received " + MessagesReceived + " messages (" + FPS(MessagesReceived, timespan) + " per second, " + FPS(MessagesReceived, (double)PacketsReceived) + " per packet)"); int libMessagesReceived = MessagesReceived - (UserMessagesReceived + AckMessagesReceived); log.Debug(" ... of which " + UserMessagesReceived + " were user messages (" + FPS(UserMessagesReceived, timespan) + " per second)"); log.Debug(" ... of which " + AckMessagesReceived + " were acknowledge messages (" + FPS(AckMessagesReceived, timespan) + " per second)"); log.Debug(" ... of which " + libMessagesReceived + " were other system messages (" + FPS(libMessagesReceived, timespan) + " per second)"); log.Debug("Resent " + MessagesResent + " messages (" + FPS(MessagesResent, timespan) + " per second)"); try { // windows specific stuff IPGlobalProperties ipProps = IPGlobalProperties.GetIPGlobalProperties(); UdpStatistics udpStats = ipProps.GetUdpIPv4Statistics(); log.Debug("System wide datagrams sent: " + (udpStats.DatagramsSent - m_sysSent)); log.Debug("System wide datagrams received: " + (udpStats.DatagramsReceived - m_sysReceived)); } catch (NotSupportedException) { } }
//初始化已发送、已接收和丢失的数据报 private void Form1_Load(object sender, EventArgs e) { if (blFlag == true) { IPGlobalProperties NetInfo = IPGlobalProperties.GetIPGlobalProperties(); UdpStatistics myUdpStat = null; myUdpStat = NetInfo.GetUdpIPv4Statistics(); SendNum1 = Int32.Parse(myUdpStat.DatagramsSent.ToString()); ReceiveNum1 = Int32.Parse(myUdpStat.DatagramsReceived.ToString()); DisNum1 = Int32.Parse(myUdpStat.IncomingDatagramsDiscarded.ToString()); } }
private void uDPIPv4СтатистикаToolStripMenuItem1_Click(object sender, EventArgs e) { IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties(); UdpStatistics udpStat = properties.GetUdpIPv6Statistics(); StatTextBox.Text = "Статисктика UDP для IPv6:" + "\r\r"; StatTextBox.Text += "Получено датаграмм: " + udpStat.DatagramsReceived + "\r"; StatTextBox.Text += "Отправлено датаграмм: " + udpStat.DatagramsSent + "\r"; StatTextBox.Text += "Число датаграмм, удаленных из-за ошибки порта: " + udpStat.IncomingDatagramsDiscarded + "\r"; StatTextBox.Text += "Число датаграмм, удаленных не из-за ошибки порта: " + udpStat.IncomingDatagramsWithErrors + "\r"; StatTextBox.Text += "Число серверов, прослушивающих датаграммы: " + udpStat.UdpListeners; }
//发送信息 private void button2_Click(object sender, EventArgs e) { if (ISPort == true)//判断是否有打开的端口号 { IPGlobalProperties NetInfo = IPGlobalProperties.GetIPGlobalProperties(); UdpStatistics myUdpStat = null; myUdpStat = NetInfo.GetUdpIPv4Statistics(); try { if (blFlag == false)//非第一次发送 { SendNum2 = Int32.Parse(myUdpStat.DatagramsSent.ToString()); ReceiveNum2 = Int32.Parse(myUdpStat.DatagramsReceived.ToString()); DisNum2 = Int32.Parse(myUdpStat.IncomingDatagramsDiscarded.ToString()); textBox1.Text = Convert.ToString(SendNum2 - SendNum3); textBox2.Text = Convert.ToString(ReceiveNum2 - ReceiveNum3); textBox3.Text = Convert.ToString(DisNum2 - DisNum3); } SendNum2 = Int32.Parse(myUdpStat.DatagramsSent.ToString()); ReceiveNum2 = Int32.Parse(myUdpStat.DatagramsReceived.ToString()); DisNum2 = Int32.Parse(myUdpStat.IncomingDatagramsDiscarded.ToString()); SendNum3 = SendNum2; //记录本次的发送数据报 ReceiveNum3 = ReceiveNum2; //记录本次的接收数据报 DisNum3 = DisNum2; //记录本次的丢失数据报 if (blFlag == true) //第一次发送 { textBox1.Text = Convert.ToString(SendNum2 - SendNum1); textBox2.Text = Convert.ToString(ReceiveNum2 - ReceiveNum1); textBox3.Text = Convert.ToString(DisNum2 - DisNum1); blFlag = false; } } catch (Exception ex) { MessageBox.Show(ex.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } string str = EncryptDES(rtbSend.Text, "mrsoftxk");//加密要发送的信息 data = Encoding.Unicode.GetBytes(str); mySocket.SendTo(data, data.Length, SocketFlags.None, Client); rtbSend.Text = ""; } else { MessageBox.Show("请首先打开端口!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); button4.Focus(); } }
// Display the data for given UdpStatistics object. private static void DisplayUdpStatistics(UdpStatistics udpStat) { Console.WriteLine( equalsFormatString, "Datagrams Received", udpStat.DatagramsReceived); Console.WriteLine( equalsFormatString, "No Ports", udpStat.IncomingDatagramsDiscarded); Console.WriteLine( equalsFormatString, "Receive Errors", udpStat.IncomingDatagramsWithErrors); Console.WriteLine( equalsFormatString, "Datagrams Sent", udpStat.DatagramsSent); }
public UdpStatisticsEventArgs(UdpStatistics statistics) { this.UdpStatistics = statistics; }