我在那裏我試圖同時在我的套接字接收數據增長的字節數組代碼的時候,只不過是想增加字節數組時失敗,但確定。這是錯誤的。發送通過套接字大序列化對象使用一個巨大的字節數組
public bool ReceiveObject2(ref Object objRec, ref string sErrMsg)
{
try
{
byte[] buffer = new byte[1024];
byte[] byArrAll = new byte[0];
bool bAllBytesRead = false;
int iRecLoop = 0;
// grow the byte array to match the size of the object, so we can put whatever we
// like through the socket as long as the object serialises and is binary formatted
while (!bAllBytesRead)
{
if (m_socClient.Receive(buffer) > 0)
{
byArrAll = Combine(byArrAll, buffer);
iRecLoop++;
}
else
{
m_socClient.Close();
bAllBytesRead = true;
}
}
MemoryStream ms = new MemoryStream(buffer);
BinaryFormatter bf1 = new BinaryFormatter();
ms.Position = 0;
Object obj = bf1.Deserialize(ms);
objRec = obj;
return true;
}
catch (System.Runtime.Serialization.SerializationException se)
{
objRec = null;
sErrMsg += "SocketClient.ReceiveObject " + "Source " + se.Source + "Error : " + se.Message;
return false;
}
catch (Exception e)
{
objRec = null;
sErrMsg += "SocketClient.ReceiveObject " + "Source " + e.Source + "Error : " + e.Message;
return false;
}
}
private byte[] Combine(byte[] first, byte[] second)
{
byte[] ret = new byte[first.Length + second.Length];
Buffer.BlockCopy(first, 0, ret, 0, first.Length);
Buffer.BlockCopy(second, 0, ret, first.Length, second.Length);
return ret;
}
錯誤:mscorlibError:輸入流不是有效的二進制格式。起始內容(以字節爲單位)是:68-61-73-43-68-61-6E-67-65-73-3D-22-69-6E-73-65-72 ...
然而,當我只是欺騙並使用MASSIVE緩衝區大小的罰款。
public bool ReceiveObject(ref Object objRec, ref string sErrMsg)
{
try
{
byte[] buffer = new byte[5000000];
m_socClient.Receive(buffer);
MemoryStream ms = new MemoryStream(buffer);
BinaryFormatter bf1 = new BinaryFormatter();
ms.Position = 0;
Object obj = bf1.Deserialize(ms);
objRec = obj;
return true;
}
catch (Exception e)
{
objRec = null;
sErrMsg += "SocketClient.ReceiveObject " + "Source " + e.Source + "Error : " + e.Message;
return false;
}
}
這真的讓我很難過。我不知道爲什麼它不工作。我已經從這裏提出了一個建議,所以我很確定這不是做錯事情?
我希望有人能指出我要去的地方錯了
Gaargh!我認爲這將是答案。不幸的是,它現在讀入循環並在沒有剩餘字節時掛起。我不知道爲什麼會發生這種情況,因爲我認爲它會返回零然後退出。 – FinancialRadDeveloper 2010-01-26 11:02:09
我想我會需要檢查讀取的字節,如果這是比緩衝區小於退出循環爲讓到它掛結束。我想它等待另一個發送,因爲它完成了第一個接收,如果你打電話給另一個,那麼你會期待另一個數據加載。 – FinancialRadDeveloper 2010-01-26 11:45:18
@AlanR - 在這種情況下,請檢查遠程流是否已關閉。如果意圖將其打開,那麼是的:您需要通過一定數量的數據跟蹤自己的進度。 – 2010-01-26 12:38:35