Beispiel #1
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                if (!start)
                {
                    int portNo = 0;
                    if (string.IsNullOrEmpty(txtPortNo.Text))
                    {
                        errorProvider1.SetError(txtPortNo, "Cannot be empty");
                        return;
                    }
                    else if (!int.TryParse(txtPortNo.Text, out portNo))
                    {
                        errorProvider1.SetError(txtPortNo, "Not valid Port No");
                        return;
                    }
                    else if (portNo < 100 && portNo > 65535)
                    {
                        errorProvider1.SetError(txtPortNo, "Not valid Port No");
                        return;
                    }

                    server = new Chat.Core.Server.ChatServer(portNo);
                    server.NewMessageReceivedFromClient += new dgNewMessageReceivedFromClient(newMessageReceivedFromClient);
                    server.ClientDisconnected           += new dgClientDisconnected(clientDisconnected);
                    server.ClientConnected += new dgClientConnected(clientConnection);

                    server.Start();
                    string hostName    = Dns.GetHostName();
                    var    addressList = Dns.GetHostByName(hostName).AddressList;
                    setMessage($"Server started => IP: {(addressList.Length > 0 ? addressList[0].ToString() : hostName)} Port: {portNo}, Date: {DateTime.Now.ToShortDateString()} {DateTime.Now.ToLongTimeString()}");
                    btnStart.Text      = "Stop";
                    btnStart.BackColor = Color.Crimson;
                    BackColor          = Color.GhostWhite;
                    notifyIcon1.Text   = $"Chat | Server [{(addressList.Length > 0 ? addressList[0].ToString() : hostName)}:{portNo}]";
                }
                else
                {
                    server.Stop();
                    string hostName    = Dns.GetHostName();
                    var    addressList = Dns.GetHostByName(hostName).AddressList;
                    setMessage($"Server stopped => IP: {(addressList.Length > 0 ? addressList[0].ToString() : "hostName")} Port: {txtPortNo.Text}, Date: {DateTime.Now.ToShortDateString()} {DateTime.Now.ToLongTimeString()}");
                    btnStart.Text      = "Start";
                    btnStart.BackColor = Color.LimeGreen;
                    BackColor          = Color.AliceBlue;
                    lvClients.Items.Clear();
                    notifyIcon1.Text = $"Chat | Server";
                }

                start = !start;
                txtPortNo.ReadOnly = start;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Beispiel #2
0
 public ConnectionListener(ChatServer server, int port)
 {
     this.server = server;
     this.port   = port;
 }