Graceful disconnection from server

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, I am writing an terminal app that would allow you to connect to POP3
server and receive mail data in raw format. Now the POP3 server expects a
graceful disconnection by sending a "quit\r\n" command to it. The server then
replies you with a "+OK ... Goodbye" message.

Now I have a code as follows for the disconnect button:---
<pre>
private void btnDisconnect_Click(object sender, System.EventArgs e)
{
if (serverSock != null)
{
if (serverSock.Connected)
{
Send(serverSock, "quit\r\n");
sendDone.WaitOne();
serverSock.Shutdown(SocketShutdown.Both);
serverSock.Close();
serverSock = null;
}
}
}
</pre>

Now the problem arises when this code executes. By the time the "quit\r\n"
command is sent to the server and the server sends a response message, the
socket object is already null and an Object Already disposed exception
arises..
Is there anything I can do to make the system wait asynchronously before the
socket is closed ???
 
Set a class field indicating that you have sent the quit command. Move the
socket.Close() call, and other cleanup code, into a separate method (or
try/catch/finally block or a using block). In the method where you are
receiving your data,test for the field you set. If the field is set, and
the received bytes are OK, then call your cleanup method.

HTH

DalePres
MCAD, MCDBA, MCSE
 
Back
Top