public void readfile() { try { filelock.EnterReadLock(); records.Clear(); int ttl; StreamReader sr = new StreamReader(filepath, true); string nextline = null; string[] str = null; while ((nextline = sr.ReadLine()) != null) { str = nextline.Split(' '); int.TryParse(str[2], out ttl); MacRecord record = new MacRecord(str[0], str[1], ttl); records.Add(record); } sr.Close(); } catch (Exception ex) { Console.Write(ex.Message); } finally { filelock.ExitReadLock(); } }
public void recv(object socketclientpara) { Socket socketReciver = socketclientpara as Socket; while (true) { //创建一个内存缓冲区,其大小为1024*1024字节 即1M byte[] arrServerRecMsg = new byte[1024 * 1024]; //将接收到的信息存入到内存缓冲区,并返回其字节数组的长度 try { int length = socketReciver.Receive(arrServerRecMsg); //将机器接受到的字节数组转换为人可以读懂的字符串 string strSRecMsg = Encoding.UTF8.GetString(arrServerRecMsg, 0, length); string[] frame = strSRecMsg.Split('@'); if (frame[0] == "11") { _syncContext.Post(updateMessage, "端口" + getPortByIp(socketReciver) + ":" + strSRecMsg + "\r\n"); _syncContext.Post(updateMessage, "查表中......\r\n"); readfile(); bool hasS = false; bool hasD = false; string dPort = null; foreach (MacRecord rec in records) { rec.Ttl = rec.Ttl - 1; //如果是源端口或者目的端口,重置为5 if (rec.Mac == frame[1] || rec.Mac == frame[2]) { hasS = true; rec.Ttl = 5; } } for (int i = 0; i < records.Count; i++) { if (records[i].Ttl <= 0) { records.RemoveAt(i); } } //如果源Mac记录不存在,则写入Mac表中 if (hasS == false) { MacRecord newRec = new MacRecord(frame[1], getPortByIp(socketReciver), 5); records.Add(newRec); _syncContext.Post(updateMessage, "Mac表中不存在源Mac,添加源Mac" + frame[1] + "\r\n"); } foreach (MacRecord rec in records) { if (rec.Mac == frame[2]) { hasD = true; dPort = rec.Port; break; } } if (hasD == true) { //mac表中有目的mac _syncContext.Post(updateMessage, "Mac表中存在目的Mac,转发到端口" + dPort + "\r\n"); int i = 1; foreach (KeyValuePair <string, Socket> item in clientConnectionItems) { if (i.ToString() == dPort) { item.Value.Send(Encoding.UTF8.GetBytes("11@" + frame[1] + "@" + frame[2] + "@" + frame[3])); } i++; } } else { //mac表中没有目的mac,广播 _syncContext.Post(updateMessage, "Mac表中没有目的Mac,广播\r\n"); //connector.Send(Encoding.UTF8.GetBytes(strSRecMsg)); int i = 1; foreach (KeyValuePair <string, Socket> item in clientConnectionItems) { if (i == clientConnectionItems.Count) { //广播给其他Switcher item.Value.Send(Encoding.UTF8.GetBytes("10@" + frame[1] + "@" + frame[2] + "@" + frame[3]));//交给交换机 } else { //广播给客户 item.Value.Send(Encoding.UTF8.GetBytes("00@" + frame[1] + "@" + frame[2] + "@" + frame[3])); } i++; } } writefile(); } else if (frame[0] == "10") { //代理另一个交换机的广播请求 _syncContext.Post(updateMessage, "端口" + getPortByIp(socketReciver) + ":" + strSRecMsg + "\r\n"); _syncContext.Post(updateMessage, "查表中......\r\n"); readfile(); bool hasS = false; bool hasD = false; string dPort = null; foreach (MacRecord rec in records) { rec.Ttl = rec.Ttl - 1; if (rec.Mac == frame[1] || rec.Mac == frame[2]) { hasS = true; rec.Ttl = 5; } } for (int i = 0; i < records.Count; i++) { if (records[i].Ttl <= 0) { records.RemoveAt(i); } } //如果源Mac记录不存在,则写入Mac表中 if (hasS == false) { MacRecord newRec = new MacRecord(frame[1], getPortByIp(socketReciver), 5); records.Add(newRec); _syncContext.Post(updateMessage, "Mac表中不存在源Mac,添加源Mac" + frame[1] + "\r\n"); } foreach (MacRecord rec in records) { if (rec.Mac == frame[2]) { hasD = true; dPort = rec.Port; break; } } if (hasD == true) { //mac表中有目的mac,单点传送 _syncContext.Post(updateMessage, "Mac表中存在目的Mac,转发到端口" + dPort + "\r\n"); int i = 1; foreach (KeyValuePair <string, Socket> item in clientConnectionItems) { if (i.ToString() == dPort) { item.Value.Send(Encoding.UTF8.GetBytes("11@" + frame[1] + "@" + frame[2] + "@" + frame[3])); } i++; } //告诉另一个switcher,我的mac表中有此目的mac getSwitcher().Send(Encoding.UTF8.GetBytes("01@" + frame[2])); } else { //mac表中没有目的mac,代广播 _syncContext.Post(updateMessage, "Mac表中没有目的Mac,广播\r\n"); //connector.Send(Encoding.UTF8.GetBytes(strSRecMsg)); int i = 1; foreach (KeyValuePair <string, Socket> item in clientConnectionItems) { /*if (i != clientConnectionItems.Count) { * //发送代广播信息,但不发给传来的switcher,避免环 * item.Value.Send(Encoding.UTF8.GetBytes("10@"+frame[1]+"@"+frame[2]+"@"+frame[3])); * }*/ if (item.Value != socketReciver) { item.Value.Send(Encoding.UTF8.GetBytes("10@" + frame[1] + "@" + frame[2] + "@" + frame[3])); } i++; } } writefile(); } else if (frame[0] == "01") { //收到了广播的响应 readfile(); _syncContext.Post(updateMessage, "端口" + getPortByIp(socketReciver) + "响应了广播,"); MacRecord newRec = new MacRecord(frame[1], getPortByIp(socketReciver), 5); records.Add(newRec); _syncContext.Post(updateMessage, "添加目的Mac: " + frame[1] + "到表中\r\n"); writefile(); } else if (frame[0] == "00") { readfile(); //收到了代理广播的响应 _syncContext.Post(updateMessage, "端口" + getPortByIp(socketReciver) + "响应了广播"); _syncContext.Post(updateMessage, strSRecMsg + "\r\n"); MacRecord newRec = new MacRecord(frame[1], getPortByIp(socketReciver), 5); records.Add(newRec); _syncContext.Post(updateMessage, "转发并添加目的Mac:" + frame[1] + "到表中\r\n"); //响应交换机的代理信息 getSwitcher().Send(Encoding.UTF8.GetBytes("01@" + frame[1])); writefile(); } } catch (Exception ex) { if (socketReciver != null) { //提示套接字监听异常 _syncContext.Post(updateMessage, "端口" + socketReciver.RemoteEndPoint + "已经中断连接" + "\r\n" + ex.Message + "\r\n" + ex.StackTrace + "\r\n"); //Console.WriteLine("客户端" + socketReciver.RemoteEndPoint + "已经中断连接" + "\r\n" + ex.Message + "\r\n" + ex.StackTrace + "\r\n"); //关闭之前accept出来的和客户端进行通信的套接字 socketReciver.Close(); } break; } } }