0 Replies - 231 Views - Last Post: 16 December 2013 - 09:46 AM Rate Topic: -----

#1 Zyvox  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-December 13

VB.NET UDP Sender/Receiver returns chinese symbols?

Posted 16 December 2013 - 09:46 AM

Hey! So I've been looking into sending/receiving data by using UDP in vb.net lately, and the application I made basically works and it returns different values compared to what I send, however it seems that it's translating it to chinese. (It's not any actual chinese that makes sense.)

Here's a picture:
http://gyazo.com/9c7...361033b0e91.png

I have absolutely no idea what causes this. My guess is that somehow in the translations from bytes to ASCII (or opposite) it messes up something.
So, can anyone see what I've done wrong?

Here's my entire project code:
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Dim data As String

Dim SendingUdpClient As New UdpClient
Dim sIP As IPAddress
Dim sPort As Integer
Dim bytCommand As Byte() = New Byte() {}

Public ReceivingUdpClient As New UdpClient
Public rIP As New System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)
Dim rPort As Integer
Public thrReceiver As System.Threading.Thread

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
Try
sIP = IPAddress.Parse(txt_s_IP.Text) 'Dim IP
sPort = txt_s_Port.Text 'Dim port
SendingUdpClient.Connect(sIP, sPort) 'Connect to UDP client
bytCommand = Encoding.ASCII.GetBytes(txtSend.Text) 'Get bytes from ASCII text
SendingUdpClient.Send(bytCommand, bytCommand.Length) 'Send bytes
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub

Private Sub btnReceive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReceive.Click
If btnReceive.Text = "Start" Then
btnReceive.Text = "Stop"
rPort = txt_r_Port.Text
Try
ReceivingUdpClient = New System.Net.Sockets.UdpClient(rPort)
thrReceiver = New System.Threading.Thread(AddressOf DoReceive)
thrReceiver.Start()
Catch ex As Exception
End Try
ElseIf btnReceive.Text = "Stop" Then
btnReceive.Text = "Start"
Try
ReceivingUdpClient.Close()
If thrReceiver.IsAlive = True Then thrReceiver.Abort()
Catch ex As Exception
End Try
End If
End Sub
Private Sub DoReceive()
Dim receiveBytes As [Byte]() = ReceivingUdpClient.Receive(rIP) 'Get bytes from the UDP client from receiving IP
Dim BitDet As BitArray 'Make a bitarray
BitDet = New BitArray(receiveBytes) 'Put bytes into bitarray
data = System.Text.Encoding.Unicode.GetString(receiveBytes) 'Convert bitarray to string
ReceiveAgain()
End Sub
Private Sub ReceiveAgain()
thrReceiver = New System.Threading.Thread(AddressOf DoReceive)
thrReceiver.Start()
End Sub

Private Sub tDisplay_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tDisplay.Tick
If Not rIP.Address.ToString() = "" Then txt_r_IP.Text = rIP.Address.ToString() 'Display IP from receiver in textbox
txtReceive.Text = data 'Put string in receiving textbox
End Sub
End Class


Thank you for your time.

Couldn't find the edit button... Anyhow, I just realized I missclicked the code-tag button. This might be easier to read.

Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
    Dim data As String

    Dim SendingUdpClient As New UdpClient
    Dim sIP As IPAddress
    Dim sPort As Integer
    Dim bytCommand As Byte() = New Byte() {}

    Public ReceivingUdpClient As New UdpClient
    Public rIP As New System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)
    Dim rPort As Integer
    Public thrReceiver As System.Threading.Thread

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Try
            sIP = IPAddress.Parse(txt_s_IP.Text) 'Dim IP
            sPort = txt_s_Port.Text 'Dim port
            SendingUdpClient.Connect(sIP, sPort) 'Connect to UDP client
            bytCommand = Encoding.ASCII.GetBytes(txtSend.Text) 'Get bytes from ASCII text
            SendingUdpClient.Send(bytCommand, bytCommand.Length) 'Send bytes
        Catch ex As Exception
            MsgBox(ex.ToString())
        End Try
    End Sub

    Private Sub btnReceive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReceive.Click
        If btnReceive.Text = "Start" Then
            btnReceive.Text = "Stop"
            rPort = txt_r_Port.Text
            Try
                ReceivingUdpClient = New System.Net.Sockets.UdpClient(rPort)
                thrReceiver = New System.Threading.Thread(AddressOf DoReceive)
                thrReceiver.Start()
            Catch ex As Exception
            End Try
        ElseIf btnReceive.Text = "Stop" Then
            btnReceive.Text = "Start"
            Try
                ReceivingUdpClient.Close()
                If thrReceiver.IsAlive = True Then thrReceiver.Abort()
            Catch ex As Exception
            End Try
        End If
    End Sub
    Private Sub DoReceive()
        Dim receiveBytes As [Byte]() = ReceivingUdpClient.Receive(rIP) 'Get bytes from the UDP client from receiving IP
        Dim BitDet As BitArray 'Make a bitarray
        BitDet = New BitArray(receiveBytes) 'Put bytes into bitarray
        data = System.Text.Encoding.Unicode.GetString(receiveBytes) 'Convert bitarray to string
        ReceiveAgain()
    End Sub
    Private Sub ReceiveAgain()
        thrReceiver = New System.Threading.Thread(AddressOf DoReceive)
        thrReceiver.Start()
    End Sub

    Private Sub tDisplay_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tDisplay.Tick
        If Not rIP.Address.ToString() = "" Then txt_r_IP.Text = rIP.Address.ToString() 'Display IP from receiver in textbox
        txtReceive.Text = data 'Put string in receiving textbox
    End Sub
End Class



Problem solved
I'm sorry to not have even tested this before posting this topic.
I was trying to get the bytes from ASCII instead of Unicode...

Can't find an edit or junk button.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1