我对C#中有关套接字的OOP有一个共同的问题。它如下:
然而,任何调用方法的尝试都会返回上述内容。 我已尝试以下方法来修复它:
NetTools.cs:
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text.RegularExpressions; // this will be used to verify the syntax of the IP address entered.
namespace Aneas
{
public class NetTools
{
static bool ConfirmIPSyntax(string IPAddress)
{
Regex IPConfirmed = new Regex(@"\b(?:\d{1,3}\.){3}\d{1,3}\b");
return IPConfirmed.IsMatch(IPAddress);
}
/* static bool DNSResolution(string AddressToLookUp, out IPAddress ScanIPAddress)
{
ScanIPAddress = null;
IPHostEntry NameToIpAddress;
try
{
NameToIpAddress = Dns.EndGetHostEntry(AddressToLookUp);
}
catch(SocketException)
{
return false;
}
if(NameToIpAddress.AddressList.Length > 0)
{
ScanIPAddress = NameToIpAddress.AddressList[0];
return true;
}
return false;
}*/
static bool ScanPorts(IPAddress Address, int Port)
{
TcpClient Client = new TcpClient();
//int PortLength;
try
{
//Console.WriteLine("enter number of ports to scan.");
// PortLength = Console.Read();
// Port = new int[PortLength];
// for (int i = 0; i <= Port.Length; i++ )
Client.Connect(Address, Port);
NetworkStream ClientStream = Client.GetStream();
ClientStream.Close();
Client.Close();
}
catch (SocketException)
{
return false;
}
return true;
}
/*public void Ping()
{ }
public void TraceRoute()
{ }
public void NetEnumarator()
{ }*/
}
}
的Program.cs:
using System;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
namespace Aneas
{
public class Program
{
static void Main(string[] args)
{
NetTools Athena = new NetTools();
String ScanAddress;
IPAddress ScanIPAddress;
try
{
if (args.Length != 0)
ScanAddress = args[0];
else
ScanAddress = "127.0.0.1";
if (Athena.ConfirmIPSyntax(ScanAddress))
{
ScanIPAddress = IPAddress.Parse(ScanAddress);
}
else
Console.WriteLine("IP NOT FOUND");
Console.WriteLine("Port Scanning {0} ({1})", ScanAddress, ScanIPAddress.ToString());
for (int Port = IPEndPoint.MinPort; Port < IPEndPoint.MaxPort; Port++)
{
Console.Write("Scanning port {0} : ", Port);
if (Athena.ScanPorts(ScanIPAddress, Port))
Console.WriteLine("OPEN");
else
Console.WriteLine("closed");
}
Console.WriteLine("Finished scanning all ports");
}//end try
catch (Exception e)
{
Console.WriteLine("Exception caught!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
}//end static void main string[] args
}//end class
}//end name space
答案 0 :(得分:-1)
您无法从实例访问静态方法。
尝试这个(在公开之后):
Aneas.NetTools.ConfirmIPSyntax(ScanAddress)
Aneas.NetTools.ScanPorts(ScanIPAddress, Port)
或者你可以删除&#34;静态&#34;关于方法的关键字,以及上面的代码可以使用(在公开方法之后)。