修改tcp的ip选择逻辑
This commit is contained in:
commit
280062d86c
3 files changed
+64
-57
No files matched your search
+6
-3
@@ -74,7 +74,8 @@ namespace formula_manage
|
||||
InitializeComponent();
|
||||
this.Closing += Window_Closing; //添加窗口关闭事件
|
||||
|
||||
UserClass.TCPServer.Start(); //开启网路通讯
|
||||
UserClass.IniFile.IniFiles Configini = new UserClass.IniFile.IniFiles(INIPath);
|
||||
if (Boolean.Parse(Configini.IniReadvalue("SOFTWARE_SET", "L6"))) UserClass.TCPServer.Start(); //开启网路通讯
|
||||
|
||||
USER.Text = App.USER_Purview;
|
||||
RRODUCTdataTable.Columns.Add("ID", typeof(int));
|
||||
@@ -197,6 +198,7 @@ namespace formula_manage
|
||||
catch (Exception)
|
||||
{
|
||||
System.Windows.MessageBox.Show("请求信息失败,检查连接");
|
||||
UserClass.LogGing.LogGingDATA("DatabaseConnectionFailed");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -258,8 +260,8 @@ namespace formula_manage
|
||||
}
|
||||
}
|
||||
|
||||
// UserClass.LogGing.LogGingDATA(Log_time + "HttpSERVER_STOP");
|
||||
// UserClass.HttpSERVER.Stop(); //停止网络通信
|
||||
// UserClass.LogGing.LogGingDATA(Log_time + "HttpSERVER_STOP");
|
||||
//if (Boolean.Parse(Configini.IniReadvalue("SOFTWARE_SET", "L6"))) UserClass.HttpSERVER.Stop(); //停止网络通信
|
||||
UserClass.LogGing.LogGingDATA("FORMULA_STOP");
|
||||
}
|
||||
private void SearchBox_OnKeyDownd(object sender, System.Windows.Input.KeyEventArgs e) //回车跳转功能
|
||||
@@ -502,6 +504,7 @@ namespace formula_manage
|
||||
catch (Exception)
|
||||
{
|
||||
System.Windows.MessageBox.Show("数据访问失败,重试");
|
||||
UserClass.LogGing.LogGingDATA("DatabaseConnectionFailed");
|
||||
return;
|
||||
} //单号重复检查
|
||||
if (Machine.Text == "")
|
||||
|
||||
+39
-54
@@ -25,68 +25,56 @@ namespace formula_manage.UserClass
|
||||
public static Dictionary<string, Socket> OnLineList = new Dictionary<string, Socket>();
|
||||
|
||||
//当前时间
|
||||
private string CurrentTime
|
||||
{
|
||||
get { return DateTime.Now.ToString("HH:mm:ss") + Environment.NewLine; }
|
||||
}
|
||||
|
||||
private string CurrentTime
|
||||
{
|
||||
get { return DateTime.Now.ToString("HH:mm:ss") + Environment.NewLine; }
|
||||
}
|
||||
|
||||
//编码格式
|
||||
Encoding econding = Encoding.Default;
|
||||
Encoding econding = Encoding.Default;
|
||||
|
||||
|
||||
public static void Start()
|
||||
{
|
||||
public static void Start()
|
||||
{
|
||||
//第一步:调用socket()函数创建一个用于通信的套接字
|
||||
listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
//第二步:给已经创建的套接字绑定一个端口号,这一般通过设置网络套接口地址和调用Bind()函数来实现
|
||||
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(UserClass.get_local_ip_address.getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.Ethernet)), int.Parse("11080"));
|
||||
|
||||
try
|
||||
{
|
||||
listenSocket.Bind(endPoint);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show("服务器开启失败:" + ex.Message, "开启服务器");
|
||||
return;
|
||||
}
|
||||
|
||||
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(UserClass.get_local_ip_address.IP_Address()), int.Parse("11080"));
|
||||
UserClass.LogGing.LogGingDATA("NativeIP = "+ UserClass.get_local_ip_address.IP_Address());
|
||||
UserClass.LogGing.LogGingDATA("NativePORT = 11080");
|
||||
try
|
||||
{
|
||||
listenSocket.Bind(endPoint);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
UserClass.LogGing.LogGingDATA("TCP_NotStart");
|
||||
UserClass.LogGing.LogGingDATA(ex.Message);
|
||||
return;
|
||||
}
|
||||
//第三步:调用listen()函数使套接字成为一个监听套接字
|
||||
listenSocket.Listen(10);
|
||||
|
||||
listenSocket.Listen(10);
|
||||
//ShowMessage("服务器开启成功");
|
||||
|
||||
//开启一个线程监听
|
||||
Task.Run(new Action(() =>
|
||||
{
|
||||
ListenConnection();
|
||||
}
|
||||
));
|
||||
|
||||
}
|
||||
Task.Run(new Action(() =>{ListenConnection();} ));
|
||||
}
|
||||
|
||||
public static void ListenConnection()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
Socket clientSocket = listenSocket.Accept();
|
||||
|
||||
string ip = clientSocket.RemoteEndPoint.ToString();
|
||||
|
||||
Socket clientSocket = listenSocket.Accept();
|
||||
string ip = clientSocket.RemoteEndPoint.ToString();
|
||||
//更新在线列表
|
||||
// AddOnLine(ip, true);
|
||||
//更新在线列表集合
|
||||
OnLineList.Add(ip, clientSocket);
|
||||
|
||||
// ShowMessage(ip + "上线了");
|
||||
|
||||
Task.Run(() => ReceiveMsg(clientSocket));
|
||||
|
||||
}
|
||||
|
||||
OnLineList.Add(ip, clientSocket);
|
||||
UserClass.LogGing.LogGingDATA("IP_Online = "+ ip);
|
||||
// ShowMessage(ip + "上线了");
|
||||
Task.Run(() => ReceiveMsg(clientSocket));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 接收方法
|
||||
@@ -106,15 +94,13 @@ namespace formula_manage.UserClass
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
|
||||
//客户端下线了
|
||||
|
||||
//更新在线列表
|
||||
string ip = clientSocket.RemoteEndPoint.ToString();
|
||||
UserClass.LogGing.LogGingDATA("IP_Line = " + ip);
|
||||
// AddOnLine(ip, false);
|
||||
|
||||
// AddOnLine(ip, false);
|
||||
|
||||
OnLineList.Remove(ip);
|
||||
OnLineList.Remove(ip);
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -122,13 +108,12 @@ namespace formula_manage.UserClass
|
||||
if (length == 0)
|
||||
{
|
||||
//客户端下线了
|
||||
|
||||
//更新在线列表
|
||||
string ip = clientSocket.RemoteEndPoint.ToString();
|
||||
|
||||
UserClass.LogGing.LogGingDATA("IP_Line = " + ip);
|
||||
// AddOnLine(ip, false);
|
||||
|
||||
OnLineList.Remove(ip);
|
||||
OnLineList.Remove(ip);
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -29,5 +29,24 @@ namespace formula_manage.UserClass
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
public static string IP_Address()
|
||||
{
|
||||
string ip;
|
||||
|
||||
ip = getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.Ethernet);
|
||||
if(ip=="")ip = getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.Wireless80211);
|
||||
if (ip == "") ip = getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.Ppp);
|
||||
if (ip == "") ip = getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.Wwanpp);
|
||||
if (ip == "") ip = getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.TokenRing);
|
||||
if (ip == "") ip = getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.Fddi);
|
||||
if (ip == "") ip = getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.GenericModem);
|
||||
if (ip == "") ip = getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.IPOverAtm);
|
||||
if (ip == "") ip = getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.Tunnel);
|
||||
if (ip == "") ip = getLocalIPAddressWithNetworkInterface(NetworkInterfaceType.Unknown);
|
||||
|
||||
if (ip == "") ip = "127.0.0.1";
|
||||
return ip;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user