Click here to Skip to main content
13,193,441 members (40,897 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

47.3K views
3K downloads
42 bookmarked
Posted 12 Mar 2008

Managing Dialup connection

, 30 Nov 2010
Rate this:
Please Sign up or sign in to vote.
Mange your Internet connection .
main.JPG

Introduction

I am going to tell you in this artical about how you can access your PC from outside, using simple mobile Internet connection which is frequently disconnected. Normally our mobile GPRS connection gets Dynamic IP(which is frequently changed). 

   Now i want connect to my FTP floder located at my home PC. So that i created a application, this application automatically connected to the dial-up connection if  it's disconnected, and send a mail to your mail id with the IP address.

Background

I taken some code from the code project for the network quality check.

I used "rasdial.exe" for the dial a dial up connection. located at ("c:\windows\system32\rasdial.exe")

Using the code

Before using this code user have set some modification in their connections, code.

ISP Entry Name: Please take care about the ISP entry name, This name don't put spaces and other special characters.

dialupname.JPG

And also remove the redial option for the dialup connection properties....

dialupredial.JPG

SMTP mail : Change the mail address/password in the code. for sender mail ID. this mail id must be enable with the POP3,SMTP Out .

youruserpass.JPG

#Region "</span />Imports"</span />
 mports System
Imports System.IO
Imports System.Net
Imports System.Net.Mail
#End Region
Public Class Manage
#Region "</span />Declarations"</span />
    '</span />'</span />'</span /> <summary>
    '</span />'</span />'</span />  Process Delaration , used for</span /> the run external command (rasdial.exe)
    '</span />'</span />'</span /> </summary>
    Public MyProcess As New System.Diagnostics.Process
    '</span />'</span />'</span /> <</span />summary></span />
    '</span />'</span />'</span /> Holds the Current IP address
    '</span />'</span />'</span /> <</span />/summary></span />
    Public PresentIPAddress As String</span /> = "</span />"</span />
    '</span />'</span />'</span /> <summary>
    '</span />'</span />'</span /> Quality of the connection (String</span /> Type)
    '</span />'</span />'</span /> </summary>
    Public Conn_Qulity As String = "Bad"
    '</span />'</span />'</span /> <</span />summary></span />
    '</span />'</span />'</span /> windows API function for Internet Connection type and connection quality.
    '</span />'</span />'</span /> <</span />/summary></span />
    Private Declare Function InternetGetConnectedState Lib _
              "</span />wininet.dll"</span /> (ByRef lpSFlags As Int32</span />, _
              ByVal dwReserved As Int32</span />) As Boolean</span />
    '</span />'</span />'</span /> <summary>
    '</span />'</span />'</span /> enum</span /> used for</span /> the internet connection type.
    '</span />'</span />'</span /> </summary>
    Public Enum InetConnState
        modem = &H1
        lan = &H2
        proxy = &H4
        ras = &H10
        offline = &H20
        configured = &H40
    End Enum
#End Region
#Region "Network Handlers/Methods"
    '</span />'</span />'</span /> <</span />summary></span />
    '</span />'</span />'</span /> Network Connection Handler.
    '</span />'</span />'</span /> <</span />/summary></span />
    Private Sub Handle_NetworkAvailabilityChanged()
        AddHandler My.Computer.Network.NetworkAvailabilityChanged, _
           AddressOf MyComputerNetwork_NetworkAvailabilityChanged
        DisplayAvailability(My.Computer.Network.IsAvailable)
    End Sub
    '</span />'</span />'</span /> <summary>
    '</span />'</span />'</span /> Network Availability changing Handler, 
    '</span />'</span />'</span /> </summary>
    '</span />'</span />'</span /> <</span />remarks></span />
    '</span />'</span />'</span /> *** Problems witn when two network connctions are present.
    '</span />'</span />'</span /> <</span />/remarks></span />
    Private Sub MyComputerNetwork_NetworkAvailabilityChanged( _
       ByVal sender As Object</span />, _
       ByVal e As Devices.NetworkAvailableEventArgs)

        DisplayAvailability(e.IsNetworkAvailable)
    End Sub
    '</span />'</span />'</span /> <summary>
    '</span />'</span />'</span /> Display availability methods
    '</span />'</span />'</span /> </summary>
    '</span />'</span />'</span /> <</span />param name="</span />available"</span />></span />
    '</span />'</span />'</span /> True/False
    '</span />'</span />'</span /> <</span />/param></span />
    Private Sub DisplayAvailability(ByVal available As Boolean</span />)
        Application.DoEvents()
        If available = True Then

            '</span />'</span /> Verifiying for</span /> Genuine Connection
            Try
                Application.DoEvents()
                MyProcess.StartInfo.FileName = "</span />c:\myip.bat"</span />
                MyProcess.StartInfo.Arguments = "</span />"</span />
                MyProcess.StartInfo.UseShellExecute = False
                MyProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
                '</span />Process.GetCurrentProcess()
                Application.DoEvents()
                MyProcess.Start()
                MyProcess.WaitForExit()
                MyProcess.Close()
                '</span />System.Diagnostics.Process.Start("</span />c:\myip.bat"</span />, "</span />"</span />)
                Application.DoEvents()
                Delay(5000</span />)
                Dim line As String</span /> = "</span />"</span />
                Using sr As StreamReader = New StreamReader("</span />c:\MYIP.txt"</span />)
                    line = sr.ReadToEnd()
                End Using
                If InStr(UCase(line), UCase(txtISPEntry.Text)) <</span />= 0</span /> Then
                    lblstatus.Text = "</span />Connecting..."</span />
                    AppendStatus("</span />Connecting..."</span />)
                    lblstatus.Refresh()
                    Delay(5000</span />)
                    Application.DoEvents()
                    Call DisplayAvailability(False)
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            lblstatus.Text = "</span />Reading IP Address(s)."</span />
            AppendStatus("</span />Reading IP Address(s)."</span />)
            Dim hostName As String</span /> = System.Net.Dns.GetHostName()
            Delay(2000</span />)
            lblstatus.Text = System.Net.Dns.GetHostByName(hostName).AddressList(0</span />).ToString()
            AppendStatus("</span />IP Address : "</span /> & System.Net.Dns.GetHostByName(hostName).AddressList(0</span />).ToString())
            PresentIPAddress = System.Net.Dns.GetHostByName(hostName).AddressList(0</span />).ToString()
            Beep()
            Beep()
            Beep()
            lblstatus.Text = "</span />Connected."</span />
            AppendStatus("</span />Connected."</span />)
            lblstatus.Refresh()
            Check_Net_Conn_Type()
            Call Send_email()
        Else
            lblstatus.Text = "</span />Disconnected."</span />
            AppendStatus("</span />Disconnected."</span />)
            lblstatus.Refresh()
            lblstatus.Text = "</span />Waiting For Connection..."</span />
            AppendStatus("</span />Waiting For Connection..."</span />)
            lblstatus.Refresh()
            Application.DoEvents()
            lblstatus.Text = "</span />Connecting..."</span />
            AppendStatus("</span />Connecting..."</span />)
            lblstatus.Refresh()
            Try
                MyProcess.StartInfo.FileName = "</span />rasdial.exe"</span />
                If Trim(txtusername.Text) = "</span />"</span /> Or Trim(txtpassword.Text) = "</span />"</span /> Then
                    MyProcess.StartInfo.Arguments = txtISPEntry.Text
                Else
                    MyProcess.StartInfo.Arguments = txtISPEntry.Text & "</span /> "</span /> & txtusername.Text & "</span /> "</span /> & txtpassword.Text
                End If
                MyProcess.StartInfo.UseShellExecute = False
                MyProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal

                '</span />Process.GetCurrentProcess()
                MyProcess.Start()
                MyProcess.WaitForExit()
                MyProcess.Close()
                '</span />System.Diagnostics.Process.Start("</span />rasdial.exe"</span />, TextBox5.Text)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            Try
                Application.DoEvents()
                MyProcess.StartInfo.FileName = "</span />c:\myip.bat"</span />
                MyProcess.StartInfo.Arguments = "</span />"</span />
                MyProcess.StartInfo.UseShellExecute = False
                MyProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
                '</span />Process.GetCurrentProcess()
                Application.DoEvents()
                MyProcess.Start()
                MyProcess.WaitForExit()
                MyProcess.Close()
                '</span />System.Diagnostics.Process.Start("</span />c:\myip.bat"</span />, "</span />"</span />)
                Application.DoEvents()
                Delay(5000</span />)
                Dim line As String</span /> = "</span />"</span />
                Using sr As StreamReader = New StreamReader("</span />c:\MYIP.txt"</span />)
                    line = sr.ReadToEnd()
                End Using
                If InStr(UCase(line), UCase(txtISPEntry.Text)) <</span />= 0</span /> Then
                    lblstatus.Text = "</span />Re-Connecting..."</span />
                    AppendStatus("</span />Re-Connecting..."</span />)
                    lblstatus.Refresh()
                    Delay(5000</span />)
                    Application.DoEvents()
                    Call DisplayAvailability(False)
                End If
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            Beep()
            Beep()
            Beep()
            Application.DoEvents()

        End If
    End Sub
    '</span />'</span />'</span /> <summary>
    '</span />'</span />'</span /> Force disconnect/re-connect by the User.
    '</span />'</span />'</span /> </summary>
    Public Sub Re_Connect()
        Try
            MyProcess.StartInfo.FileName = "rasdial"
            MyProcess.StartInfo.Arguments = txtISPEntry.Text & " /disconnect"
            MyProcess.StartInfo.UseShellExecute = False
            MyProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
            '</span />Process.GetCurrentProcess()
            MyProcess.Start()
            MyProcess.WaitForExit()
            MyProcess.Close()
            Delay(5000</span />)
            Call DisplayAvailability(False)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    '</span />'</span />'</span /> <summary>
    '</span />'</span />'</span /> Creats bat file for</span /> every fresh run of the application.
    '</span />'</span />'</span /> </summary>
    Public Sub Create_MYIP_Batfile()
        Dim sr As New FileStream("c:\myip.bat", FileMode.Create)
        Dim wr As New StreamWriter(sr)
        wr.WriteLine("rasdial >c:\myip.txt")
        wr.Close()
        sr.Close()
    End Sub
    '</span />'</span />'</span /> <</span />summary></span />
    '</span />'</span />'</span /> Chaking for Network connction type.
    '</span />'</span />'</span /> <</span />/summary></span />
    Function Check_Net_Conn_Type() As Boolean</span />
        Dim NetFlags As Long
        If InternetGetConnectedState(NetFlags, 0</span />) Then
            '</span /> True

            If NetFlags And InetConnState.lan Then
                lblconntype.Text = "LAN."
            ElseIf NetFlags And InetConnState.modem Then
                lblconntype.Text = "Modem."
            ElseIf NetFlags And InetConnState.configured Then
                lblconntype.Text = "Configured."
            ElseIf NetFlags And InetConnState.proxy Then
                lblconntype.Text = "Proxy."
            ElseIf NetFlags And InetConnState.ras Then
                lblconntype.Text = "RAS."
            ElseIf NetFlags And InetConnState.offline Then
                lblconntype.Text = "Offline."
            End If
        Else
            lblconntype.Text = "Off"
        End If
        Call CheckInetConnection()
    End Function
    '</span />'</span />'</span /> <</span />summary></span />
    '</span />'</span />'</span /> Checking for Neteork connction quality.
    '</span />'</span />'</span /> <</span />/summary></span />
    Function CheckInetConnection() As Boolean</span />

        Dim lngFlags As Long

        If InternetGetConnectedState(lngFlags, 0</span />) Then
            '</span /> True
            If lngFlags And InetConnState.lan Then
                Select Case Conn_Qulity
                    Case "Good"
                        lblqlty.Text = " Good"
                        Conn_Qulity = "Good"
                    Case "Intermittent"
                        lblqlty.Text = " Good"
                        Conn_Qulity = "Good"
                    Case "Bad"
                        lblqlty.Text = " Intermittent"
                        Conn_Qulity = "Intermittent"
                End Select
                Me.Refresh()
            ElseIf lngFlags And InetConnState.modem Then
                Select Case Conn_Qulity
                    Case "Good"
                        lblqlty.Text = " Good"
                        Conn_Qulity = "Good"
                    Case "Intermittent"
                        lblqlty.Text = " Good"
                        Conn_Qulity = "Good"
                    Case "Bad"
                        lblqlty.Text = " Intermittent"
                        Conn_Qulity = "Intermittent"
                End Select
            ElseIf lngFlags And InetConnState.configured Then
                Select Case Conn_Qulity
                    Case "Good"
                        lblqlty.Text = " Good"
                        Conn_Qulity = "Good"
                    Case "Intermittent"
                        lblqlty.Text = " Good"
                        Conn_Qulity = "Good"
                    Case "Bad"
                        lblqlty.Text = " Intermittent"
                        Conn_Qulity = "Intermittent"
                End Select
            ElseIf lngFlags And InetConnState.proxy Then
                Select Case Conn_Qulity
                    Case "Good"
                        lblqlty.Text = " Good"
                        Conn_Qulity = "Good"
                    Case "Intermittent"
                        lblqlty.Text = " Good"
                        Conn_Qulity = "Good"
                    Case "Bad"
                        lblqlty.Text = " Intermittent"
                        Conn_Qulity = "Intermittent"
                End Select
            ElseIf lngFlags And InetConnState.ras Then
                Select Case Conn_Qulity
                    Case "Good"
                        lblqlty.Text = " Good"
                        Conn_Qulity = "Good"
                    Case "Intermittent"
                        lblqlty.Text = " Good"
                        Conn_Qulity = "Good"
                    Case "Bad"
                        lblqlty.Text = " Intermittent"
                        Conn_Qulity = "Intermittent"
                End Select
            ElseIf lngFlags And InetConnState.offline Then
                Select Case Conn_Qulity
                    Case "Good"
                        lblqlty.Text = " Good"
                        Conn_Qulity = "Good"
                    Case "Intermittent"
                        lblqlty.Text = " Good"
                        Conn_Qulity = "Good"
                    Case "Bad"
                        lblqlty.Text = " Intermittent"
                        Conn_Qulity = "Intermittent"
                End Select
            End If
        Else
            '</span /> False
            Select Case Conn_Qulity
                Case "</span />Good"</span />
                    lblqlty.Text = "</span /> Intermittent"</span />
                    Conn_Qulity = "</span />Intermittent"</span />
                Case "</span />Intermittent"</span />
                    lblqlty.Text = "</span /> Bad"</span />
                    Conn_Qulity = "</span />Bad"</span />
                Case "</span />Bad"</span />
                    lblqlty.Text = "</span /> Bad"</span />
                    Conn_Qulity = "</span />Bad"</span />
            End Select
        End If

    End Function

    '</span />'</span />'</span /> <summary>
    '</span />'</span />'</span /> Timer for</span /> the check network quality frequentkly(2000</span /> milli seconds.)
    '</span />'</span />'</span /> </summary>
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        CheckInetConnection()
        Application.DoEvents()
        Me.Refresh()
    End Sub
#End Region
#Region "Buttons Code"
    '</span />'</span />'</span /> <</span />summary></span />
    '</span />'</span />'</span /> Close Application.
    '</span />'</span />'</span /> <</span />/summary></span />
    Private Sub Button2_Click(ByVal sender As System.Object</span />, ByVal e As System.EventArgs) Handles Button2.Click
        Application.Exit()
    End Sub
    '</span />'</span />'</span /> <summary>
    '</span />'</span />'</span /> Starts Operations.
    '</span />'</span />'</span /> </summary>
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call Create_MYIP_Batfile()
        Call Handle_NetworkAvailabilityChanged()
    End Sub
    '</span />'</span />'</span /> <</span />summary></span />
    '</span />'</span />'</span /> Force Reconnction Button.
    '</span />'</span />'</span /> <</span />/summary></span />
    Private Sub Button3_Click(ByVal sender As System.Object</span />, ByVal e As System.EventArgs) Handles Button3.Click
        Call AppendStatus("</span />Force Disconnction By User."</span />)
        Call Re_Connect()
    End Sub
#End Region
#Region "</span />Functions"</span />
    '</span />'</span />'</span /> <summary>
    '</span />'</span />'</span /> Delay Methods .., in</span /> this</span /> code i used Stopwatch for</span /> exact Delaytime.
    '</span />'</span />'</span /> </summary>
    Public Sub Delay(ByVal Delaytime As Object)
        Dim mystopwatch As New Stopwatch
        mystopwatch.Start()
        Dim mytime As Integer = mystopwatch.ElapsedMilliseconds
        Do
            If mystopwatch.ElapsedMilliseconds > (mytime + Delaytime) Then
                mystopwatch.Stop()
                Exit Do
            End If
        Loop
    End Sub
    '</span />'</span />'</span /> <</span />summary></span />
    '</span />'</span />'</span /> Mail Sending .....
    '</span />'</span />'</span /> <</span />/summary></span />
    Public Sub Send_email()
        If Not chkdntmail.Checked Then
            Dim innlop As Integer
            For innlop = 1</span /> To CInt(txtmailretry.Text)
                Try
                    Dim oMsg As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
                    oMsg.From = New System.Net.Mail.MailAddress("</span />rajeshcragy@yahoo.co.in"</span />, "</span />Manage Dialup."</span />)
                    oMsg.To.Add(New MailAddress(txtemailaddrs.Text, "</span />Manage Dialup."</span />))
                    oMsg.Subject = "</span />Your Internet Connection Info."</span />
                    oMsg.Body = "</span /> *** IP Address: "</span /> & PresentIPAddress & "</span />   [At: "</span /> & Now.ToString & "</span /> ]"</span /> & vbCrLf _
                              & "</span /> *** Connection Type: "</span /> & lblconntype.Text & vbCrLf _
                              & "</span /> *** Connection Quality: "</span /> & lblqlty.Text & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf _
                              & "</span /> Regards..,"</span /> & vbCrLf _
                              & "</span /> Rajesh B..,  -- A Poor Workman Blames His Tools. --"</span /> & vbCrLf _
                              & "</span /> e-mail: rajeshcragy@gmail.com, rajeshcragy@yahoo.co.in."</span /> & vbCrLf _
                              & "</span /> Ph: +919397099540"</span /> & vbCrLf _
                              & "</span /> Hyderabad, Andra Pradesh, India."</span /> & vbCrLf

                    Dim oSmtp As New SmtpClient("</span />smtp.mail.yahoo.com"</span />)
                    oSmtp.DeliveryMethod = SmtpDeliveryMethod.Network
                    Dim oCredential As New NetworkCredential("</span />yourmailid"</span />, "</span />yourpassword"</span />)
                    oSmtp.UseDefaultCredentials = False
                    oSmtp.Credentials = oCredential
                    oSmtp.Send(oMsg)
                    AppendStatus("</span />Message Sent Successfully!"</span />)
                    Exit For
                Catch ex As Exception
                    AppendStatus(ex.Message)
                    AppendStatus("</span />Retrinig to Send Mail...."</span />)
                End Try
            Next
        End If
    End Sub
    '</span />'</span />'</span /> <summary>
    '</span />'</span />'</span /> Records all Status for</span /> further Refrrence.
    '</span />'</span />'</span /> </summary>
    Public Sub AppendStatus(ByVal MessageText As String)
        txtstatus.Items.Add(MessageText & "   [At: " & Now.ToString & " ]")
        txtstatus.SelectedIndex = txtstatus.Items.Count - 1
    End Sub
#End Region
End Class

        </span />

 In this code i covered.., Sending mail from vb.NET,  checking the Internet connection quality.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rajesh Anuhya
Software Developer (Senior)
India India
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 2 Pin
zohaib12121214-Nov-14 12:29
memberzohaib12121214-Nov-14 12:29 
GeneralMy vote of 5 Pin
Nikolay Aph K.1-Feb-13 3:20
memberNikolay Aph K.1-Feb-13 3:20 
GeneralRe: My vote of 5 Pin
Rajesh Anuhya5-Feb-13 21:27
memberRajesh Anuhya5-Feb-13 21:27 
GeneralMy vote of 5 Pin
Аslam Iqbal17-Mar-11 2:11
memberАslam Iqbal17-Mar-11 2:11 
GeneralRe: My vote of 5 Pin
Rajesh Puli17-Mar-11 2:15
memberRajesh Puli17-Mar-11 2:15 
GeneralMy vote of 5 Pin
-~-Tiba-~- 217-Jan-11 20:54
member-~-Tiba-~- 217-Jan-11 20:54 
GeneralRe: My vote of 5 Pin
Rajesh Puli17-Jan-11 22:08
memberRajesh Puli17-Jan-11 22:08 
Generalnice - have 5 Pin
Pranay Rana17-Jan-11 0:59
memberPranay Rana17-Jan-11 0:59 
GeneralRe: nice - have 5 Pin
Rajesh Puli17-Jan-11 22:08
memberRajesh Puli17-Jan-11 22:08 
GeneralMy vote of 5 Pin
Support_Team29-Nov-10 23:07
memberSupport_Team29-Nov-10 23:07 
GeneralRe: My vote of 5 Pin
Rajesh Puli17-Jan-11 22:07
memberRajesh Puli17-Jan-11 22:07 
Rant[My vote of 1] Very poor coding Pin
Paul Pencoed17-Feb-10 0:46
memberPaul Pencoed17-Feb-10 0:46 
QuestionManaging Connection in LAN Pin
Nisha Agrawal6-Oct-09 20:03
memberNisha Agrawal6-Oct-09 20:03 
AnswerRe: Managing Connection in LAN Pin
Rajesh Puli6-Oct-09 21:43
memberRajesh Puli6-Oct-09 21:43 
Questionnice code Pin
3smrodin328-Oct-08 17:00
member3smrodin328-Oct-08 17:00 
AnswerRe: nice code Pin
Fatal Error6-Nov-08 22:29
memberFatal Error6-Nov-08 22:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171017.2 | Last Updated 1 Dec 2010
Article Copyright 2008 by Rajesh Anuhya
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid