|
-
Jun 19th, 2006, 10:17 PM
#1
Thread Starter
Admodistrator
[2005] Send a UDP packet anywhere?
Seriously...This is something I think is not possible in versions of vb.net up to now, but is it possible to send a packet udp style? I dont want a link to this one, if someone has a WORKING example then I would like to see it, any link or project I find is not for UDP, and if it is it does not work.
-
Jun 20th, 2006, 09:10 AM
#2
Re: [2005] Send a UDP packet anywhere?
This works, but it's not cleaned up (validation, error handling, etc.. It has a few issues).
.Net 02/03
VB Code:
Private localIP As String = Net.Dns.GetHostByName(System.Environment.MachineName).AddressList(0).ToString
Private localPort As Int32
Private remIP As String
Private remPort As Int32
Private UdpClient As New System.Net.Sockets.UdpClient(Net.Sockets.AddressFamily.InterNetwork)
Private UdpListen As New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Dgram, Net.Sockets.ProtocolType.Udp)
Private thrdListen As New System.Threading.Thread(AddressOf WaitForPending)
Private Sub WaitForPending()
Try
UdpListen.Poll(1000, Net.Sockets.SelectMode.SelectRead)
While True
If UdpListen.Available Then
Dim byt(100) As Byte
UdpListen.Receive(byt, 100, Net.Sockets.SocketFlags.None)
For Each x As Byte In byt
Me.txtDisp.Text &= Chr(x)
Next x
Me.txtDisp.Text &= System.Environment.NewLine
Me.txtDisp.Text &= "-----End of Trans--------------------"
Me.txtDisp.Text &= System.Environment.NewLine
Array.Clear(byt, LBound(byt), UBound(byt))
End If
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
Try
If Not Me.txtSend.Text.Trim = String.Empty Then
Dim byt() As Byte
Dim x As Int32 = 0
For Each chr As Char In Me.txtSend.Text.Trim.ToCharArray
ReDim Preserve byt(x)
byt(x) = Asc(chr)
x += 1
Next chr
UdpClient.Connect(Net.IPAddress.Parse(remIP), remPort)
UdpClient.Send(byt, byt.Length)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub txtIP_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtIP.TextChanged
remIP = Me.txtIP.Text.Trim
End Sub
Private Sub txtPort_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPort.TextChanged
remPort = Me.txtPort.Text.Trim
End Sub
Private Sub txtlocPort_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtlocPort.TextChanged
localPort = Me.txtlocPort.Text.Trim
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
UdpListen.Bind(New Net.IPEndPoint(Net.IPAddress.Parse(localIP), localPort))
thrdListen.Start()
End Sub
textboxes:
txtIP is the IP of the computer you're sending to
txtPort is the remote port to send to
txtlocPort is the local port to receive on
txtDisp is the display for buffers received
txtSend is what you're sending
buttons:
Button2 starts polling every second for new data
btnSend sends the contents of txtSend
A few notes:
-the snippet will handle both incoming and outgoing messages.
-I've tested it by having it send the packet to myself (on seperate ports) and also on other computers on our network. We have to use proxy authentication, but I haven't added that functionality yet (shouldn't be too hard, I just wanted to get it working first).
LIke I said, this is pretty nasty and needs some cleaning. I just wanted to get a message sent and received using udp first.
Last edited by sevenhalo; Jun 20th, 2006 at 09:15 AM.
-
Jun 20th, 2006, 11:50 AM
#3
Thread Starter
Admodistrator
Re: [2005] Send a UDP packet anywhere?
I havent tried it yet, but I really appreciate the effort. If it works it is the ONLY example that I have ever recieved that works, even MSDN's examples do not work. It seems like UDP knowledge is very scarce in this .NET scene and I do not understand why :S
-
Jun 20th, 2006, 12:12 PM
#4
Re: [2005] Send a UDP packet anywhere?
The only real difference I encountered is that you can't connect with the socket, only poll. I'm pretty sure that this has to do with the one way nature of the UDP, but not 110% sure. I'll play with it more later (that code runs, but runs really nasty and slow).
-
Jun 20th, 2006, 12:13 PM
#5
Thread Starter
Admodistrator
Re: [2005] Send a UDP packet anywhere?
Do you see anything I missed? I tried to shorten your code a bit
VB Code:
Option Strict On
Option Explicit On
Public Class Form1
Private ThreadListen As New Threading.Thread(AddressOf WaitForPending)
Private UDPClient As New Net.Sockets.UdpClient(Net.Sockets.AddressFamily.InterNetwork)
Private UDPListen As New Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Dgram, Net.Sockets.ProtocolType.Udp)
Private localIP As String = Net.Dns.GetHostByName(System.Environment.MachineName).AddressList(0).ToString
Private Sub WaitForPending()
UDPListen.Poll(1000, Net.Sockets.SelectMode.SelectRead)
While True
If CBool(UDPListen.Available) Then
Dim Byt(100) As Byte
UDPListen.Receive(Byt, 100, Net.Sockets.SocketFlags.None)
For Each tByte As Byte In Byt
Debug.Print(Convert.ToChar(tByte))
Next
End If
End While
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
ThreadListen.Abort()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
UDPListen.Bind(New Net.IPEndPoint(Net.IPAddress.Parse(localIP), 2111))
ThreadListen.Start()
UDPClient.Connect(Net.IPAddress.Parse(anyiphere), anyport)
Dim ToBytes() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes("AAAAAA")
UDPClient.Send(ToBytes, ToBytes.Length)
End Sub
End Class
-
Jun 20th, 2006, 12:18 PM
#6
Re: [2005] Send a UDP packet anywhere?
Looks ok. 
I just had it all pulled apart so that I had a form that worked alot like an instant messenger. That and I was jumping between sending UDP packets to the same computer and then other computers.
-
Jun 20th, 2006, 12:20 PM
#7
Thread Starter
Admodistrator
Re: [2005] Send a UDP packet anywhere?
Well then it hates 2005, I dont get a response, but with winsock in vb6 I get a response almost instantly :|
Thanks for all the help!
-
Jun 20th, 2006, 12:21 PM
#8
Re: [2005] Send a UDP packet anywhere?
And you're running the same code on the other computer?
-
Jun 20th, 2006, 12:21 PM
#9
Thread Starter
Admodistrator
Re: [2005] Send a UDP packet anywhere?
No I am sending this to a game to request info (BF2 for example) and I am not getting a reply
-
Jun 20th, 2006, 12:28 PM
#10
Re: [2005] Send a UDP packet anywhere?
This deffinitely works...

Maybe it's not using UDP? Or... I don't know.
-
Jun 20th, 2006, 12:40 PM
#11
Thread Starter
Admodistrator
Re: [2005] Send a UDP packet anywhere?
Well hell..It works if I open up another vb.net and change the ports and stuff around, really cool. I wonder whats goin on when not sending to myself?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|