Ejemplo n.º 1
0
        private void DoChat()
        {
            NetworkStream stream = null;

            try
            {
                string message = string.Empty;

                while (true)
                {
                    stream = TcpSocket.GetStream();
                    byte[] sizeBuf = new byte[TcpSocket.ReceiveBufferSize];
                    stream.Read(sizeBuf, 0, (int)TcpSocket.ReceiveBufferSize);
                    int          size         = BitConverter.ToInt32(sizeBuf, 0);
                    MemoryStream memoryStream = new MemoryStream();

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

                        int rec = stream.Read(buffer, 0, buffer.Length);
                        size -= rec;
                        memoryStream.Write(buffer, 0, buffer.Length);
                    }
                    memoryStream.Close();
                    byte[] data = memoryStream.ToArray();
                    memoryStream.Dispose();
                    message = Encoding.UTF8.GetString(data);
                    if (message == "")
                    {
                        throw new Exception("공백 출력 == 상대방 소켓 끊어짐");
                    }
                    serverEvent.ReceiveServerLog(message);
                    OnReceived(message, ClientList[TcpSocket].ToString(), false);
                }
            }
            catch (Exception ex)
            {
                serverEvent.ErrorLog("DoChat", ex.Message);
                if (TcpSocket != null)
                {
                    OnDisconnected(TcpSocket);
                    TcpSocket.Close();
                    stream.Close();
                    if (ThreadHandler.IsAlive == true)
                    {
                        ThreadHandler.Interrupt();
                        ThreadHandler.Abort();
                    }
                }
            }
        }
Ejemplo n.º 2
0
        private void DoChat()
        {
            NetworkStream stream = null;

            try
            {
                byte[] buffer  = new byte[1024];
                string message = string.Empty;
                int    bytes   = 0;

                while (true)
                {
                    stream  = TcpSocket.GetStream();
                    bytes   = stream.Read(buffer, 0, buffer.Length);
                    message = Encoding.Unicode.GetString(buffer, 0, bytes);

                    if (OnReceived != null)
                    {
                        if (message.Substring(message.LastIndexOf("$") + 1) == "@")
                        {
                            OnReceived(message.Substring(0, message.LastIndexOf("$")), ClientList[TcpSocket].ToString(), true);
                        }
                        else
                        {
                            OnReceived(message.Substring(0, message.LastIndexOf("$")), ClientList[TcpSocket].ToString(), false);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (TcpSocket != null)
                {
                    if (OnDisconnected != null)
                    {
                        OnDisconnected(TcpSocket);
                    }

                    if (ThreadHandler.IsAlive == true)
                    {
                        ThreadHandler.Interrupt();
                        ThreadHandler.Abort();
                    }

                    TcpSocket.Close();
                    stream.Close();
                }
            }
        }
Ejemplo n.º 3
0
        private void DoChat()
        {
            NetworkStream stream = null;

            try
            {
                byte[] buffer  = new byte[1024];
                string message = string.Empty;
                int    bytes   = 0;
                log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo("log_ChattingSystem.xml"));

                while (true)
                {
                    stream  = TcpSocket.GetStream();
                    bytes   = stream.Read(buffer, 0, buffer.Length);
                    message = Encoding.Unicode.GetString(buffer, 0, bytes);
                    logger.Info("Recv: " + message); //받은 메시지 원문 기록
                    message = message.Substring(0, message.LastIndexOf("$"));
                    if (OnReceived != null)
                    {
                        OnReceived(message, ClientList[TcpSocket].ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                if (TcpSocket != null)
                {
                    if (OnDisconnected != null)
                    {
                        OnDisconnected(TcpSocket);
                    }

                    if (ThreadHandler.IsAlive == true)
                    {
                        ThreadHandler.Interrupt();
                        ThreadHandler.Abort();
                    }

                    TcpSocket.Close();
                    stream.Close();
                }
            }
        }
Ejemplo n.º 4
0
        private void DoChat()
        {
            NetworkStream stream = null;

            try
            {
                string message = string.Empty;

                while (true)
                {
                    stream = TcpSocket.GetStream();

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

                    MemoryStream ms = new MemoryStream();


                    while (size > 0)
                    {
                        byte[] buffer;
                        if (size < TcpSocket.ReceiveBufferSize)
                        {
                            buffer = new byte[size];
                        }
                        else
                        {
                            buffer = new byte[TcpSocket.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();

                    message = Encoding.UTF8.GetString(data);
                    //MessageBox.Show(message);
                    serverEvent.ReceiveServerLog(message); //받은 메시지 원문 기록
                    OnReceived(message, ClientList[TcpSocket].ToString(), false);

                    /*
                     * if (OnReceived != null)
                     * {
                     *  if (message.Substring(message.LastIndexOf("$") + 1) == "@")
                     *      OnReceived(message.Substring(0, message.LastIndexOf("$")), ClientList[TcpSocket].ToString(), true);
                     *  else
                     *      ;
                     * }
                     * */
                }
            }
            catch (Exception ex)
            {
                serverEvent.ErrorLog("DoChat", ex.Message);
                if (TcpSocket != null)
                {
                    /*
                     * ServerSecurity Encrypted = new ServerSecurity();
                     *
                     *
                     * string Encrypted = Encrypt.EncryptedMessage(IpEndPoint.Address + ":" + IpEndPoint.Port + "/" + " 연결이 종료되었습니다."
                     *  , serverEvent.LocalIPAddress() + ":" + PortNum() + "/" + ClientList[TcpSocket].ToString());
                     * MessageBox.Show(Encrypted);
                     *
                     * OnReceived(Encrypted, ClientList[TcpSocket].ToString(), true);
                     * */
                    TcpSocket.Close();
                    stream.Close();
                    if (ThreadHandler.IsAlive == true)
                    {
                        ThreadHandler.Interrupt();
                        ThreadHandler.Abort();
                    }
                    if (OnDisconnected != null)
                    {
                        OnDisconnected(TcpSocket);
                    }
                }
            }
        }