VB.NET 2015 UDPClient: Reconnect a lost connection RRS feed

  • Question

  • Hi. Thanks for your attention.
    I am working on a proyect that need to connect whit anothers host of the lan by UPD. It is  working, but the problem is that if i forces the network to disconnect, i cannot retur to connect. I need close de application and open again.
    I was tried to abort the thread and start it again, i was tried to disconnect the UPDClient, but a dont got it works.
    I attach the code, a hope some can helpe.

     Sub IniciarConexiones()
                HiloMostrarUsuariosDisponibles = New Thread(New ThreadStart(AddressOf MostrarUsuariosLogueados))
        End Sub
    Public Sub MostrarUsuariosLogueados()
                If Recibiendo = False Then 
                    Recibiendo = True
                    Dim ep As IPEndPoint = Nothing
                    cont = cont + 1
                    lb_cont.Text = cont
                    Dim ClienteUDPUsuarios As UdpClient
                    ClienteUDPUsuarios = New UdpClient(PpChtUDPPuertoDestino)
                    Dim group As IPAddress = IPAddress.Parse(PpChtUDPIpDestino)
                    Dim timeToLive As Integer = CInt("255")
                    Dim port As Integer = CInt(PpChtUDPPuertoDestino)
                    ClienteUDPUsuarios.JoinMulticastGroup(group, timeToLive)
                    Dim remoteEP As IPEndPoint = New IPEndPoint(group, port)
                    Dim buffer() As Byte = ClienteUDPUsuarios.Receive(ep)
                    Dim MensajeString As String = System.Text.Encoding.UTF7.GetString(buffer)
                    IPRemite = ep.ToString.Substring(0, ep.ToString.IndexOf(":"))
                    Comando = Mid(MensajeString, 1, 3).Trim()
                    UsuLignum = Mid(MensajeString, 4, 20).Trim()
                    Nombre = Mid(MensajeString, 24, 40).Trim()
                    PC = Mid(MensajeString, 64, 25).Trim()
                    UsuWin = Mid(MensajeString, 89, 15).Trim()
                    ' If IPRemite <> ppMiIP Then 'SI LA IP REMITENTE ES DIFERENTE QUE LA LOCAL
                    If Comando = "CON" Then
                        Dim Existe As Integer = 0
                        For Each row As DataRow In tb_usuarios.Rows
                            If row.Item("ip") = IPRemite Then
                                Existe = 1
                                row.Item("Ultima") = Now
                                Exit For
                            End If
                        If Existe = 0 Then 
                            Dim funcion As New CFunciones
                            Dim tb_InfoUsuario As New DataTable
                            tb_InfoUsuario = BuscarInfoUsuario(UsuLignum).Copy
                            Dim LogoUsuario As Image = Nothing
                            Dim mailUsuario As String = ""
                            Dim InternoUsuario As String = ""
                            Dim LegajoUsuario As String = ""
                            If tb_InfoUsuario.Rows.Count = 1 Then
                                LogoUsuario = funcion.BytesAImagen(tb_InfoUsuario.Rows(0).Item("foto"))
                                mailUsuario = tb_InfoUsuario.Rows(0).Item("mail").ToString
                                InternoUsuario = tb_InfoUsuario.Rows(0).Item("int").ToString
                                LegajoUsuario = tb_InfoUsuario.Rows(0).Item("leg").ToString
                            End If
                            Dim scale_factor As Single = Single.Parse(0.5)
                            Dim bm_source As New Bitmap(LogoUsuario)
                            Dim bm_dest As New Bitmap(CInt(32), CInt(32))
                            Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
                            gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width + 1, bm_dest.Height + 1)
                            LogoUsuario = bm_dest
                             Dim funcion As New CFunciones
                            Dim MapaBits As Bitmap = CType(LogoUsuario, Bitmap)
                            Dim imgRedonda As Bitmap = New Bitmap(MapaBits.Width, MapaBits.Height)
                            Using gp As New GraphicsPath
                                gp.AddEllipse(0, 0, MapaBits.Width, MapaBits.Height)
                                Using gr As Graphics = Graphics.FromImage(imgRedonda)
                                    gr.DrawImage(MapaBits, Point.Empty)
                                End Using
                            End Using
                            LogoUsuario = imgRedonda
                                                 Dim FormatoUsuario As String = ""
                            FormatoUsuario = "{\rtf1\fbidis\ansi\ansicpg1252\deff0\deflang3082{\fonttbl{\f0\fswiss\fprq2\fcharset0 Segoe UI Semibold;}{\f1\fswiss\fprq2\fcharset0 Calibri;}} {\colortbl ;\red5\green99\blue193;} \viewkind4\uc1\pard\ltrpar\lang11274\f0\fs22 " & Nombre & " \f1 [" & InternoUsuario & "]\par      \cf1\ul\i\fs20" & mailUsuario & "\cf0\ulnone\i0\f0\fs22\par }"
                            tb_usuarios.Rows.Add(LogoUsuario, IPRemite, UsuLignum, Nombre, PC, UsuWin, Now, FormatoUsuario)
                            AvisarDesconexion(tb_usuarios.Rows(tb_usuarios.Rows.Count - 1).Item("ip"), 1)
                            CambiosGrillaDetectados = True
                        End If
                    End If
                    If Comando = "DES" Then
                        For Each usuario As DataRow In tb_usuarios.Rows
                            If IPRemite = usuario.Item("ip") Then
                                AvisarDesconexion(usuario.Item("ip"), 0)
                                CambiosGrillaDetectados = True
                                Exit For
                            End If
                    End If
                    If CambiosGrillaDetectados = True Then
                        Dim dv_OrdenaUsuarios As New DataView
                        dv_OrdenaUsuarios = tb_usuarios.DefaultView
                        dv_OrdenaUsuarios.Sort = " Nombre ASC"
                        tb_usuarios = dv_OrdenaUsuarios.ToTable()
                    End If
                    End If
                    Recibiendo = False
                    Recibiendo = False
                End If
        End Sub

    • Edited by CM16 Wednesday, March 13, 2019 1:38 PM olvido
    Wednesday, March 13, 2019 1:32 PM

All replies

  • Hi,

    I think you can send a signal to the server in 30 seconds. If there is no response, it will be disconnected, and you will reconnect.

    Dim event_ok As ManualResetEvent = New ManualResetEvent(False)

    Best Regards,


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Thursday, March 14, 2019 7:11 AM
  • Hi Alex. Thanks for your response.
    The problem is that when i disconnect the lan cable, the thread still on running status. The last line on execute is:
    Dim buffer() As Byte = ClienteUDPUsuarios.Receive(ep)
    And then is kicked of the Do loop.
    I was tryied catch the error with a try but i cant doit (is not catched)

    I would gratifully your help.
    Thursday, March 14, 2019 5:05 PM