/// <summary> /// Disconnect /// </summary> /// <returns></returns> public int Disconnect() { if (IsConnected) { try { core.DebugLog("Closing"); if (streamWriter != null) { streamWriter.Close(); streamWriter.Dispose(); streamWriter = null; } if (streamReader != null) { streamReader.Close(); streamReader.Dispose(); streamReader = null; } if (networkStream != null) { networkStream.Close(); networkStream.Dispose(); networkStream = null; } connected = false; } catch (Exception fail) { core.handleException(fail); } } return(0); }
public override void Dispose() { base.Dispose(); Reader.Dispose(); Writer.Dispose(); netStream.Dispose(); Stream.Dispose(); }
internal static void Stop() { stop = true; if (hostToNetsms != null) { lock (hostToNetsms) { foreach (KeyValuePair <string, System.Net.Sockets.NetworkStream> pair in hostToNetsms) { System.Net.Sockets.NetworkStream netsm = pair.Value; try { netsm.WriteByte((byte)'s'); //Stop heartbeat } catch { } finally { netsm.Close(1000); netsm.Dispose(); } } } } if (receivethds != null) { foreach (System.Threading.Thread thd in receivethds) { try { thd.Abort(); } catch { } } } if (monitorthd != null) { try { monitorthd.Abort(); } catch { } } }
/****************************************************************************************** * <summary> * POP Before SMTP認証のため、POPサーバに接続 * <param name="strUser"></param> * <param name="strPass"></param> */ public void PopBeforeSmtp(String strUser, String strPass) { System.Net.Sockets.NetworkStream stream = null; System.Net.Sockets.TcpClient tcp = null; try{ String strResult; tcp = new System.Net.Sockets.TcpClient(); tcp.Connect(SERVER, PORT); stream = tcp.GetStream(); strResult = WriteAndRead(stream, ""); if (strResult.IndexOf("+OK") != 0) { throw new Exception("Failed: POP Server Connection."); } strResult = WriteAndRead(stream, "USER " + strUser + "\r\n"); if (strResult.IndexOf("+OK") != 0) { throw new Exception("Error: User ID."); } strResult = WriteAndRead(stream, "PASS " + strPass + "\r\n"); if (strResult.IndexOf("+OK") != 0) { throw new Exception("Error: Pass"); } strResult = WriteAndRead(stream, "STAT" + "\r\n"); if (strResult.IndexOf("+OK") != 0) { } strResult = WriteAndRead(stream, "QUIT" + "\r\n"); } catch (Exception e) { throw e; } finally{ if (stream != null) { stream.Close(); stream.Dispose(); } if (tcp != null) { tcp.Close(); } } }
public void DestroyConnection() { _continue = false; Close(); if (_networkStream != null) { _networkStream.Dispose(); _networkStream = null; } if (_client != null) { _client = null; } }
private void button2_Click(object sender, EventArgs e) { //close everything down before we leave. if (thisClient != null) { connected = false; try { networkStream.Close(); thisClient.Close(); networkStream.Dispose(); } catch (Exception ex) { errDisp(ex); } lblStatus.Text = "OFF-LINE"; lblStatus.ForeColor = System.Drawing.Color.Red; } this.Close(); }
public void Dispose() { _stream.Dispose(); _client.Dispose(); IsConnected = false; }
/// <summary> /// Pop Before Smtp認証のためPOPサーバに接続 /// </summary> /// <param name="serv">POPサーバー</param> /// <param name="port">POPポート番号</param> /// <param name="user">ユーザID</param> /// <param name="pass">パスワード</param> public void PopBeforeSmtp( String serv, int port, String user, String pass) { System.Net.Sockets.NetworkStream stream = null; System.Net.Sockets.TcpClient client = null; try { String rstr; client = new System.Net.Sockets.TcpClient(); // POPサーバーに接続 client.Connect(serv, port); stream = client.GetStream(); // POPサーバー接続時のレスポンス受信 rstr = WriteAndRead(stream, ""); if (rstr.IndexOf("+OK") != 0) { throw new Exception("POPサーバー接続エラー"); } // ユーザIDの送信 rstr = WriteAndRead(stream, "USER " + user + "\r\n"); if (rstr.IndexOf("+OK") != 0) { throw new Exception("ユーザIDエラー"); } // パスワードの送信 rstr = WriteAndRead(stream, "PASS " + pass + "\r\n"); if (rstr.IndexOf("+OK") != 0) { throw new Exception("パスワードエラー"); } //// APOPの場合は[ユーザID送信]と[パスワード送信]の処理を以下のように変更します //// POPサーバー接続時のレスポンスからAPOP用のキー(<>で囲まれた部分)を取得して //// パスワードと連結(例:"<*****@*****.**>PASS")してMD5(HEX)変換して //// "APOP user MD5(HEX)"形式で送信します //Byte[] byt = System.Text.Encoding.ASCII.GetBytes("<*****@*****.**>" + pass); //System.Security.Cryptography.MD5CryptoServiceProvider md5 = // new System.Security.Cryptography.MD5CryptoServiceProvider(); //Byte[] res = md5.ComputeHash(byt); //String aps = BitConverter.ToString(res).Replace("-", "").ToLower(); //rstr = WriteAndRead(stream, "APOP " + user + " " + aps + "\r\n"); //if (rstr.IndexOf("+OK") != 0) //{ // throw new Exception("ユーザIDまたはパスワードエラー"); //} // ステータスの送信 rstr = WriteAndRead(stream, "STAT" + "\r\n"); if (rstr.IndexOf("+OK") != 0) { throw new Exception("STATエラー"); } // 終了の送信 rstr = WriteAndRead(stream, "QUIT" + "\r\n"); } catch (Exception ex) { throw ex; } finally { if (stream != null) { stream.Close(); stream.Dispose(); } if (client != null) { client.Close(); } } }