修改tcp的ip选择逻辑
This commit is contained in:
commit
280062d86c
3 files changed
+69
-62
No files matched your search
+44
-59
@@ -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; }
|
||||
}
|
||||
|
||||
//编码格式
|
||||
Encoding econding = Encoding.Default;
|
||||
|
||||
|
||||
public static void Start()
|
||||
{
|
||||
//第一步:调用socket()函数创建一个用于通信的套接字
|
||||
listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
|
||||
|
||||
private string CurrentTime
|
||||
{
|
||||
get { return DateTime.Now.ToString("HH:mm:ss") + Environment.NewLine; }
|
||||
}
|
||||
|
||||
//编码格式
|
||||
Encoding econding = Encoding.Default;
|
||||
|
||||
public static void Start()
|
||||
{
|
||||
//第一步:调用socket()函数创建一个用于通信的套接字
|
||||
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)
|
||||
{
|
||||
Socket clientSocket = listenSocket.Accept();
|
||||
|
||||
string ip = clientSocket.RemoteEndPoint.ToString();
|
||||
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
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