[英]ObjectDisposedException while using socket
我的類庫遇到了很大的麻煩,該類庫用於通過套接字獲取和發送文本消息。 這是我的簡單代碼:
private void AcceptNewSocket()
{
Socket socket = list.AcceptSocket();
socket.ReceiveBufferSize = 1001;
Thread socketTh = new Thread(new ThreadStart(AcceptNewSocket));
socketTh.Start();
WaitForMessenge(socket);
}
private void WaitForMessenge(Socket socket)
{
byte[] buff = new byte[1001];
int bufcount = socket.Receive(buff);
/////////some operations on byte array
while(bufcount > 0)
{
buffcount = socket.Receive(buff); //HERE it throws ObjectDisposedException and source is socket
/////some operations on byte array
}
}
當我在控制台應用程序中使用我的庫時,也不例外。 當我在Windows窗體應用程序中使用它時,它就會發生。 你能幫助我嗎 ?
AcceptNewSocket()對我來說真的很可疑 。 我強烈建議對其進行審查。
list.AcceptSocket()有什么作用? 這可能是放置插槽的可能位置。
為什么要遞歸創建線程? 這可能會產生數十個線程,因此在處理套接字時它們將引發異常。
很好...除此之外,我將使用ThreadPool類而不是Threads以獲得更好的性能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.