public bool Send(Socket socket, byte command, string text) { try { //byte[] sendb = System.Text.Encoding.UTF8.GetBytes(text); //byte[] lens = System.Text.Encoding.UTF8.GetBytes(sendb.Length.ToString()); //byte[] b = new byte[2 + lens.Length + sendb.Length]; //b[0] = command; //b[1] = (byte)lens.Length; //从指定的目标数组索引处开始,将当前一维数组的所有元素复制到指定的一维数组中 ,将lens所有元素复制到b中,从b的第2个元素开始 //lens.CopyTo(b, 2); //将sendb所有元素复制到b中,从(2+lens长度)开始 // sendb.CopyTo(b, 2 + lens.Length); //最终b的结构是[0] 是command的byte // [1]是text数据转换为byte后的长度length //[2]从2往后是实际的text数据转换为byte的数据 byte[] b = WeaveServerHelper.CodingProtocol(command, text); int slen = 40960; //40kb左右 if (socketLisener.ProtocolType == ProtocolType.Udp) { slen = 520; } int count = (b.Length <= slen ? b.Length / slen : (b.Length / slen) + 1); if (count == 0) { socket.Send(b); } else { //只有在slen 大于40959,约40kb 才会进入这个方法,实现数据分页发送 for (int i = 0; i < count; i++) { int zz = b.Length - (i * slen) > slen ? slen : b.Length - (i * slen); byte[] temp = new byte[zz]; Array.Copy(b, i * slen, temp, 0, zz); socket.Send(temp); System.Threading.Thread.Sleep(1); } } } catch { return(false); } // tcpc.Close(); return(true); }
public bool Send(Socket socket, byte command, byte[] text) { try { int slen = 40960; if (socketLisener.ProtocolType == ProtocolType.Udp) { slen = 520; } //byte[] sendb = text; //byte[] lens = WeaveServerHelper.ConvertToByteList(sendb.Length); //byte[] b = new byte[2 + lens.Length + sendb.Length]; //b[0] = command; //b[1] = (byte)lens.Length; //lens.CopyTo(b, 2); //sendb.CopyTo(b, 2 + lens.Length); //最终b的结构是[0] 是command的byte // [1]是text数据转换为byte后的长度length //[2]从2往后是实际的text数据转换为byte的数据 byte[] b = WeaveServerHelper.CodingProtocol(command, text); int count = (b.Length <= slen ? b.Length / slen : (b.Length / slen) + 1); if (count == 0) { socket.Send(b); } else { for (int i = 0; i < count; i++) { int zz = b.Length - (i * slen) > slen ? slen : b.Length - (i * slen); byte[] temp = new byte[zz]; Array.Copy(b, i * slen, temp, 0, zz); socket.Send(temp); System.Threading.Thread.Sleep(1); } } } catch { return(false); } // tcpc.Close(); return(true); }
public bool send(int index, byte command, string text) { try { Socket socket = weaveNetworkItems[index].SocketSession; //byte[] sendb = Encoding.UTF8.GetBytes(text); //byte[] lens = Encoding.UTF8.GetBytes(sendb.Length.ToString()); //byte[] b = new byte[2 + lens.Length + sendb.Length]; //b[0] = command; //b[1] = (byte)lens.Length; //lens.CopyTo(b, 2); //sendb.CopyTo(b, 2 + lens.Length); byte[] b = WeaveServerHelper.CodingProtocol(command, text); socket.Send(b); } catch { return(false); } // tcpc.Close(); return(true); }