public void Disconnect()
        {
            try
            {
                foreach (var pair in ClientList)
                {
                    TcpClient client = pair.Key as TcpClient;
                    client.Close();
                }
                ClientList.Clear();

                if (TcpClient != null)
                {
                    TcpClient.Close();
                }
                if (TcpListner != null)
                {
                    TcpListner.Stop();
                }
            }
            catch (Exception ex)
            {
                ServerEvent.ErrorLog("Disconnect", ex.Message);
            }
        }
        public void Disconnect()
        {
            try
            {
                foreach (var pair in ClientList)
                {
                    TcpClient client = pair.Key as TcpClient;
                    IpPoint  = (IPEndPoint)client.Client.RemoteEndPoint;
                    ClientIP = IpPoint.Address + ":" + IpPoint.Port;
                    DisplayText(ClientIP + " 님의 연결을 해제합니다.");
                    client.Close();
                }
                ClientList.Clear();

                if (TcpClient != null)
                {
                    TcpClient.Close();
                }
                if (TcpListner != null)
                {
                    TcpListner.Stop();
                }
            }
            catch (Exception ex)
            {
                ServerEvent.ErrorLog("Disconnect", ex.Message);
            }
        }
        public void SendMessageAll(string message, string channel, bool isServer)
        {
            try
            {
                foreach (var pair in ClientList)
                {
                    TcpClient     client = pair.Key as TcpClient;
                    NetworkStream stream = client.GetStream();
                    byte[]        buffer = null;

                    if (isServer)
                    {
                        if (channel == pair.Value)
                        {
                            message = Encrypted.EncryptedMessage(message, IpPoint.Address.ToString());
                            buffer  = Encoding.Unicode.GetBytes("@" + message);
                            stream.Write(buffer, 0, buffer.Length);
                            stream.Flush();
                        }
                        else if (channel == "All")
                        {
                            ClientIP    = message.Split('/')[0];
                            Sendmessage = message.Substring(message.IndexOf("/") + 1);
                            buffer      = Encoding.Unicode.GetBytes(ClientIP + "/" + channel + ">" + Sendmessage);
                            stream.Write(buffer, 0, buffer.Length);
                            stream.Flush();
                        }
                    }
                    else
                    {
                        if (channel == pair.Value)
                        {
                            //ClientIP = message.Split('/')[0];
                            //Sendmessage = message.Substring(message.IndexOf("/") + 1);
                            buffer = Encoding.Unicode.GetBytes(message);
                            stream.Write(buffer, 0, buffer.Length);
                            stream.Flush();
                        }
                        else if (channel == "All")
                        {
                            ClientIP    = message.Split('/')[0];
                            Sendmessage = message.Substring(message.IndexOf("/") + 1);
                            buffer      = Encoding.Unicode.GetBytes(ClientIP + "/" + channel + ">" + Sendmessage);
                            stream.Write(buffer, 0, buffer.Length);
                            stream.Flush();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ServerEvent.ErrorLog("SendMessageAll", ex.Message);
            }
        }
Exemple #4
0
 public void ServerForm_Load(object sender, EventArgs e)
 {
     try
     {
         ServerEvent serverEvent = new ServerEvent();
         tbxLocalIpAddress.Text = serverEvent.LocalIPAddress();
         btnStart.Enabled       = true;
         btnStop.Enabled        = false;
     }
     catch (Exception ex) { }
 }
 public void ButtonStatusChange()
 {
     try
     {
         btnStart.Enabled = !(btnStart.Enabled);
         btnStop.Enabled  = !(btnStop.Enabled);
     }
     catch (Exception ex)
     {
         ServerEvent.ErrorLog("ButtonStatusChange", ex.Message);
     }
 }
 private void OnReceived(string message, string channel, bool isClientClose)
 {
     try
     {
         DisplayText(message);
         SendMessageAll(message, channel, false);
     }
     catch (Exception ex)
     {
         ServerEvent.ErrorLog("OnReceived", ex.Message);
     }
 }
 private void GetClientIP(string channel)
 {
     try
     {
         IpPoint = (IPEndPoint)TcpClient.Client.RemoteEndPoint;
         DisplayText(IpPoint.Address + ":" + IpPoint.Port + "/" + channel + " 님이 연결되었습니다.\r\n");
     }
     catch (Exception ex)
     {
         ServerEvent.ErrorLog("GetClientIP", ex.Message);
     }
 }
Exemple #8
0
 private void btnSend_Click(object sender, EventArgs e)
 {
     try
     {
         ServerEvent serverEvent = new ServerEvent();
         serverEvent.SendServerLog(tbxSendData.Text);
         SendMessageAll(tbxSendData.Text, "Server", false);
         DisplayText("Server >" + tbxSendData.Text);
         tbxSendData.Text = "";
     }
     catch (Exception ex) { }
 }
 private void GetClientIP(string channel)
 {
     try
     {
         IpPoint  = (IPEndPoint)TcpClient.Client.RemoteEndPoint;
         ClientIP = IpPoint.Address + ":" + IpPoint.Port + "/";
         rtbxReceivedData.Text += IpPoint.Address + ":" + IpPoint.Port + "/" + channel + " 님이 연결되었습니다.\r\n";
     }
     catch (Exception ex)
     {
         ServerEvent.ErrorLog("GetClientIP", ex.Message);
     }
 }
Exemple #10
0
 private void btnSend_Click(object sender, EventArgs e)
 {
     try
     {
         SendMessageAll(tbxSendData.Text, cbxChannel.Text, true);
         DisplayText("Server /" + cbxChannel.Text + ">" + tbxSendData.Text);
         tbxSendData.Text = "";
     }
     catch (Exception ex)
     {
         ServerEvent.ErrorLog("btnStart_Click", ex.Message);
     }
 }
Exemple #11
0
 void HandlerClientsOnDisconnected(TcpClient clientScoket)
 {
     try
     {
         if (ClientList.ContainsKey(clientScoket))
         {
             ClientList.Remove(clientScoket);
         }
     }
     catch (Exception ex)
     {
         ServerEvent.ErrorLog("HandlerClientsOnDisconnected", ex.Message);
     }
 }
Exemple #12
0
 public void ServerForm_Load(object sender, EventArgs e)
 {
     try
     {
         ServerEvent serverEvent = new ServerEvent();
         tbxLocalIpAddress.Text = serverEvent.LocalIPAddress();
         btnStart.Enabled       = true;
         btnStop.Enabled        = false;
     }
     catch (Exception ex)
     {
         MessageBox.Show("btnStart_Click : \r\n" + ex.ToString());
     }
 }
Exemple #13
0
 public void ServerForm_Load(object sender, EventArgs e)
 {
     log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("log_ErrorLog.xml"));
     try
     {
         ServerEvent serverEvent = new ServerEvent();
         tbxLocalIpAddress.Text = serverEvent.LocalIPAddress();
         btnStart.Enabled       = true;
         btnStop.Enabled        = false;
     }
     catch (Exception ex)
     {
         logger.Info("ServerForm_Load Error: " + ex.ToString());
     }
 }
Exemple #14
0
        public void SendMessageAll(string message, string channel, bool isServer)
        {
            try
            {
                foreach (var pair in ClientList)
                {
                    TcpClient client = pair.Key as TcpClient;
                    IpPoint = (IPEndPoint)client.Client.RemoteEndPoint;
                    NetworkStream stream = client.GetStream();
                    byte[]        buffer = null;

                    // 20200820 서버 전용 암호화 기능 추가
                    if (isServer)
                    {
                        string sendmessage = Encrypted.EncryptedMessage(message, IpPoint.Address.ToString());
                        ServerEvent.SendServerLog(sendmessage);
                        if (channel == pair.Value)
                        {
                            buffer = Encoding.Unicode.GetBytes("ⓐ" + sendmessage);
                            stream.Write(buffer, 0, buffer.Length);
                            stream.Flush();
                        }
                        else if (channel == "All")
                        {
                            buffer = Encoding.Unicode.GetBytes("ⓐ" + sendmessage);
                            stream.Write(buffer, 0, buffer.Length);
                            stream.Flush();
                        }
                    }
                    else
                    {
                        if (channel == pair.Value)
                        {
                            buffer = Encoding.Unicode.GetBytes(message);
                            stream.Write(buffer, 0, buffer.Length);
                            stream.Flush();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ServerEvent.ErrorLog("SendMessageAll", ex.Message);
            }
        }
Exemple #15
0
 public void Disconnect()
 {
     try
     {
         if (TcpClient != null)
         {
             TcpClient.Close();
         }
         if (TcpListner != null)
         {
             TcpListner.Stop();
         }
     }
     catch (Exception ex)
     {
         ServerEvent.ErrorLog("Disconnect", ex.Message);
     }
 }
Exemple #16
0
 void OnDisconnected(TcpClient clientScoket)
 {
     try
     {
         IpPoint  = (IPEndPoint)clientScoket.Client.RemoteEndPoint;
         ClientIP = ClientIP = IpPoint.Address + ":" + IpPoint.Port;
         DisplayText(ClientIP + "/ 님의 연결이 해제되었습니다.");
         SendMessageAll(ClientIP + "/ 님의 연결이 해제되었습니다.", ClientList[clientScoket].ToString(), true);
         if (ClientList.ContainsKey(clientScoket))
         {
             ClientList.Remove(clientScoket);
         }
     }
     catch (Exception ex)
     {
         ServerEvent.ErrorLog("OnDisconnected", ex.Message);
     }
 }
Exemple #17
0
        private void OnReceived(string message, string channel, bool isClientClose)
        {
            try
            {
                ClientIP    = message.Split('/')[0];
                Sendmessage = message.Substring(message.IndexOf("/") + 1);
                string displayMessage = ClientIP + "/" + channel + ">" + Sendmessage;
                DisplayText(displayMessage);
                SendMessageAll(message, channel, false);

                if (isClientClose)
                {
                    ClientInfoList.Remove(ClientIP);
                }
            }
            catch (Exception ex)
            {
                ServerEvent.ErrorLog("OnReceived", ex.Message);
            }
        }
Exemple #18
0
 private void DisplayText(string text)
 {
     try
     {
         if (rtbxReceivedData.InvokeRequired)
         {
             rtbxReceivedData.BeginInvoke(new MethodInvoker(delegate
             {
                 rtbxReceivedData.AppendText(text + Environment.NewLine);
             }));
         }
         else
         {
             rtbxReceivedData.AppendText(text + Environment.NewLine);
         }
     }
     catch (Exception ex)
     {
         ServerEvent.ErrorLog("DisplayText", ex.Message);
     }
 }
Exemple #19
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                bool isConnect = true;
                rtbxReceivedData.Text = "";

                Thread ServerThread = new Thread(InitSocket);
                ServerThread.IsBackground = true;
                ServerThread.Start();

                if (isConnect == true)
                {
                    ButtonStatusChange();
                }
            }
            catch (Exception ex)
            {
                ServerEvent.ErrorLog("btnStart_Click", ex.Message);
            }
        }
Exemple #20
0
        public void ServerForm_Load(object sender, EventArgs e)
        {
            try
            {
                ServerEvent serverEvent = new ServerEvent();
                tbxLocalIpAddress.Text = serverEvent.LocalIPAddress();
                btnStart.Enabled       = true;
                btnStop.Enabled        = false;

                cbxChannel.Items.Add("All");
                for (int i = 65; i < 91; i++)
                {
                    cbxChannel.Items.Add(Convert.ToChar(i));
                }
                cbxChannel.Text = "All";
            }
            catch (Exception ex)
            {
                ServerEvent.ErrorLog("ServerForm_Load", ex.Message);
            }
        }
Exemple #21
0
        public void ButtonStatusChange()
        {
            try
            {
                btnStart.Enabled = !(btnStart.Enabled);
                btnStop.Enabled  = !(btnStop.Enabled);


                if (btnStart.Enabled == true && ClientIP != null)
                {
                    foreach (string clientInfo in ClientInfoList)
                    {
                        DisplayText(clientInfo + " 와의 연결이 끊어졌습니다.");
                    }
                    ClientInfoList.Clear();
                }
            }
            catch (Exception ex)
            {
                ServerEvent.ErrorLog("ButtonStatusChange", ex.Message);
            }
        }
Exemple #22
0
        private void InitSocket()
        {
            try
            {
                tbxLocalIpAddress.Text =
                    Regex.Replace(tbxLocalIpAddress.Text, @"[^0-9].[^0-9].[^0-9].[^0-9]", "");
                tbxPort.Text = Regex.Replace(tbxPort.Text, @"[^0-9]", "");
                if (tbxLocalIpAddress.Text == "" || tbxPort.Text == "")
                {
                    MessageBox.Show("Local IP Address 또는 Port 번호가 올바르지 않습니다.");
                    throw new Exception();
                }
                TcpListner = new TcpListener(IPAddress.Parse(tbxLocalIpAddress.Text.Trim()), Int32.Parse(tbxPort.Text));
                TcpClient  = default(TcpClient);
                TcpListner.Start();
                DisplayText(">> server Started");
                while (true)
                {
                    try
                    {
                        TcpClient = TcpListner.AcceptTcpClient();

                        NetworkStream stream = TcpClient.GetStream();

                        stream = TcpClient.GetStream();//acc.Receive(sizeBuf, 0, sizeBuf.Length, 0);

                        byte[] sizeBuf = new byte[TcpClient.ReceiveBufferSize];
                        stream.Read(sizeBuf, 0, (int)TcpClient.ReceiveBufferSize);
                        int size = BitConverter.ToInt32(sizeBuf, 0);

                        MemoryStream ms = new MemoryStream();

                        while (size > 0)
                        {
                            byte[] buffer;
                            if (size < TcpClient.ReceiveBufferSize) //acc.ReceiveBufferSize)
                            {
                                buffer = new byte[size];
                            }
                            else
                            {
                                buffer = new byte[TcpClient.ReceiveBufferSize];
                            }

                            int rec = stream.Read(buffer, 0, buffer.Length);

                            size -= rec;
                            ms.Write(buffer, 0, buffer.Length);
                        }
                        ms.Close();

                        byte[] data = ms.ToArray();

                        ms.Dispose();

                        string channel = Encoding.UTF8.GetString(data);


                        this.Invoke(new DeligateGetClientIP(GetClientIP), channel);

                        ClientList.Add(TcpClient, channel);

                        IpPoint  = (IPEndPoint)TcpClient.Client.RemoteEndPoint;
                        ClientIP = IpPoint.Address + ":" + IpPoint.Port;
                        ClientInfoList.Add(ClientIP.ToString());

                        SendMessageAll(ClientIP + "/ 님이 연결되었습니다.", channel, true);

                        HandleClient handle = new HandleClient();
                        handle.OnReceived     += new HandleClient.MessageDisplayHandler(OnReceived);
                        handle.OnDisconnected += new HandleClient.DisconnectedHandler(HandlerClientsOnDisconnected);
                        handle.StartClient(TcpClient, ClientList);
                    }
                    catch
                    {
                        throw new SocketException();
                    }
                }
            }
            catch (SocketException ex)
            {
                this.Invoke(new DeligateDisconnect(Disconnect));
                ServerEvent.ErrorLog("InitSocket", ex.Message);
            }
            catch (Exception ex)
            {
                ServerEvent.ErrorLog("InitSocket", ex.Message);
            }
            finally
            {
                this.Invoke(new DeligateButtonChange(ButtonStatusChange));
            }
        }