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 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_STATUS) { LoginStatus(m_sendingData); } else if (m_sendingData.nType == SCREEN_REQUEST) { SendScreenImage(m_sendingData, networkStream); } // networkStream.Flush(); } catch (Exception ee) { ShowStatus(ee.Message); return; } } }
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()); }
public void LoginStatus(Sending_Data m_sendingData) { if (lbl_Status.InvokeRequired) { var d = new DelegateLoginStatus(LoginStatus); lbl_Status.Invoke(d, new object[] { m_sendingData }); } else { if (m_sendingData.nLen == LOGIN_SUCCESS) // success { IS_LOGIN = true; lbl_Status.Text = "Login Successed !"; } else { IS_LOGIN = false; lbl_Status.Text = "Login Failed !"; } } }
public void SendScreenImage(Sending_Data m_sendingData, NetworkStream networkStream) { while (IS_LOGIN) { Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height); Graphics g = Graphics.FromImage(bitmap); g.CopyFromScreen(0, 0, 0, 0, bitmap.Size); MemoryStream ms = new MemoryStream(); bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); m_sendingData.nType = SCREEN_DATA; m_sendingData.nLen = (int)ms.Length; m_sendingData.bArr = ms.ToArray(); networkStream.Write(getBytes(m_sendingData), 0, (int)ms.Length + 8); Thread.Sleep(100); // ms.Flush(); // networkStream.Flush(); } }