C# fetching Active Ip address in LAN, this works fine in console application, wen it comes on form based application i getting errors, iam new to c# pls help. getting a error object reference for non-static field
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private static List<Ping> pingers = new List<Ping>();
private static int instances = 0;
private static object @lock = new object();
private static int result = 0;
private static int timeOut = 250;
private static int ttl = 5;
public String IP;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
IP = textBox1.Text;
string baseIP = "192.168.1.";
Console.WriteLine("Pinging 255 destinations of D-class in {0}*", baseIP);
CreatePingers(255);
PingOptions po = new PingOptions(ttl, true);
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
byte[] data = enc.GetBytes("abababababababababababababababab");
SpinWait wait = new SpinWait();
int cnt = 1;
Stopwatch watch = Stopwatch.StartNew();
foreach (Ping p in pingers)
{
lock (@lock)
{
instances += 1;
}
p.SendAsync(string.Concat(baseIP, cnt.ToString()), timeOut, data, po);
cnt += 1;
}
while (instances > 0)
{
wait.SpinOnce();
}
watch.Stop();
DestroyPingers();
Console.WriteLine("Finished in {0}. Found {1} active IP-addresses.", watch.Elapsed.ToString(), result);
Console.ReadKey();
}
public static void Ping_completed(object s, PingCompletedEventArgs e)
{
lock (@lock)
{
instances -= 1;
}
if (e.Reply.Status == IPStatus.Success)
{
IP = e.Reply.Address.ToString();
result += 1;
}
else
{
//Console.WriteLine(String.Concat("Non-active IP: ", e.Reply.Address.ToString()))
}
}
private static void CreatePingers(int cnt)
{
for (int i = 1; i <= cnt; i++)
{
Ping p = new Ping();
p.PingCompleted += Ping_completed;
pingers.Add(p);
}
}
private static void DestroyPingers()
{
foreach (Ping p in pingers)
{
p.PingCompleted -= Ping_completed;
p.Dispose();
}
pingers.Clear();
}
}
}
You use the IP "variable" as field in your class.
public String IP;
But you cannot access fields in static methods. As far as I can see there is no need for your 3 methods to be static. I guess this is the result of copy/pasting from the console application, where you coded your methods to be static :)
So you need either to:
I'd suggest you to make all members non-static, since they "belong" to the form.
之所以会出现此错误,是因为IP被声明为非静态字段并且正在以静态方法使用。
Finally this code works.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.NetworkInformation;
using System.Threading;
using System.Diagnostics;
using System.Net;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
private static List<Ping> pingers = new List<Ping>();
private static int instances = 0;
private static object @lock = new object();
private static int result = 0;
private static int timeOut = 250;
private static int ttl = 5;
private static string[] IPs=new string[100];
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string baseIP = "192.168.1.";
IP.Items.Add("Pinging 255 destinations of D-class in " + baseIP+"xxx");
CreatePingers(255);
PingOptions po = new PingOptions(ttl, true);
System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
byte[] data = enc.GetBytes("abababababababababababababababab");
SpinWait wait = new SpinWait();
int cnt = 1;
Stopwatch watch = Stopwatch.StartNew();
foreach (Ping p in pingers)
{
lock (@lock)
{
instances += 1;
}
p.SendAsync(string.Concat(baseIP, cnt.ToString()), timeOut, data, po);
cnt += 1;
}
}
public static void Ping_completed(object s, PingCompletedEventArgs e)
{
lock (@lock)
{
instances -= 1;
}
if (e.Reply.Status == IPStatus.Success)
{
Console.WriteLine(string.Concat("Active IP: ", e.Reply.Address.ToString()));
IPs[result] = e.Reply.Address.ToString();
result += 1;
}
else
{
//Console.WriteLine(String.Concat("Non-active IP: ", e.Reply.Address.ToString()))
}
}
private static void CreatePingers(int cnt)
{
for (int i = 1; i <= cnt; i++)
{
Ping p = new Ping();
p.PingCompleted += Ping_completed;
pingers.Add(p);
}
}
private static void DestroyPingers()
{
foreach (Ping p in pingers)
{
p.PingCompleted -= Ping_completed;
p.Dispose();
}
pingers.Clear();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.