using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; namespace DyeingComputer.UserClass { public class TCPServer { public static string infoR; //第一步:调用socket()函数创建一个用于通信的套接字 public static Socket listenSocket; //字典集合:存储IP和Socket的集合 public static Dictionary OnLineList = new Dictionary(); //当前时间 private string CurrentTime { get { return DateTime.Now.ToString("HH:mm:ss") + Environment.NewLine; } } //编码格式 public static Encoding econding = Encoding.UTF8; 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.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); //ShowMessage("服务器开启成功"); //开启一个线程监听 Task.Run(new Action(() => { ListenConnection(); })); } public static void ListenConnection() { while (true) { Socket clientSocket = listenSocket.Accept(); string ip = clientSocket.RemoteEndPoint.ToString(); //更新在线列表 // AddOnLine(ip, true); //更新在线列表集合 OnLineList.Add(ip, clientSocket); UserClass.LogGing.LogGingDATA("IP_Online = " + ip); // ShowMessage(ip + "上线了"); Task.Run(() => ReceiveMsg(clientSocket)); } } /// /// 接收方法 /// /// public static void ReceiveMsg(Socket clientSocket) { while (true) { //定义一个2M的缓冲区 byte[] buffer = new byte[1024 * 1024 * 10]; int length = -1; try { length = clientSocket.Receive(buffer); } catch (Exception) { //客户端下线了 //更新在线列表 string ip = clientSocket.RemoteEndPoint.ToString(); UserClass.LogGing.LogGingDATA("IP_Line = " + ip); // AddOnLine(ip, false); OnLineList.Remove(ip); //移除ip break; //退出循环 } if (length == 0) { //客户端下线了 //更新在线列表 string ip = clientSocket.RemoteEndPoint.ToString(); UserClass.LogGing.LogGingDATA("IP_Line = " + ip); // AddOnLine(ip, false); OnLineList.Remove(ip); break; } else { infoR = econding.GetString(buffer, 0, length); //ShowMessage(info); string ip = clientSocket.RemoteEndPoint.ToString(); } } } /* /// /// 在线列表更新 /// /// /// private void AddOnLine(string clientIp, bool value) { Invoke(new Action(() => { if (value) { this.lst_Online.Items.Add(clientIp); } else { this.lst_Online.Items.Remove(clientIp); } })); } /// /// 更新接收区 /// /// private void ShowMessage(string info) { Invoke(new Action(() => { this.txt_Rcv.AppendText(CurrentTime + info + Environment.NewLine); })); } */ /// /// 消息发送 /// public static void Bn_Send(string IP, string DAT) { if (OnLineList.ContainsKey(IP)) { OnLineList[IP].Send(econding.GetBytes(DAT)); } } /// /// 群发功能 /// public static void Btn_SendAll(string DAT) { foreach (string item in OnLineList.Keys.ToArray()) { if (OnLineList.ContainsKey(item)) { OnLineList[item].Send(econding.GetBytes(DAT)); } } } } }