【发布时间】:2016-04-11 20:43:43
【问题描述】:
我正在开发一个从文本文件加载一组打印机的应用程序:
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 地址之间没有关系,尤其是在内部 LAN 中
-
我能想到的唯一可能的事情是对每个 ping 几次,然后取所有 ping 的平均值,然后无论哪一个是最低的,你可以猜出最接近的一个,但可能几乎每次都错了..
-
我怀疑会有办法做到这一点superuser.com/questions/327417/…
-
我想您可以将客户端和打印机的静态 IP 地址与坐标相关联,并编写一个脚本来确定客户端和所有打印机之间的距离,然后选择最近的 IP。然而,维护所有这些坐标会很痛苦。
标签: c# asp.net printing client zebra-printers