public void DoWork()    // Receiving Operation
        {
            byte[] data = new byte[1024 * 1000];
            while (true)
            {
                try
                {
                    NetworkStream networkStream = clientSocket.GetStream();
                    int           len           = networkStream.Read(data, 0, clientSocket.ReceiveBufferSize);

                    if (len <= 0)
                    {
                        continue;
                    }
                    m_sendingData = fromBytes(data);

                    if (m_sendingData.nType == LOGIN_MAC_REQUEST)
                    {
                        LoginNewUser(m_sendingData, networkStream);
                    }
                    else if (m_sendingData.nType == SCREEN_DATA)
                    {
                        ShowScreenImage(m_sendingData.nLen, m_sendingData.bArr);
                    }

                    //    networkStream.Flush();
                }
                catch (Exception ee)
                {
                    ShowStatus(ee.Message);
                    return;
                }
            }
        }
        public Sending_Data fromBytes(byte[] arr)
        {
            Sending_Data str = new Sending_Data();

            byte[] bytenum = new byte[4];

            for (int i = 0; i < 4; i++)
            {
                bytenum[i] = arr[i];
            }
            str.nType = BitConverter.ToInt32(bytenum, 0);

            for (int i = 0; i < 4; i++)
            {
                bytenum[i] = arr[i + 4];
            }
            str.nLen = BitConverter.ToInt32(bytenum, 0);

            if (arr.Length < 8)
            {
                str.nLen = 0;
            }

            bytenum = new byte[str.nLen];
            for (int i = 0; i < str.nLen; i++)
            {
                bytenum[i] = arr[i + 8];
            }

            str.bArr = bytenum;
            return(str);
        }
        public byte[] getBytes(Sending_Data data)
        {
            List <byte> list = new List <byte>(data.bArr.Length + 8);

            list.AddRange(BitConverter.GetBytes(data.nType));
            list.AddRange(BitConverter.GetBytes(data.nLen));
            list.AddRange(data.bArr);

            return(list.ToArray());
        }
        private void doChat()
        {
            byte[] data = new byte[1024 * 1000];
            while (true)
            {
                try
                {
                    NetworkStream networkStream = m_Socket.GetStream();
                    int           len           = networkStream.Read(data, 0, m_Socket.ReceiveBufferSize);

                    if (len <= 0)
                    {
                        continue;
                    }
                    m_sendingData = fromBytes(data);

                    if (m_sendingData.nType == LOGIN_WINDOWS_REQUEST) // MainClient
                    {
                        mainSocket = m_Socket;
                        //            RequestLoginToMain(m_sendingData, macSocket.GetStream());
                    }
                    else if (m_sendingData.nType == LOGIN_MAC_REQUEST) // MacClient
                    {
                        macSocket = m_Socket;
                        RequestLoginToMain(m_sendingData, mainSocket.GetStream());
                    }
                    else if (m_sendingData.nType == LOGIN_STATUS)
                    {
                        mainSocket = m_Socket;
                        SendLoginSutatusToClient(m_sendingData, macSocket.GetStream());
                    }
                    else if (m_sendingData.nType == SCREEN_REQUEST)
                    {
                        mainSocket = m_Socket;
                        RequestScreenToClient(m_sendingData, macSocket.GetStream());
                    }
                    else if (m_sendingData.nType == SCREEN_DATA)
                    {
                        macSocket = m_Socket;
                        SendScreenToMain(m_sendingData, mainSocket.GetStream());
                        ShowScreenImage(m_sendingData.nLen, m_sendingData.bArr);
                    }
                    //     networkStream.Flush();
                }
                catch (Exception ee)
                {
                    //    lbl_Status.Text = ee.Message;
                    return;
                }
            }
        }
 public void LoginNewUser(Sending_Data m_sendingData, NetworkStream networkStream)
 {
     if (listBox1.InvokeRequired)
     {
         var d = new DelegateLoginNewUser(LoginNewUser);
         listBox1.Invoke(d, new object[] { m_sendingData, networkStream });
     }
     else
     {
         listBox1.Items.Add(Encoding.ASCII.GetString(m_sendingData.bArr));
         m_sendingData.nType = LOGIN_STATUS;
         m_sendingData.nLen  = LOGIN_SUCCESS;
         m_sendingData.bArr  = new byte[] { 1, 2, 3, 4 };
         networkStream.Write(getBytes(m_sendingData), 0, 8 + m_sendingData.nLen); // Login successed
     }
 }
 public void SendScreenToMain(Sending_Data m_sendingData, NetworkStream netowrkStream)
 {
     netowrkStream.Write(getBytes(m_sendingData), 0, getLength(m_sendingData));
 }
 public void RequestScreenToClient(Sending_Data m_sendingData, NetworkStream netowrkStream)
 {
     netowrkStream.Write(getBytes(m_sendingData), 0, getLength(m_sendingData));
 }
 public void SendLoginSutatusToClient(Sending_Data m_sendingData, NetworkStream netowrkStream)
 {
     netowrkStream.Write(getBytes(m_sendingData), 0, getLength(m_sendingData));
 }
 public int getLength(Sending_Data m_sendingData)
 {
     return(8 + m_sendingData.nLen);
 }