Results 1 to 11 of 11

Thread: [2005] Send a UDP packet anywhere?

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    [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.

  2. #2
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    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:
    1. Private localIP As String = Net.Dns.GetHostByName(System.Environment.MachineName).AddressList(0).ToString
    2.     Private localPort As Int32
    3.     Private remIP As String
    4.     Private remPort As Int32
    5.  
    6.     Private UdpClient As New System.Net.Sockets.UdpClient(Net.Sockets.AddressFamily.InterNetwork)
    7.     Private UdpListen As New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Dgram, Net.Sockets.ProtocolType.Udp)
    8.  
    9.     Private thrdListen As New System.Threading.Thread(AddressOf WaitForPending)
    10.  
    11.     Private Sub WaitForPending()
    12.         Try
    13.             UdpListen.Poll(1000, Net.Sockets.SelectMode.SelectRead)
    14.             While True
    15.                 If UdpListen.Available Then
    16.                     Dim byt(100) As Byte
    17.                     UdpListen.Receive(byt, 100, Net.Sockets.SocketFlags.None)
    18.  
    19.                     For Each x As Byte In byt
    20.                         Me.txtDisp.Text &= Chr(x)
    21.                     Next x
    22.                     Me.txtDisp.Text &= System.Environment.NewLine
    23.                     Me.txtDisp.Text &= "-----End of Trans--------------------"
    24.                     Me.txtDisp.Text &= System.Environment.NewLine
    25.                     Array.Clear(byt, LBound(byt), UBound(byt))
    26.                 End If
    27.             End While
    28.         Catch ex As Exception
    29.             MsgBox(ex.Message)
    30.         End Try
    31.     End Sub
    32.  
    33.     Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
    34.         Try
    35.             If Not Me.txtSend.Text.Trim = String.Empty Then
    36.                 Dim byt() As Byte
    37.                 Dim x As Int32 = 0
    38.  
    39.                 For Each chr As Char In Me.txtSend.Text.Trim.ToCharArray
    40.                     ReDim Preserve byt(x)
    41.                     byt(x) = Asc(chr)
    42.                     x += 1
    43.                 Next chr
    44.  
    45.                 UdpClient.Connect(Net.IPAddress.Parse(remIP), remPort)
    46.                 UdpClient.Send(byt, byt.Length)
    47.             End If
    48.         Catch ex As Exception
    49.             MsgBox(ex.Message)
    50.         End Try
    51.     End Sub
    52.  
    53.     Private Sub txtIP_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtIP.TextChanged
    54.         remIP = Me.txtIP.Text.Trim
    55.     End Sub
    56.     Private Sub txtPort_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPort.TextChanged
    57.         remPort = Me.txtPort.Text.Trim
    58.     End Sub
    59.     Private Sub txtlocPort_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtlocPort.TextChanged
    60.         localPort = Me.txtlocPort.Text.Trim
    61.     End Sub
    62.  
    63.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    64.         UdpListen.Bind(New Net.IPEndPoint(Net.IPAddress.Parse(localIP), localPort))
    65.         thrdListen.Start()
    66.     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.

  3. #3

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    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

  4. #4
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    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).

  5. #5

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: [2005] Send a UDP packet anywhere?

    Do you see anything I missed? I tried to shorten your code a bit

    VB Code:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Public Class Form1
    5.     Private ThreadListen As New Threading.Thread(AddressOf WaitForPending)
    6.     Private UDPClient As New Net.Sockets.UdpClient(Net.Sockets.AddressFamily.InterNetwork)
    7.     Private UDPListen As New Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Dgram, Net.Sockets.ProtocolType.Udp)
    8.     Private localIP As String = Net.Dns.GetHostByName(System.Environment.MachineName).AddressList(0).ToString
    9.  
    10.     Private Sub WaitForPending()
    11.         UDPListen.Poll(1000, Net.Sockets.SelectMode.SelectRead)
    12.         While True
    13.             If CBool(UDPListen.Available) Then
    14.                 Dim Byt(100) As Byte
    15.                 UDPListen.Receive(Byt, 100, Net.Sockets.SocketFlags.None)
    16.                 For Each tByte As Byte In Byt
    17.                     Debug.Print(Convert.ToChar(tByte))
    18.                 Next
    19.             End If
    20.         End While
    21.     End Sub
    22.  
    23.     Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    24.         ThreadListen.Abort()
    25.     End Sub
    26.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    27.         UDPListen.Bind(New Net.IPEndPoint(Net.IPAddress.Parse(localIP), 2111))
    28.         ThreadListen.Start()
    29.         UDPClient.Connect(Net.IPAddress.Parse(anyiphere), anyport)
    30.         Dim ToBytes() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes("AAAAAA")
    31.         UDPClient.Send(ToBytes, ToBytes.Length)
    32.     End Sub
    33. End Class

  6. #6
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    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.

  7. #7

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    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!

  8. #8
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [2005] Send a UDP packet anywhere?

    And you're running the same code on the other computer?

  9. #9

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    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

  10. #10
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: [2005] Send a UDP packet anywhere?

    This deffinitely works...

    Maybe it's not using UDP? Or... I don't know.

  11. #11

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    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
  •  



Click Here to Expand Forum to Full Width