Hola a todos, estoy haciendo una aplicacion para enviar datos y recibirla para probarlo, estoy enviando los datos al localhost 127.0.0.1, pero el programa que recibe los datos no se hace nada, si podeis decir donde puede esta el error.
Programa que envia datos
Código:
  1. Imports System.IO
  2. Imports System.Net.Sockets.Socket
  3. Imports System
  4. Imports System.Text
  5. Imports System.Net
  6. Imports System.Net.Sockets
  7. Imports Microsoft.VisualBasic
  8. Imports System.Runtime.InteropServices
  9. Public Class Form1
  10.     Inherits System.Windows.Forms.Form
  11.     Dim GLOIP As IPAddress
  12.     Dim GLOINTPORT As Integer
  13.     Dim bytCommand As Byte() = New Byte() {}
  14.     Dim udpClient As New UdpClient
  15.     'CERRAMOS LA VENTANA Y EL CLIENTE UDP
  16.    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
  17.         udpClient.Close()
  18.     End Sub
  19.  
  20.     Private Sub cmdenviar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdenviar.Click
  21.         Dim pRet As Integer
  22.         Dim tempStr As String
  23.         Dim tempStr2 As String
  24.         Dim i As Integer = 0
  25.         Dim j As Integer = 0
  26.         Dim line As Integer = 0
  27.         Dim BitDet As BitArray
  28.  
  29.         Try
  30.             GLOIP = IPAddress.Parse(txtIP.Text)                                         'Pasa la direccion ip a una variable
  31.            GLOINTPORT = txtPort.Text                                                   'Pasa la direccion del puerto a una variable
  32.            udpClient.Connect(GLOIP, GLOINTPORT)                                        'Se crea una conexion a la IP y por el puerto dado
  33.            bytCommand = Encoding.ASCII.GetBytes(TxtMessage.Text)                       'Pasa de ASCII a byte
  34.            pRet = udpClient.Send(bytCommand, bytCommand.Length)                        'Envia el texto, longitud de envio
  35.            Console.WriteLine("N. DE BYTES ENVIO " & pRet)
  36.             txtInfo.Text = "INFORMACION" & vbCrLf & "N. BYTES ENVIO " & pRet            ' Pone informacion, salta de linea y el n de bit enviados
  37.            Console.WriteLine(Encoding.ASCII.GetString(bytCommand))
  38.             txtInfo.Text = txtInfo.Text + vbCrLf + "MENSAJE ENVIADO """                 'Avisamos del final del mensaje
  39.            txtInfo.Text = txtInfo.Text & Encoding.ASCII.GetChars(bytCommand) & """"
  40.  
  41.             'Codigo para escribir en binario las los datos mandados
  42.            BitDet = New BitArray(bytCommand)
  43.             txtInfo.Text = txtInfo.Text + vbCrLf
  44.  
  45.             txtInfo.Text = txtInfo.Text + line.ToString & ") "
  46.             For j = 0 To BitDet.Length - 1
  47.                 If BitDet(j) = True Then
  48.                     Console.Write("1 ")
  49.                     tempStr2 = tempStr
  50.                     tempStr = " 1" + tempStr2
  51.                 Else
  52.                     Console.Write("0 ")
  53.                     tempStr2 = tempStr
  54.                     tempStr = " 0" + tempStr2
  55.                 End If
  56.                 i += 1
  57.                 If i = 8 And j <= (BitDet.Length - 1) Then
  58.                     line += 1
  59.                     i = 0
  60.                     txtInfo.Text = txtInfo.Text + tempStr
  61.                     tempStr = ""
  62.                     tempStr2 = ""
  63.                     txtInfo.Text = txtInfo.Text + vbCrLf
  64.                     If j <> (BitDet.Length - 1) Then
  65.                         txtInfo.Text = txtInfo.Text + line.ToString & ") "
  66.                         Console.WriteLine()
  67.                     End If
  68.                 End If
  69.             Next
  70.  
  71.         Catch ex As Exception
  72.             Console.WriteLine(ex.Message)
  73.             txtInfo.Text = txtInfo.Text & vbCrLf & ex.Message
  74.         End Try
  75.     End Sub
  76. End Class

Prograa que recibe los datos
Código:
  1. Imports System.IO
  2. Imports System.Net.Sockets.Socket
  3. Imports System
  4. Imports System.Text
  5. Imports System.Net
  6. Imports System.Net.Sockets
  7. Imports Microsoft.VisualBasic
  8. Imports System.Runtime.InteropServices
  9. Public Class Form1
  10.     Inherits System.Windows.Forms.Form
  11.     Public receivingUdpClient As UdpClient
  12.     Public RemoteIpEndPoint As New System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)
  13.     Public ThreadReceive As System.Threading.Thread
  14.     Dim SocketNO As Integer
  15.     Dim MensajeError As Integer = 0
  16.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  17.  
  18.     End Sub
  19.     Public Sub ReceiveMessages()
  20.         Try
  21.             Dim receiveBytes As &#91;Byte&#93;() = receivingUdpClient.Receive(RemoteIpEndPoint)
  22.             Dim longitudmensaje As Integer = receiveBytes.Length
  23.             txtIP.Text = RemoteIpEndPoint.Address.ToString
  24.             Dim BitDet As BitArray
  25.             BitDet = New BitArray(receiveBytes)
  26.             Dim strReturnData As String = System.Text.Encoding.Unicode.GetString(receiveBytes)
  27.             Dim DatosRetornados As String = strReturnData
  28.             Dim DatosRecibidos As String = Encoding.ASCII.GetChars(receiveBytes)
  29.             NewInitialize()
  30.         Catch e As Exception
  31.             Console.WriteLine(e.Message)
  32.         End Try
  33.     End Sub
  34.     Public Sub NewInitialize()
  35.  
  36.         ThreadReceive = New System.Threading.Thread(AddressOf ReceiveMessages)
  37.         ThreadReceive.Start()
  38.     End Sub
  39.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  40.         Try
  41.             SocketNO = txtSocket.Text
  42.             receivingUdpClient = New System.Net.Sockets.UdpClient(SocketNO)
  43.             ThreadReceive = New System.Threading.Thread(AddressOf ReceiveMessages)
  44.             ThreadReceive.Start()
  45.  
  46.         Catch x As Exception
  47.             Console.WriteLine(x.Message)
  48.             TextBox1.Text = TextBox1.Text & vbCrLf & x.Message
  49.         End Try
  50.     End Sub
  51.  
  52.     Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
  53.         Try
  54.             receivingUdpClient.Close()
  55.         Catch ex As Exception
  56.             Console.WriteLine(ex.Message)
  57.         End Try
  58.     End Sub
  59.  
  60.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  61.         Try
  62.             ThreadReceive.Abort()
  63.             receivingUdpClient.Close()
  64.             TextBox1.Text = "INFORMACION"
  65.             TextBox1.Enabled = False
  66.             Button2.Enabled = False
  67.             Button1.Enabled = True
  68.             txtIP.Text = ""
  69.             txtSocket.ReadOnly = False
  70.         Catch ex As Exception
  71.             Console.WriteLine(ex.Message)
  72.         End Try
  73.     End Sub
  74. End Class