string s1 = Encoding.ASCII.GetString(buf2);
我看到类型字符串只限制8个字节(8个字符)。大家可以告诉我如何接收超过8个字节的方式? 详情:
服务器
byte[] buf = Encoding.ASCII.GetBytes(fileSize.ToString());
MemoryStream ms = new MemoryStream();
ms.Write(buf,0,buf.Length);
ms.Seek(0, SeekOrigin.Begin);
byte[] buf2 = new byte[32*1024];
ms.Read(buf2,0,buf2.Length);
FileStream fs = File.OpenRead(savefilename);
MessageBox.Show("1 " + buf2.Length +" "+ fileSize+" "+ms.Length);
int ns = sendSocket.Send(buf2, sizeof(ulong), SocketFlags.None);
客户端
byte[] buf = new byte[32 * 1024];
int nr = listenSocket2.Receive(buf, sizeof(ulong), SocketFlags.None);
MemoryStream ms = new MemoryStream();
ms.Write(buf, 0, buf.Length);
ms.Seek(0, SeekOrigin.Begin);
byte[] buf2 = new byte[32 * 1024];
ms.Read(buf2, 0, buf2.Length);
string s1 = Encoding.ASCII.GetString(buf2);
MessageBox.Show("" + s1+" "+ms.Length);
ulong fileSize = UInt64.Parse(s1);
s1不超过8个字符
答案 0 :(得分:1)
以下一行
int nr = listenSocket2.Receive(buf, sizeof(ulong), SocketFlags.None);
从流中读取前8个字节。这意味着包含文件的 size 。您应该发出另一个Receive
来接收实际的文件内容。