private void Button2_Click(object sender, EventArgs e) { try { server = new AsyncSocketServer(IPAddress.Parse(IPcomboBox.Text), int.Parse(PorttextBox.Text), 10); server._ServerStart += TcpServerStar; server._ClientConnected += ClientConnected; server._ClientDisconnected += ClientDisconnected; server._ReceiveData += ReceiveData; server._SendData += SendData; server.ServerStart(); button2.Enabled = !server.IsRunning; button1.Enabled = server.IsRunning; IPcomboBox.Enabled = !server.IsRunning; PorttextBox.Enabled = !server.IsRunning; } catch { server?.Dispose(); button2.Enabled = true; button1.Enabled = false; IPcomboBox.Enabled = true; PorttextBox.Enabled = true; } }
/// <summary> /// 打开服务器操作,此服务器最大连接数为10 /// 默认服务器端口位11000 /// </summary> /// <param name="iPAddress">服务器地址</param> /// <param name="port">服务器端口</param> /// <param name="maxbacklog">服务器最大连接数</param> /// <returns>服务器运行状态</returns> public bool _ServerStart(int maxbacklog) { server = new AsyncSocketServer(ServerAddress, ServerPort, maxbacklog); server._ClientConnected += ClientConnected; server._ClientDisconnected += ClientDisconnected; server._ReceiveData += ReceiveData; server.ServerStart(maxbacklog); IsRunning = server.IsRunning; return(server.IsRunning); }