E
Ertugrul Söylemez
Hello group,
I'm having difficulty with the TcpListener class. See the code below.
For some reason, as soon as I call the stop() method after having called
start(), suddenly the AsyncCallback is invoked. It runs up to the
lst.EndAcceptTcpClient call, where the program aborts with an
ObjectDisposedException.
I could solve the problem by catching and ignoring the exception, but
this appears incorrect to me. Any ideas?
Thanks,
Ertugrul.
public class DPServer {
private TcpListener listener;
private ServerFunc callback;
public DPServer(ServerFunc f, int port, IPAddress addr) {
listener = new TcpListener(addr, port);
callback = f;
}
public DPServer(ServerFunc f, int port)
: this(f, port, IPAddress.Any) { }
public DPServer(ServerFunc f)
: this(f, 9346, IPAddress.Any) { }
~DPServer() { stop(); }
public void start() {
listener.Start();
listener.BeginAcceptTcpClient(Utils.asyncCbFix2((r, ar) => {
TcpListener lst = (TcpListener)ar.AsyncState;
TcpClient tcp;
tcp = lst.EndAcceptTcpClient(ar);
ThreadPool.QueueUserWorkItem(
obj => { using (tcp) callback(tcp); },
null);
lst.BeginAcceptTcpClient(r, lst);
}), listener);
}
public void stop() {
listener.Stop();
}
}
I'm having difficulty with the TcpListener class. See the code below.
For some reason, as soon as I call the stop() method after having called
start(), suddenly the AsyncCallback is invoked. It runs up to the
lst.EndAcceptTcpClient call, where the program aborts with an
ObjectDisposedException.
I could solve the problem by catching and ignoring the exception, but
this appears incorrect to me. Any ideas?
Thanks,
Ertugrul.
public class DPServer {
private TcpListener listener;
private ServerFunc callback;
public DPServer(ServerFunc f, int port, IPAddress addr) {
listener = new TcpListener(addr, port);
callback = f;
}
public DPServer(ServerFunc f, int port)
: this(f, port, IPAddress.Any) { }
public DPServer(ServerFunc f)
: this(f, 9346, IPAddress.Any) { }
~DPServer() { stop(); }
public void start() {
listener.Start();
listener.BeginAcceptTcpClient(Utils.asyncCbFix2((r, ar) => {
TcpListener lst = (TcpListener)ar.AsyncState;
TcpClient tcp;
tcp = lst.EndAcceptTcpClient(ar);
ThreadPool.QueueUserWorkItem(
obj => { using (tcp) callback(tcp); },
null);
lst.BeginAcceptTcpClient(r, lst);
}), listener);
}
public void stop() {
listener.Stop();
}
}