private void AsyncSend(Socket client, DataSendType type, string p = "", byte[] value = null) { //if (client == null || p == string.Empty) return; //数据转码 byte[] data = new byte[1024]; //MemoryStream ms = new MemoryStream(); //BinaryWriter bw = new BinaryWriter(ms, new UTF8Encoding()); //bw.Write() byte sendType = (byte)type; byte[] length = BitConverter.GetBytes(value.Length + 1 + 4); //byte[] message = Encoding.UTF8.GetBytes(p); List <byte> allMessage = new List <byte>(); allMessage.Add(sendType); allMessage.AddRange(length); allMessage.AddRange(value); byte[] allMessageBytes = allMessage.ToArray(); Debug.Log("ServiceSendType:" + (DataSendType)((int)allMessage[0])); byte[] lengthRecive = new byte[4]; Array.Copy(allMessageBytes, 1, lengthRecive, 0, 4); int messageLength = BitConverter.ToInt32(lengthRecive, 0); Debug.Log("messageLength:" + messageLength); byte[] messageRecive = new byte[messageLength]; Array.Copy(allMessageBytes, 5, messageRecive, 0, messageLength - 5); string filename = Application.streamingAssetsPath + "/Screenshot.jpg"; System.IO.File.WriteAllBytes(filename, messageRecive); Debug.Log(string.Format("截屏了一张图片: {0}", filename)); Debug.Log(Encoding.UTF8.GetString(messageRecive)); data = Encoding.UTF8.GetBytes(p); try { //开始发送消息 client.BeginSend(data, 0, data.Length, SocketFlags.None, asyncResult => { //完成消息发送 client.EndSend(asyncResult); //输出消息 Debug.Log(string.Format("服务器发出消息:{0}", p)); }, null); } catch (Exception e) { Debug.Log(e.Message); } }
public void Clear() { type = DataSendType.NONE; dataLength = 0; data = new byte[0]; }