none
Can't find IPAddress when working with remote desktop connection RRS feed

  • Question

  • Hi everyone,

    I found a site that explain how to create a remote desktop connection using vb.net, however, the code is 7 years old and a part of it was deprecated, here is the code:

    FrmRemoteDesktop.vb

    Imports System.Net.Sockets
    Imports System.Threading
    Imports System.Drawing
    Imports System.Runtime.Serialization.Formatters.Binary
    
    Public Class FrmRemoteDesktop
        Dim client As New TcpClient
        Dim port As Integer
        Dim server As TcpListener
        Dim ns As NetworkStream
        Dim listening As New Thread(AddressOf Listen)
        Dim GetImage As New Thread(AddressOf ReceiveImage)
    
        Private Sub ReceiveImage()
            Dim bf As New BinaryFormatter
            While client.Connected = True
                ns = client.GetStream
                PictureBox1.Image = bf.Deserialize(ns)
            End While
        End Sub
        Private Sub Listen()
            While client.Connected = False
                server.Start()
                client = server.AcceptTcpClient
            End While
            GetImage.Start()
        End Sub
        Private Sub FrmRemoteDesktop_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            port = Integer.Parse(FrmPortListening.PortTB.Text)
            server = New TcpListener(port)
            listening.Start()
        End Sub
    End Class

    The error is on server = New TcpListener(port)

    the message: 

    Public Overloads Sub New(port As Integer) est obsolete: This methos has been deprecated. Please use TcpListener(IPAddress localaddr, intport) instead. 
    http://go.microsoft.com/fwlink/?linkid=14202'

    I tried to use this:

    Dim ip As IPAddress = IPAddress.Parse("0.0.0.0")
    Dim port As Integer = 6335
    Dim listener As New TcpListener(ip, port)

    but the error now says: IPAddress is not declared, it might be unavailable due to its protection level.

    Someone can help?

    Thanks Claude from Quebec, Canada

    Sunday, September 6, 2020 3:20 PM

Answers

  • Hi,

    Hundreds of views and no replies, may be it is to complicated, here is the solution I found after many hours of trials and errors.

    athe code:

    Imports System.Net.Sockets
    Imports System.Threading
    Imports System.Drawing
    Imports System.Runtime.Serialization.Formatters.Binary
    Public Class FrmRemoteDesktopClient
        Private SQL As New SQLControl
        Public Sub LoadGrid(Optional Query As String = "")
            If Query = "" Then
                SQL.ExecQuery("SELECT * FROM RemoteDesktop;")
            Else
                SQL.ExecQuery(Query)
            End If
            'ERROR HANDLING
            If SQL.HasException(True) Then Exit Sub
            With DataGridView1
                .AutoGenerateColumns = True
                .DataSource = SQL.DBDT
                .Columns(0).HeaderText = "ID"
                .Columns(1).HeaderText = "ClientName"
                .Columns(2).HeaderText = "ServerName"
                .Columns(3).HeaderText = "UserName"
                .Columns(4).HeaderText = "IPAddress"
                .Columns(5).HeaderText = "Port"
                .Columns(0).Width = 75
                .Columns(1).Width = 200
                .Columns(2).Width = 200
                .Columns(3).Width = 200
                .Columns(4).Width = 200
                .Columns(5).Width = 100
            End With
        End Sub
        Private Sub BtnConnect_Click(sender As Object, e As EventArgs) Handles BtnConnect.Click
            Dim process = New System.Diagnostics.Process()
            process.StartInfo = New ProcessStartInfo With {
             .FileName = "mstsc",
            .Arguments = "/v:" & IPAddressTB.Text
            }
            process.Start()
        End Sub
        Public Sub LoadFirstRecord()
            Try
                'CLEAR EXISTING RECORD
                If SQL.DBDS IsNot Nothing Then
                    SQL.DBDS.Clear()
                End If
                SQL.RunQuery("SELECT * FROM RemoteDesktop Where ID = 1")
                If SQL.DBDS.Tables(0).Rows(0).Item(0) >= 1 Then
                    Me.IDTB.Text = SQL.DBDS.Tables(0).Rows(0).Item("ID")
                    Me.ClientNameTB.Text = SQL.DBDS.Tables(0).Rows(0).Item("ClientName")
                    Me.ServerNameTB.Text = SQL.DBDS.Tables(0).Rows(0).Item("ServerName")
                    Me.UsernameTB.Text = SQL.DBDS.Tables(0).Rows(0).Item("Username")
                    Me.IPAddressTB.Text = SQL.DBDS.Tables(0).Rows(0).Item("IPAddress")
                    Me.PortTB.Text = SQL.DBDS.Tables(0).Rows(0).Item("Port")
                Else
                    MsgBox("There Is no connection 1, please ask the system administrator!", MsgBoxStyle.Critical, "NO RECORD FOUND")
                End If
                If SQL.HasException(True) Then Exit Sub
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
        Private Sub FrmRemoteDesktopClient_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'TODO: cette ligne de code charge les données dans la table 'AcDataDS.RemoteDesktop'. Vous pouvez la déplacer ou la supprimer selon les besoins.
            Me.RemoteDesktopTableAdapter.Fill(Me.AcDataDS.RemoteDesktop)
            Me.CountRecords.Text = BindingNavigatorCountItem.ToString
            LoadGrid()
            Me.RecordsCountTB.Text = SQL.DBDT.Rows.Count.ToString
            LoadClientsNames()
            LoadFirstRecord()
            ButtonFirst.PerformClick()
        End Sub
        Private Sub FindItem()
            SQL.AddParam("@item", "%" & TxtFilterTB.Text & "%")
            LoadGrid("SELECT * FROM RemoteDesktop WHERE ClientName LIKE @item;")
        End Sub
        Private Sub IDTB_TextChanged(sender As Object, e As EventArgs) Handles IDTB.TextChanged
            Me.CheckBox1.Checked = False
        End Sub
        Public Sub LoadClientsNames()
            Try
                'REFRESH COMBOBOX
                ClientNameTB.Items.Clear()
                'RUN QUERY
                SQL.ExecQuery("Select DISTINCT ID, ClientName FROM RemoteDesktop ORDER BY ID ASC;")
                If SQL.HasException(True) Then Exit Sub
                'LOOP ROW & ADD TO COMBOBOX
                For Each r As DataRow In SQL.DBDT.Rows
                    ClientNameTB.Items.Add(r("ClientName").ToString)
                Next
                If SQL.HasException(True) Then Exit Sub
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
    
        Private Sub BtnClose_Click(sender As Object, e As EventArgs) Handles BtnClose.Click
            Me.Close()
        End Sub
    
        Private Sub BtnNormal_Click(sender As Object, e As EventArgs) Handles BtnNormal.Click
            WindowState = FormWindowState.Normal
            Me.BtnMaximize.Visible = True
            Me.BtnNormal.Visible = False
        End Sub
    
        Private Sub BtnMinimize_Click(sender As Object, e As EventArgs) Handles BtnMinimize.Click
            WindowState = FormWindowState.Minimized
        End Sub
    
        Private Sub BtnMaximize_Click(sender As Object, e As EventArgs) Handles BtnMaximize.Click
            WindowState = FormWindowState.Maximized
            Me.BtnNormal.Visible = True
            Me.BtnMaximize.Visible = False
        End Sub
    
        Private Sub BtnAdd_Click(sender As Object, e As EventArgs) Handles BtnAdd.Click
            Me.IDTB.BackColor = Color.Red
            Me.IDTB.ForeColor = Color.Red
            If String.IsNullOrEmpty(IDTB.Text.Trim()) Or
               String.IsNullOrEmpty(ClientNameTB.Text.Trim()) Or
               String.IsNullOrEmpty(ServerNameTB.Text.Trim()) Or
               String.IsNullOrEmpty(UsernameTB.Text.Trim()) Or
               String.IsNullOrEmpty(IPAddressTB.Text.Trim()) Or
               String.IsNullOrEmpty(PortTB.Text.Trim()) Then
                MessageBox.Show("Please all fields must be completed To add a remote connection.",
                                "ACRegister message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Exit Sub
            End If
            Try
                Me.AddModeCB.Checked = True
                IDTB.Text = ""
                ClientNameTB.Text = ""
                ServerNameTB.Text = ""
                UsernameTB.Text = ""
                IPAddressTB.Text = ""
                PortTB.Text = ""
                BtnAdd.Text = "Add record"
                BtnSave.Text = "Save record ()"
                BtnDelete.Text = "Delete record ()"
                Me.ClientNameTB.Select()
                Me.RecordsCountTB.Text = SQL.DBDT.Rows.Count.ToString
            Catch ex As Exception
            End Try
        End Sub
        Private Sub ShowFrmMsgSmallerSaved()
            Try
                Dim SmallerMsg As New FrmMsgSmaller
                FrmMsgSmaller.Close()
                FrmMsgSmaller.TextBox1.Text = ""
                FrmMsgSmaller.TextBox1.Text = "YOUR RECORDS WERE SAVED!"
                FrmMsgSmaller.Show()
                Dim SW2 As New Stopwatch
                SW2.Start()
                Do
                    Application.DoEvents()
                Loop Until SW2.ElapsedMilliseconds >= 1000
                FrmMsgSmaller.Hide()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
        Private Sub ShowFrmMsgSmallerUpdated()
            Try
                Dim SmallerMsg As New FrmMsgSmaller
                FrmMsgSmaller.Close()
                FrmMsgSmaller.TextBox1.Text = ""
                FrmMsgSmaller.TextBox1.Text = "YOUR RECORDS WERE UPDATED!"
                FrmMsgSmaller.Show()
                Dim SW2 As New Stopwatch
                SW2.Start()
                Do
                    Application.DoEvents()
                Loop Until SW2.ElapsedMilliseconds >= 600
                FrmMsgSmaller.Hide()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
        Private Sub ShowFrmMsgSmallerAdded()
            Try
                Dim SmallerMsg As New FrmMsgSmaller
                FrmMsgSmaller.Close()
                FrmMsgSmaller.TextBox1.Text = ""
                FrmMsgSmaller.TextBox1.Text = "YOUR New RECORD WAS ADDED To CATEGORIES!"
                FrmMsgSmaller.Show()
                Dim SW2 As New Stopwatch
                SW2.Start()
                Do
                    Application.DoEvents()
                Loop Until SW2.ElapsedMilliseconds >= 600
                FrmMsgSmaller.Hide()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
        Private Sub ShowFrmMsgSmallerDeleted()
            Try
                Dim SmallerMsg As New FrmMsgSmaller
                FrmMsgSmaller.Close()
                FrmMsgSmaller.TextBox1.Text = ""
                FrmMsgSmaller.TextBox1.Text = "THE SELECTED RECORD WAS DELETED!"
                FrmMsgSmaller.Show()
                Dim SW2 As New Stopwatch
                SW2.Start()
                Do
                    Application.DoEvents()
                Loop Until SW2.ElapsedMilliseconds >= 600
                FrmMsgSmaller.Hide()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
        Private Sub ResetMe()
            IDTB.Text = ""
            ClientNameTB.Text = ""
            ServerNameTB.Text = ""
            UsernameTB.Text = ""
            IPAddressTB.Text = ""
            PortTB.Text = ""
            BtnAdd.Text = "Add record"
            BtnSave.Text = "Save record ()"
            BtnDelete.Text = "Delete record ()"
            Me.CheckBox1.Checked = False
        End Sub
    
        Private Sub BtnDelete_Click(sender As Object, e As EventArgs) Handles BtnDelete.Click
            If DataGridView1.Rows.Count = 0 Then
                MsgBox(DataGridView1.Rows.Count.ToString() & " No rows found.",
                MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Auto Cash Register information")
                Exit Sub
            End If
            If BtnDelete.Text.Equals("Delete record ()") Then
                MessageBox.Show("Please Select an item from the list.",
                    "Auto Cash Register Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Exit Sub
            End If
            Try
                If MsgBox("The selected server will be delete! ARE YOU SURE YOU WANT To Do THAT, THIS CAN'T BE UNDONE!", MsgBoxStyle.YesNo, "DELETE THIS SERVER?") = vbYes Then
                    SQL.AddParam("@ID", IDTB.Text)
                    SQL.ExecQuery("DELETE FROM RemoteDesktop WHERE ID = @ID;")
                Else
                    MessageBox.Show("The deletion was aborted!.",
                    "Auto Cash Register Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                    LoadGrid()
                    Me.RecordsCountTB.Text = SQL.DBDT.Rows.Count.ToString
                    ResetMe()
                    BtnRefresh.PerformClick()
                    Exit Sub
                End If
                ShowFrmMsgSmallerDeleted()
                LoadGrid()
                Me.RecordsCountTB.Text = SQL.DBDT.Rows.Count.ToString
                ResetMe()
                BtnRefresh.PerformClick()
    
                If SQL.HasException(True) Then Exit Sub
            Catch ex As Exception
            End Try
        End Sub
        Private Sub UpdateServers()
            'ADD SQL PARAMS & RUN THE COMMAND
            Try
                SQL.AddParam("@ID", IDTB.Text)
                SQL.AddParam("@ClientName", ClientNameTB.Text)
                SQL.AddParam("@ServerName", ServerNameTB.Text)
                SQL.AddParam("@Username", UsernameTB.Text)
                SQL.AddParam("@IPAddress", IPAddressTB.Text)
                SQL.AddParam("@Port", PortTB.Text)
                SQL.ExecQuery("UPDATE RemoteDesktop " &
                              "SET ClientName=@ClientName,ServerName=@ServerName,Username=@Username,IPAddress=@IPAddress, " &
                              "Port=@Port WHERE ID=@ID;")
                If SQL.HasException(True) Then Exit Sub
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
        Private Sub BtnSave_Click(sender As Object, e As EventArgs) Handles BtnSave.Click
            If DataGridView1.Rows.Count = 0 Then
                MsgBox(DataGridView1.Rows.Count.ToString() & " No rows found.",
                MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Auto Cash Register information")
                Exit Sub
            End If
            If String.IsNullOrEmpty(ClientNameTB.Text.Trim()) Or
               String.IsNullOrEmpty(ServerNameTB.Text.Trim()) Or
               String.IsNullOrEmpty(UsernameTB.Text.Trim()) Or
               String.IsNullOrEmpty(IPAddressTB.Text.Trim()) Or
               String.IsNullOrEmpty(PortTB.Text.Trim()) Then
    
                MessageBox.Show("Please all fields must be completed to use the Save button.",
                    "Auto Cash Register Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Exit Sub
            End If
            Try
                If AddModeCB.Checked = False Then
                    UpdateServers()
                    ShowFrmMsgSmallerSaved()
                    LoadGrid()
                    Me.RecordsCountTB.Text = SQL.DBDT.Rows.Count.ToString
                    Me.CheckBox1.Checked = True
                    Me.BtnRefresh.PerformClick()
                    Me.CheckBox1.Checked = True
                End If
                If AddModeCB.Checked = True Then
                    SQL.AddParam("@ID", IDTB.Text)
                    SQL.AddParam("@ClientName", ClientNameTB.Text)
                    SQL.AddParam("@ServerName", ServerNameTB.Text)
                    SQL.AddParam("@Username", UsernameTB.Text)
                    SQL.AddParam("@IPAddress", IPAddressTB.Text)
                    SQL.AddParam("@Port", PortTB.Text)
                    SQL.ExecQuery("INSERT INTO RemoteDesktop(ClientName,ServerName,Username,IPAddress,Port) " &
                                 "VALUES (@ClientName,@ServerName,@Username,@IPAddress,@Port) ", True)
                    ShowFrmMsgSmallerSaved()
                    Me.IDTB.BackColor = Color.White
                    Me.IDTB.ForeColor = Color.Black
                    LoadGrid()
                    Me.RecordsCountTB.Text = SQL.DBDT.Rows.Count.ToString
                    Me.CheckBox1.Checked = True
                    Me.BtnRefresh.PerformClick()
                    Me.CheckBox1.Checked = True
                End If
            Catch ex As Exception
            End Try
        End Sub
    
        Private Sub BtnRefresh_Click(sender As Object, e As EventArgs) Handles BtnRefresh.Click
            Me.Controls.Clear()
            InitializeComponent()
            FrmRemoteDesktopClient_Load(e, e)
        End Sub
    
        Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
            Dim dgv As DataGridView = DataGridView1
            Try
                If e.RowIndex <> -1 Then
                    Me.IDTB.Text = Convert.ToString(dgv.CurrentRow.Cells(0).Value)
                    BtnSave.Text = "Save record (" & Me.IDTB.Text & ")"
                    BtnDelete.Text = "Delete record (" & Me.IDTB.Text & ")"
                    ClientNameTB.Text = Convert.ToString(dgv.CurrentRow.Cells(1).Value)
                    ServerNameTB.Text = Convert.ToString(dgv.CurrentRow.Cells(2).Value)
                    UsernameTB.Text = Convert.ToString(dgv.CurrentRow.Cells(3).Value)
                    IPAddressTB.Text = Convert.ToString(dgv.CurrentRow.Cells(4).Value)
                    PortTB.Text = Convert.ToString(dgv.CurrentRow.Cells(5).Value)
                    Me.RecordPosition.Text = BindingNavigatorPositionItem.ToString
                    Me.CheckBox1.Checked = False
                End If
                If SQL.HasException(True) Then Exit Sub
            Catch ex As Exception
            End Try
        End Sub
    
        Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
            Dim dgv As DataGridView = DataGridView1
            Dim MyRowIndex As String
            MyRowIndex = Me.IDTB.Text
            Try
                If MyRowIndex <> -1 Then
                    Me.IDTB.Text = Convert.ToString(dgv.CurrentRow.Cells(0).Value)
                    BtnSave.Text = "Save record (" & Me.IDTB.Text & ")"
                    BtnDelete.Text = "Delete record (" & Me.IDTB.Text & ")"
                    ClientNameTB.Text = Convert.ToString(dgv.CurrentRow.Cells(1).Value)
                    ServerNameTB.Text = Convert.ToString(dgv.CurrentRow.Cells(2).Value)
                    UsernameTB.Text = Convert.ToString(dgv.CurrentRow.Cells(3).Value)
                    IPAddressTB.Text = Convert.ToString(dgv.CurrentRow.Cells(4).Value)
                    PortTB.Text = Convert.ToString(dgv.CurrentRow.Cells(5).Value)
                    Me.RecordPosition.Text = BindingNavigatorPositionItem.ToString
                    Me.CheckBox1.Checked = False
                    Me.RecordPosition.Text = BindingNavigatorPositionItem.ToString
                    Me.CheckBox1.Checked = False
                End If
                If SQL.HasException(True) Then Exit Sub
            Catch ex As Exception
            End Try
        End Sub
    
        Private Sub ButtonFirst_Click(sender As Object, e As EventArgs) Handles ButtonFirst.Click
            BindingNavigatorMoveFirstItem.PerformClick()
            Me.RecordPosition.Text = BindingNavigatorPositionItem.ToString
            Me.DataGridView1.DataSource = Me.BindingNavigator.BindingSource
        End Sub
    
        Private Sub ButtonPrevious_Click(sender As Object, e As EventArgs) Handles ButtonPrevious.Click
            If Me.IDTB.Text = 1 Then
                MsgBox("You are at the first record of the dataset!", MsgBoxStyle.Information, "FIRST RECORD")
                Exit Sub
            End If
            BindingNavigatorMovePreviousItem.PerformClick()
            Me.RecordPosition.Text = BindingNavigatorPositionItem.ToString
            Me.DataGridView1.DataSource = Me.BindingNavigator.BindingSource
        End Sub
    
        Private Sub ButtonNext_Click(sender As Object, e As EventArgs) Handles ButtonNext.Click
            BindingNavigatorMoveNextItem.PerformClick()
            Me.RecordPosition.Text = BindingNavigatorPositionItem.ToString
            Me.DataGridView1.DataSource = Me.BindingNavigator.BindingSource
        End Sub
    
        Private Sub ButtonLast_Click(sender As Object, e As EventArgs) Handles ButtonLast.Click
            BindingNavigatorMoveLastItem.PerformClick()
            Me.RecordPosition.Text = BindingNavigatorPositionItem.ToString
            Me.DataGridView1.DataSource = Me.BindingNavigator.BindingSource
        End Sub
    
        Private Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click
            If DataGridView1.Rows.Count = 0 Then
                MsgBox(DataGridView1.Rows.Count.ToString() & " No rows found.",
                MsgBoxStyle.OkOnly Or MsgBoxStyle.Information, "Auto Cash Register information")
                Exit Sub
            End If
            If String.IsNullOrEmpty(TxtFilterTB.Text.Trim()) Then
                MessageBox.Show("You can't search from an empty filter box!.",
                    "Auto Cash Register Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Exit Sub
            End If
            Try
                FindItem()
                If SQL.HasException(True) Then Exit Sub
            Catch ex As Exception
                MsgBox("An error occured: " & ex.Message.ToString(), MsgBoxStyle.OkOnly Or MsgBoxStyle.Critical, "Search Error")
            End Try
        End Sub
    End Class

    Friday, September 11, 2020 1:35 PM