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
        /// <summary>
        /// Threads the menu.
        /// </summary>
        private static void ThreadMenu()
        {
            System.Console.WriteLine("Main thread id = " + System.Threading.Thread.CurrentThread.GetHashCode().ToString());

            string str = "";

            while (str != "x")
            {
                System.Console.WriteLine("\n---------------");
                System.Console.WriteLine("MENU:");
                System.Console.WriteLine("---------------");
                System.Console.WriteLine(" x - eXit menu");
                System.Console.WriteLine(" 1 - start");
                System.Console.WriteLine(" 0 - abort");
                System.Console.WriteLine(" 2 - suspend");
                System.Console.WriteLine(" s - state");
                System.Console.WriteLine("---------------\n");

                try
                {
                    str = System.Console.ReadLine();



                    switch (str)
                    {
                    case "1":
                        th.Start();
                        break;

                    case "0":
                        th.Abort();
                        break;

                    case "2":
                        //th.Thread.s
                        break;

                    case "s":
                        System.Console.WriteLine("Thread state  = " + th.Thread.ThreadState.ToString());
                        System.Console.WriteLine("IsAlive       = " + th.Thread.IsAlive.ToString());
                        System.Console.WriteLine("IsAborted     = " + (((th.Thread.ThreadState & System.Threading.ThreadState.Aborted) > 0) ? true.ToString() : false.ToString()));
                        break;
                    }
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine("EXCEPTION = " + ex.Message + ", SOURCE = " + ex.StackTrace);
                }
                finally
                {
                }
            }
        }
Ejemplo n.º 3
0
 private void PosTaggerTagForm_FormClosing(object sender, FormClosingEventArgs e)
 {
     try
     {
         if (mThread.IsAlive)
         {
             mLogger.Info(null, "Počakajte ...");
         }
         mLogger.LocalLevel = Logger.Level.Off;
         ThreadHandler.Abort(mThread, /*timeoutMs=*/ 3000);
     }
     catch { }
 }
Ejemplo n.º 4
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.º 5
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.º 6
0
 private void btnCancel_Click(object sender, EventArgs e)
 {
     btnCancel.Enabled = false;
     if (mThread.IsAlive)
     {
         mLogger.Info(null, "Počakajte ...");
     }
     mLogger.LocalLevel = Logger.Level.Off;
     try
     {
         ThreadHandler.Abort(mThread, /*timeoutMs=*/ 3000);
         while (mThread.IsAlive)
         {
             Thread.Sleep(100);
         }
     }
     catch { }
     GC.Collect(); // this closes all open files by invoking finalizers on readers and writers
     EnableForm();
     mLogger.LocalLevel = Logger.Level.Debug;
     mLogger.Info(null, "Označevanje prekinjeno.");
 }
Ejemplo n.º 7
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);
                    }
                }
            }
        }