1
我上加載一組打印機從文本文件的應用程序工作:尋找最接近的打印機到客戶端的網絡
protected void LoadPrinterList()
{
string CSVFilePathName = System.Configuration.ConfigurationManager.AppSettings["FilePath"];
string[] Lines = File.ReadAllLines(CSVFilePathName);
string[] Fields;
Fields = Lines[0].Split(new char[] { ',' });
int Cols = Fields.GetLength(0);
DataTable dt = new DataTable();
//1st row must be column names; force lower case to ensure matching later on.
for (int i = 0; i < Cols; i++)
dt.Columns.Add(Fields[i].ToLower(), typeof(string));
dt.Columns.Add("nameanddescription", typeof(string), "name +'-'+ description");
dt.Columns.Add("ipandconnectionstring", typeof(string), "ip +'-'+ ConncetionStringID");
DataRow Row;
for (int i = 1; i < Lines.GetLength(0); i++)
{
Fields = Lines[i].Split(new char[] { ',' });
Row = dt.NewRow();
for (int f = 0; f < Cols; f++)
Row[f] = Fields[f];
dt.Rows.Add(Row);
}
string hostname = Request.UserHostName.Substring(0, 3);
string[] name = Printerlist.SelectedValue.Split('-');
//string plant = name[0].ToString();
//string plantvalue = plant.Substring(0, 3);
//if(hostname == plantvalue)
//{
Printerlist.DataTextField = "nameanddescription";
Printerlist.DataValueField = "ipandconnectionstring";
//}
Printerlist.DataSource = dt;
Printerlist.DataBind();
}
問題是這些打印機中大多數都在工廠和我需要找到最接近的標籤打印機給客戶,我能獲得客戶信息:
public static string DetermineCompName(string IP)
{
IPAddress myIP = IPAddress.Parse(IP);
IPHostEntry GetIPHost = Dns.GetHostEntry(myIP);
List<string> compName = GetIPHost.HostName.ToString().Split('.').ToList();
return compName.First();
}
string IP = Request.UserHostName;
string compName = CompNameHelper.DetermineCompName(IP);
我也有每個打印機的IP地址,因爲我打印使用的IP地址。我的問題是,如何基於客戶端的IP和打印機的IP找到最近的那個?
有沒有其他解決方法?我看了很多網上,但無法找到足夠的信息。
簡單的答案 - 你不能,通常沒有距離和IP地址之間的關係,特別是在內部局域網 –
我可以想到的唯一可能的事情是平均每一次,然後取平均ping,然後無論哪一個是最低的,你可以猜一個是最接近的,但幾乎每次都可能是錯的。 – pay
我懷疑會有辦法做到這一點http://superuser.com/questions/ 327417/is-it-it-possible-to-find-a-printers-physical-location-in-a-building-by-its-ip-adr –