private void btn_Server_Listen(object sender, RoutedEventArgs e)
{
try
{
//当点击开始监听的时候 在服务器端创建一个负责监听IP地址和端口号的Socket
m_socketServerWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//分析ip地址
IPAddress ip = IPAddress.Parse(“172.16.0.7”);
//创建端口号
IPEndPoint point = new IPEndPoint(ip, Convert.ToInt32(“8001”));
//绑定IP地址和端口号
m_socketServerWatch.Bind(point);
//设置最大可以同时连接多少个请求
m_socketServerWatch.Listen(20);
//开始监听:创建线程
Thread AcceptSocketThread = new Thread(new ParameterizedThreadStart(ServerStartListenThread));
AcceptSocketThread.IsBackground = true;
AcceptSocketThread.Start(m_socketServerWatch);
Exception.Text = "服务器启动监听成功";
}
catch (Exception ex)
{
Logtest("btn_Server_Listen->172.16.0.7" + ex.Message);
Exception.Text = "软件异常,请及时将日志发给开发人员处理";
return;
}
}
///
/// 服务器等待客户端的连接,并且创建与之通信用的Socket
///
///
private void ServerStartListenThread(object obj)
{
Socket socketWatch = obj as Socket;
while (true)
{
Socket ServersendSocket;
//等待客户端的连接,并且创建一个用于通信的Socket
ServersendSocket = socketWatch.Accept();
//获取远程主机的ip地址和端口号
string strIp = ServersendSocket.RemoteEndPoint.ToString();
//m_ClientIPAndUserS.Add(strIp, “user”); 没有登陆成功暂不保存客户端相关信息
string strMsg = "客户端:" + ServersendSocket.RemoteEndPoint + "连接成功";
DeleteLogTxt(); //客户端新增连接后检查日志文件大小,超过32M删除
Logtest(strMsg);
Exception.Dispatcher.Invoke(DisplayCallBack, strMsg);
//定义接收客户端消息的线程
Thread threadReceive = new Thread(new ParameterizedThreadStart(ServerReceiveThread));
threadReceive.IsBackground = true;
threadReceive.Start(ServersendSocket);
}
}
///
/// 服务器端不停的接收客户端发送的消息
///
///
private void ServerReceiveThread(object obj)
{
Socket socketSend = obj as Socket;
string strClientIp = “”;
try
{
//提取客户端IP
string strIpAndPoint = socketSend.RemoteEndPoint.ToString();
string[] sIpAndPointArray = Regex.Split(strIpAndPoint, ":", RegexOptions.IgnoreCase);
if (2 != sIpAndPointArray.Length)
{
System.Windows.Forms.MessageBox.Show("ServerReceiveThread->Split strIpAndPoint Fail");
Logtest("ServerReceiveThread->Split strIpAndPoint Fail");
return;
}
strClientIp = sIpAndPointArray[0];
socketSend.ReceiveTimeout = 65000; //客户端的超时退出登陆的时间为3*20=60
//Logtest("strClientIp:" + strClientIp);
while (true)
{
int count = 0;
byte[] buffer = new byte[2048];
try
{
count = socketSend.Receive(buffer); //count为实际接收到的有效字节数
}
catch (Exception ex)
{
if (!m_ClientIPAndUserEnableS.Remove(strClientIp))
{
Logtest("ServerReceiveThread->m_ClientIPAndUserS.Remove Fail");
}
socketSend.Close();
socketSend = null;
Logtest("ServerReceiveThread->Receive throw exciption: " + ex.Message);
Logtest(strClientIp + "ReceiveThread检查其已失去心跳,故将其移除出集合");
return;
}
if (count == 0)//count 表示客户端关闭连接,要退出循环
{
if (!m_ClientIPAndUserEnableS.Remove(strClientIp))
{
Logtest("ServerReceiveThread->m_ClientIPAndUserS.Remove Fail");
}
socketSend.Close();
socketSend = null;
Logtest(strClientIp + "Receive检查其主动断开连接,已将其移除出集合");
Exception.Dispatcher.Invoke(DisplayCallBack,strClientIp + "客户端已断开连接");
return;
}
string strReceiveMsg = Encoding.Default.GetString(buffer, 0, count);
string strValue = "接收:" + socketSend.RemoteEndPoint + strReceiveMsg;
Logtest(strValue);
string[] sArray = Regex.Split(strReceiveMsg, ":", RegexOptions.IgnoreCase);
if (3 != sArray.Length)
{
ServerSendToOneClient("FormError", socketSend);
Logtest("Client Send Msg FormError.");
continue;
}
}///while循环到这结束
}
catch (Exception ex)
{
Logtest("ServerReceiveThread->" + ex.Message);
if (!m_ClientIPAndUserEnableS.Remove(strClientIp))
{
Logtest("ServerReceiveThread->m_ClientIPAndUserS.Remove Fail");
}
socketSend.Close();
socketSend = null;
Logtest(strClientIp + "连接异常,已将其移除出集合");
Exception.Dispatcher.Invoke(DisplayCallBack, "客户端连接异常");
return;
}
}