如果我有来自
的回调listener.BeginAcceptSocket(cbConnect, listener);
和我的回调方法:
private void cbConnect(IAsyncResult ar)
{
TcpListener listener = (TcpListener)ar.AsyncState;
MyObj myObj = new MyObj();
myObj.socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
myObj.socket = listener.EndAcceptSocket(ar);
listener.BeginAcceptSocket(cbConnect, listener);//start listening again
myObj.socket.BeginReceive(myObj.buffer, 0, myObj.buffer.Length, 0, cbReceive, myObj);
}
这是MyObj类:
Public class MyObj
{
public Socket socket;
public byte[] buffer = new byte[1080];
}
我的问题是,如果连接另一端的客户端从未向myObj发送任何数据? 我如何关闭套接字并清理资源?
答案 0 :(得分:1)
如果远程连接断开,套接字应该通过垃圾收集器对其进行清理。