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個字符的
字符串數據類型可以容納您需要的任何(合理)數量的字符。你有沒有嘗試使用更大的緩衝區? –
什麼是buf2?你確定它是有效的ASCII數據嗎? – shf301
@ p.s.w.g + shf301。我使用buf 32KB :(這行代碼我在應用程序發送文件中使用。這是它:[link](https://www.mediafire.com/?qqbphpr3r2zbl92) –