Results 1 to 7 of 7

Thread: [2005] Receive and send data in UDP

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2008
    Posts
    13

    [2005] Receive and send data in UDP

    Hello, I have been working with some code to try send and receive data, but I can't figure it out how I should do.

    I used this code that I found in som thread here
    PHP Code:
    Public Class Client

        
    Private Client As Net.Sockets.UdpClient

        
    Public Event ClientReceived(ByVal Data As String)

        Public 
    Event ClientError(ByVal Number As LongByVal Description As String)

        Public 
    Event ServerConnected()

        Public 
    ReadOnly Property Socket() As Net.Sockets.UdpClient

            Get

                
    Return Client

            End Get

        End Property

        
    Public Sub Connect(ByVal HostName As StringByVal Port As Integer)

            If 
    Port 0 Then

                Err
    .Raise(44"The port to connect to was not set")

            
    End If

            If 
    HostName vbNullString Then

                Err
    .Raise(45"The hostname to connect to was not set")

            
    End If

            
    Client = New Net.Sockets.UdpClient(HostNamePort)

            
    DoRead()

        
    End Sub

        
    Private Sub DoRead()

            
    Dim ByteArray(Client.Client.ReceiveBufferSize) As Byte

            
    Do

                
    Client.Client.Receive(byteArrayClient.Client.ReceiveBufferSizeNet.Sockets.SocketFlags.None)

                
    RaiseEvent ClientReceived(System.Text.Encoding.Default.GetString(ByteArray))

            
    Loop

        End Sub

        
    Public Sub Send(ByVal Data As String)

            
    Dim Buffer() As Byte

            Buffer 
    System.Text.Encoding.ASCII.GetBytes(Data)

            
    Client.Send(BufferBuffer.Length)

        
    End Sub

    End 
    Class 
    Isn't "Private Sub DoRead" a receive function? And how shall I do so my server connect to an other client so I can send data to it? How shall the other client listen on a specific port and receive data from the server? Maby im not even going to use UDP? I don't know. If you have any server/client code please submit it, I only have som code in vb 6 but its much difference in socketing.

    //Fredde

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2005] Receive and send data in UDP

    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Receive and send data in UDP

    Quote Originally Posted by fredde_jason
    Isn't "Private Sub DoRead" a receive function?
    What do you mean by receive function? Its a subroutine, that reads incoming data and raises an event when data has been read.
    Quote Originally Posted by fredde_jason
    And how shall I do so my server connect to an other client so I can send data to it?
    Since you're using the UDP protocoll, there is no actual connections involved. You just send the data to a specific IP address and portnumber, and hope for it to arrive safely.
    Quote Originally Posted by fredde_jason
    How shall the other client listen on a specific port and receive data from the server?
    They would do pretty much the way you're doing right there, by reading from the socket.
    Quote Originally Posted by fredde_jason
    Maby im not even going to use UDP?
    That really depends on what your needs are.
    Quote Originally Posted by fredde_jason
    If you have any server/client code please submit it, I only have som code in vb 6 but its much difference in socketing.
    Check my signature.

    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2008
    Posts
    13

    Re: [2005] Receive and send data in UDP

    Thats in C# and I'm not good at translating programming languagues.

    Now I have a send function to send data to an IP to a specific port, but how can I receive the data in an other app and write it in a TextBox? If I can receive data then I could do like this. It's just an example!

    PHP Code:
    Dim ReceivedData as Received Data

    If ReceivedData "Hello" Then
            txtReceivedData
    .Text ReceivedData
    End 
    If 
    Lets say I have a code for revecing data, should I put it in a timer to check like every second if it's incomming data?

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2005] Receive and send data in UDP

    did you check out Atheist code? I bet it uses an event for receive.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Receive and send data in UDP

    Take a look at the code you just posted. You are reading for incoming data in it, just do the same on the receiving end.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  7. #7
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Receive and send data in UDP

    Here. I've put together a simple example of a class that lets you send and receive data using UDP.
    The UDPClient class is generally buggy, so its better to just use the Socket class.
    VB.NET Code:
    1. Public Class clientUDP
    2.  
    3.     Private _udpSocket As System.Net.Sockets.Socket
    4.     Private _readThread As System.Threading.Thread
    5.     Public Event Received(ByVal text As String)
    6.  
    7.     Public Sub New(ByVal port As Integer)
    8.         _udpSocket = New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Dgram, Net.Sockets.ProtocolType.Udp)
    9.         _udpSocket.Bind(New System.Net.IPEndPoint(System.Net.IPAddress.Any, port))
    10.         _readThread = New System.Threading.Thread(AddressOf ReadForIncoming)
    11.         _readThread.IsBackground = True
    12.         _readThread.Start()
    13.     End Sub
    14.  
    15.     Private Sub ReadForIncoming()
    16.         Dim buffer(_udpSocket.ReceiveBufferSize) As Byte
    17.         Dim bytesRead As Integer
    18.         Do
    19.             bytesRead = _udpSocket.Receive(buffer)
    20.             RaiseEvent Received(System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead))
    21.         Loop
    22.     End Sub
    23.  
    24.     Public Sub SendData(ByVal text As String, ByVal endpoint As System.Net.IPEndPoint)
    25.         Dim data() As Byte
    26.         data = System.Text.Encoding.UTF8.GetBytes(text)
    27.         _udpSocket.SendTo(data, System.Net.Sockets.SocketFlags.None, endpoint)
    28.     End Sub
    29. End Class

    As you can see, when you create an instance of this class, you pass the local port number on which you want to bind the socket. It will immediately start listening on a separate thread, and raise the Received event as soon as something has been received.
    Last edited by Atheist; May 16th, 2008 at 08:37 AM.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

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