I was experimenting with code from
http://www.codeproject.com/KB/IP/UDP_Send_Receive.aspx
I was hoping to find an application that did both in one application
I am ale to listen to a port and display the byte stream received in
a listbox, the text box does no good because the data received would
not make sense.
A good Example of what I get in the list box is
34
0
0
0
35
0
0
0
152
135
157
0
I wish to reply back to the IP/Port that sent me this from the same port I received it on plus 10 more byte
here is what i have so far
The data in listbox lbSend is the information i wish to send back
Code:Imports System.IO Imports System.Net.Sockets.Socket Imports System Imports System.Text Imports System.Net Imports System.Net.Sockets Imports Microsoft.VisualBasic Imports System.Runtime.InteropServices Public Class Form1 Inherits System.Windows.Forms.Form Public receivingUdpClient As UdpClient Public RemoteIpEndPoint As New System.Net.IPEndPoint(System.Net.IPAddress.Any, 0) Public ThreadReceive As System.Threading.Thread Dim SocketNO As Integer Dim GLOIP As IPAddress Dim GLOINTPORT As Integer 'Dim bytCommand As Byte() = New Byte() {} Dim udpClient As New UdpClient Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load chkBit.Checked = True lbReceived.Items.Clear() End Sub Public Sub ReceiveMessages() Try Dim receiveBytes As [Byte]() = receivingUdpClient.Receive(RemoteIpEndPoint) txtIP.Text = RemoteIpEndPoint.Address.ToString txtPort.Text = RemoteIpEndPoint.Port.ToString Dim BitDet As BitArray BitDet = New BitArray(receiveBytes) Dim strReturnData As String = System.Text.Encoding.Unicode.GetString(receiveBytes) Console.WriteLine("A message is received from IP/port " & txtIP.Text & "/ " & txtPort.Text & " and being processed") TextBox1.Text = TextBox1.Text & vbCrLf & "A message is received from " & txtIP.Text & "/ " & txtPort.Text & " and being processed" Console.WriteLine("The length of the message is ") TextBox1.Text = TextBox1.Text & vbCrLf & "The length of the message is " Console.WriteLine(receiveBytes.Length) TextBox1.Text = TextBox1.Text & receiveBytes.Length Console.WriteLine(strReturnData) TextBox1.Text = TextBox1.Text + vbCrLf + "The message received is """ TextBox1.Text = TextBox1.Text & Encoding.Unicode.GetChars(receiveBytes) + """" TextBox1.Text = TextBox1.Text & vbCrLf TextBox1.Text = TextBox1.Text + "The Received bytes are " For cnt As Integer = 0 To receiveBytes.Length - 1 TextBox1.Text = TextBox1.Text & receiveBytes(cnt) & " " lbReceived.Items.Add(receiveBytes(cnt)) Next txtReceived.Text = receiveBytes.Length TextBox1.Text = TextBox1.Text & vbCrLf Dim tempStr As String Dim tempStr2 As String If chkBit.Checked = True Then Dim i As Integer i = 0 Dim j As Integer j = 0 Dim line As Integer line = 0 TextBox1.Text = TextBox1.Text + line.ToString & ") " For j = 0 To BitDet.Length - 1 If BitDet(j) = True Then Console.Write("1 ") tempStr2 = tempStr tempStr = " 1" + tempStr2 Else Console.Write("0 ") tempStr2 = tempStr tempStr = " 0" + tempStr2 End If i += 1 If i = 8 And j <= (BitDet.Length - 1) Then line += 1 i = 0 TextBox1.Text = TextBox1.Text + tempStr tempStr = "" tempStr2 = "" TextBox1.Text = TextBox1.Text + vbCrLf If j <> (BitDet.Length - 1) Then TextBox1.Text = TextBox1.Text + line.ToString & ") " Console.WriteLine() End If End If Next End If TextBox1.Text = TextBox1.Text & vbCrLf NewInitialize() For cnt As Integer = 4 To receiveBytes.Length - 1 lbSend.Items.Add(receiveBytes(cnt)) Next lbSend.Items.Add(68) lbSend.Items.Add(205) lbSend.Items.Add(160) lbSend.Items.Add(92) lbSend.Items.Add(32) lbSend.Items.Add(61) lbSend.Items.Add(0) lbSend.Items.Add(42) lbSend.Items.Add(255) lbSend.Items.Add(255) lbSend.Items.Add(255) lbSend.Items.Add(255) Catch e As Exception Console.WriteLine(e.Message) End Try Send_Back() End Sub Public Sub NewInitialize() Console.WriteLine("Thread *Thread Receive* reinitialized") ThreadReceive = New System.Threading.Thread(AddressOf ReceiveMessages) ThreadReceive.Start() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click lbReceived.Items.Clear() txtReceived.Text = "" Try SocketNO = txtSocket.Text receivingUdpClient = New System.Net.Sockets.UdpClient(SocketNO) ThreadReceive = New System.Threading.Thread(AddressOf ReceiveMessages) ThreadReceive.Start() TextBox1.Enabled = True Button2.Enabled = True Button1.Enabled = False txtSocket.ReadOnly = True Catch x As Exception Console.WriteLine(x.Message) TextBox1.Text = TextBox1.Text & vbCrLf & x.Message End Try End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 'lbReceived.Items.Clear() Try ThreadReceive.Abort() receivingUdpClient.Close() 'TextBox1.Text = "INFORMATION" TextBox1.Enabled = False Button2.Enabled = False Button1.Enabled = True txtIP.Text = "" txtSocket.ReadOnly = False Catch ex As Exception Console.WriteLine(ex.Message) End Try End Sub Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing Try receivingUdpClient.Close() Catch ex As Exception Console.WriteLine(ex.Message) End Try End Sub Private Sub Send_Back() Dim pRet As Integer Try GLOIP = IPAddress.Parse(txtIP.Text) GLOINTPORT = txtPort.Text udpClient.Connect(GLOIP, GLOINTPORT) 'bytCommand = Encoding.ASCII.GetBytes(txtMessage.Text) 'pRet = udpClient.Send(bytCommand, bytCommand.Length) Console.WriteLine("No of bytes send " & pRet) 'txtInfo.Text = "INFORMATION" & vbCrLf & "No of bytes send " & pRet 'Console.WriteLine(Encoding.ASCII.GetString(bytCommand)) 'txtInfo.Text = txtInfo.Text + vbCrLf + "The messege send is """ 'txtInfo.Text = txtInfo.Text & Encoding.ASCII.GetChars(bytCommand) & """" Dim BitDet As BitArray 'BitDet = New BitArray(bytCommand) 'txtInfo.Text = txtInfo.Text + vbCrLf Dim tempStr As String Dim tempStr2 As String Dim i As Integer i = 0 Dim j As Integer j = 0 Dim line As Integer line = 0 'txtInfo.Text = txtInfo.Text + line.ToString & ") " For j = 0 To BitDet.Length - 1 If BitDet(j) = True Then Console.Write("1 ") tempStr2 = tempStr tempStr = " 1" + tempStr2 Else Console.Write("0 ") tempStr2 = tempStr tempStr = " 0" + tempStr2 End If i += 1 If i = 8 And j <= (BitDet.Length - 1) Then line += 1 i = 0 'txtInfo.Text = txtInfo.Text + tempStr tempStr = "" tempStr2 = "" 'txtInfo.Text = txtInfo.Text + vbCrLf If j <> (BitDet.Length - 1) Then 'txtInfo.Text = txtInfo.Text + line.ToString & ") " Console.WriteLine() End If End If Next Catch ex As Exception Console.WriteLine(ex.Message) 'txtInfo.Text = txtInfo.Text & vbCrLf & ex.Message End Try End Sub End Class




Reply With Quote