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.