Beispiel #1
0
        private void ReceiveData(object state)
        {
            CSState myObj = new CSState();

            myObj.ClientSocket  = ((CSState)state).ClientSocket;
            myObj.myTcpListener = ((CSState)state).myTcpListener;
            myObj.mystring      = "";
            NetworkStream myNetworkStream = new NetworkStream(myObj.ClientSocket);
            int           InBytesCount    = 0;

            byte[] myReceiveBytes = new byte[1024];
            int    i = 0;

            while (true)
            {
                try
                {
                    InBytesCount = myNetworkStream.Read(myReceiveBytes, 0, myReceiveBytes.Length);
                    System.Threading.Thread.Sleep(100);
                    if (InBytesCount == 0)
                    {
                        break;
                    }
                    myObj.mystring = Encoding.GetEncoding(950).GetString(myReceiveBytes).TrimEnd().TrimStart();
                    DisplayMsg1(myObj);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                    return;
                }
            }
        }
Beispiel #2
0
        private void StartListen(object state)
        {
            int iPort = 0;

            iPort         = Convert.ToInt32(state);
            myTcpListener = new TcpListener(IPAddress.Any, iPort);
            try
            {
                Socket ClientSocket = null;
                myTcpListener.Start();
                int iCount = 0;
                ChangeLB("TCP Listener 已啟動", Color.Blue);
                do
                {
                    ClientSocket = myTcpListener.AcceptSocket();
                    if (ClientSocket.Connected == true)
                    {
                        CSState myObj = new CSState();
                        myObj.myTcpListener = myTcpListener;
                        myObj.ClientSocket  = ClientSocket;
                        myObj.mystring      = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "已連線";
                        DisplayMsg1(myObj);
                        Thread ReceiveThread = new Thread(ReceiveData);
                        ReceiveThread.IsBackground = true;
                        ReceiveThread.Start(myObj);
                        iCount += 1;
                    }
                } while (true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        internal static void LogMessage(Message message, CSState value)
        {
            string text;

            text  = $"\nServer received this message:\n";
            text += message.ToString();
            text += $"It was unexpected during {value} server state.\n";
            Logger.LogError(text, ProgramType.CommunicationServer);
        }
Beispiel #4
0
        private void DisplayMsg1(object state)
        {
            CSState myObj = new CSState();

            myObj = (CSState)state;
            if (this.DataGridView1.InvokeRequired)
            {
                SetMsgCallBack d = new SetMsgCallBack(DisplayMsg1);
                this.Invoke(d, new object[] { myObj });
            }
            else
            {
                DataRow xRow = myDatatable.NewRow();
                xRow[0] = ((IPEndPoint)myObj.ClientSocket.RemoteEndPoint).Address.ToString();
                xRow[1] = ((IPEndPoint)myObj.ClientSocket.RemoteEndPoint).Port.ToString();
                xRow[2] = myObj.mystring;
                myDatatable.Rows.Add(xRow);
            }
        }
Beispiel #5
0
 public static void  SetCSState(CSState state)
 {
     m_State = state;
     if (m_State == CSState.Ready)
     {
         CombatReady();
         SetCSState(CSState.Start);
     }
     else if (m_State == CSState.Start)
     {
         m_CombatTime = CombatMaxTime * 1.0f;
     }
     else if (m_State == CSState.Combat)
     {
         //m_pause = false;
     }
     else if (m_State == CSState.End)
     {
         m_CombatTime = 0;
         (SceneM.GetCurIScene() as CombatScene).DoCombatFinish();
     }
 }
 private void DisplayMsg1(object state)
 {
     CSState myObj = new CSState();
     myObj = (CSState)state;
     if (this.DataGridView1.InvokeRequired)
     {
         SetMsgCallBack d = new SetMsgCallBack(DisplayMsg1);
         this.Invoke(d, new object[] {myObj});
     }
     else
     {
         DataRow xRow = myDatatable.NewRow();
         xRow[0] = ((IPEndPoint)myObj.ClientSocket.RemoteEndPoint).Address.ToString();
         xRow[1] = ((IPEndPoint)myObj.ClientSocket.RemoteEndPoint).Port.ToString();
         xRow[2] = myObj.mystring;
         myDatatable.Rows.Add(xRow);
     }
 }
 private void StartListen(object state)
 {
     int iPort = 0;
     iPort = Convert.ToInt32(state);
     myTcpListener = new TcpListener(IPAddress.Any, iPort);
     try
     {
         Socket ClientSocket = null;
         myTcpListener.Start();
         int iCount = 0;
         ChangeLB("TCP Listener �w�Ұ�", Color.Blue);
         do
         {
             ClientSocket = myTcpListener.AcceptSocket();
             if (ClientSocket.Connected == true)
             {
                 CSState myObj = new CSState();
                 myObj.myTcpListener = myTcpListener;
                 myObj.ClientSocket = ClientSocket;
                 myObj.mystring = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "�w�s�u";
                 DisplayMsg1(myObj);
                 Thread ReceiveThread = new Thread(ReceiveData);
                 ReceiveThread.IsBackground = true;
                 ReceiveThread.Start(myObj);
                 iCount += 1;
             }
         } while (true);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.ToString());
     }
 }
 private void ReceiveData(object state)
 {
     CSState myObj = new CSState();
     myObj.ClientSocket = ((CSState)state).ClientSocket;
     myObj.myTcpListener = ((CSState)state).myTcpListener;
     myObj.mystring = "";
     NetworkStream myNetworkStream = new NetworkStream(myObj.ClientSocket);
     int InBytesCount = 0;
     byte[] myReceiveBytes = new byte[1024];
     int i = 0;
     while (true)
     {
         try
         {
             InBytesCount = myNetworkStream.Read(myReceiveBytes, 0, myReceiveBytes.Length);
             System.Threading.Thread.Sleep(100);
             if (InBytesCount == 0)
             {
                 break;
             }
             myObj.mystring = Encoding.GetEncoding(950).GetString(myReceiveBytes).TrimEnd().TrimStart();
             DisplayMsg1(myObj);
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.ToString());
             return;
         }
     }
 }