求救~socket 文件接收只能获得4M内容,后面的数据库包获取不到啊.
我捣鼓这个问题弄了一个星期了,实在搞不明白。真是没办法了,所以推上首页问问。阿弥陀佛
这是两个方法:
/// <summary> /// 获取标示 /// </summary> /// <param name="s"></param> /// <returns></returns> private static byte[] ReceiveVarFlag(Socket s) { int recv; byte[] datasize = new byte[4]; recv = s.Receive(datasize, 0, 4, SocketFlags.None); return datasize; } /// <summary> /// 按照长度获取内容 /// </summary> /// <param name="s"></param> /// <returns></returns> private static byte[] ReceiveVarContent(Socket s,int len) { int recv; byte[] datasize = new byte[len]; recv = s.Receive(datasize, 0, len, SocketFlags.None); return datasize; }
这是main函数:
static string filesfolder = "c:\\xxx\\"; static void Main(string[] args) { int port = 12345; //string hostip = "127.0.0.1"; //IPAddress ip = IPAddress.Parse(hostip); IPEndPoint ipe = new IPEndPoint(IPAddress.Any, port); Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); s.Bind(ipe); s.Listen(0); Console.WriteLine("Waitforconnect"); while (true) { int packsiez = 4; Socket temp = s.Accept(); byte[] biaoshi = new byte[4]; byte[] nssid = new byte[4]; byte[] urllen = new byte[4]; byte[] filelen = new byte[4]; //------------ //------------ biaoshi = ReceiveVarFlag(temp); nssid = ReceiveVarFlag(temp); urllen = ReceiveVarFlag(temp); int _urllen = BitConverter.ToInt32(urllen, 0); byte[] urlcontent = new byte[_urllen]; urlcontent = ReceiveVarContent(temp, _urllen); filelen = ReceiveVarFlag(temp); int _biaoshi = BitConverter.ToInt32(biaoshi, 0); int _nssid = BitConverter.ToInt32(nssid, 0); string _urlcontent = Encoding.Default.GetString(urlcontent); Random r = new Random(); string filename = DateTime.Now.ToString("yyyyMMddHHmmss") + r.Next(); int _filelen = BitConverter.ToInt32(filelen, 0); int rescount = _filelen / packsiez; int lastpacksizi = _filelen % packsiez; if (lastpacksizi!=0) { rescount++; } string SendFileName = filesfolder + filename + ".rar"; int wcount=0; using (FileStream MyFileStream = new FileStream(SendFileName, FileMode.Create, FileAccess.Write)) { for (int i = 0; i <rescount; i++) { if (temp.Connected) { if (i==rescount-1) { packsiez = lastpacksizi; } byte[] t=new byte[packsiez]; int counts= temp.Receive(t, 0, packsiez, SocketFlags.None); if (counts==0) { break; } MyFileStream.Write(t, 0, packsiez); wcount+=packsiez; t = null; } else { break; } } } Console.WriteLine("-----------------------------------------------"); Console.WriteLine("包总长:" + _filelen.ToString() +"保存:" + wcount ); Console.WriteLine("文件内容写入:" + SendFileName); WriteInfos(_biaoshi, _nssid, _urllen, _urlcontent, filename); //写入日志信息 } }
我每次大概只能获得4100KB的东西。
如果不退出,继续获取,那么获得的全是空。最后用UE看文件,文件大小是对上了,但是后半截内容全是0x00