|
-
May 15th, 2008, 02:48 PM
#1
Thread Starter
New Member
[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 Long, ByVal 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 String, ByVal 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(HostName, Port)
DoRead()
End Sub
Private Sub DoRead()
Dim ByteArray(Client.Client.ReceiveBufferSize) As Byte
Do
Client.Client.Receive(byteArray, Client.Client.ReceiveBufferSize, Net.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(Buffer, Buffer.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
-
May 15th, 2008, 03:00 PM
#2
Re: [2005] Receive and send data in UDP
-
May 15th, 2008, 03:07 PM
#3
Re: [2005] Receive and send data in UDP
 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.
 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.
 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.
 Originally Posted by fredde_jason
Maby im not even going to use UDP?
That really depends on what your needs are.
 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.
-
May 15th, 2008, 03:18 PM
#4
Thread Starter
New Member
Re: [2005] Receive and send data in UDP
 Originally Posted by dbasnett
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?
-
May 15th, 2008, 03:49 PM
#5
Re: [2005] Receive and send data in UDP
did you check out Atheist code? I bet it uses an event for receive.
-
May 15th, 2008, 03:53 PM
#6
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.
-
May 16th, 2008, 08:24 AM
#7
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:
Public Class clientUDP
Private _udpSocket As System.Net.Sockets.Socket
Private _readThread As System.Threading.Thread
Public Event Received(ByVal text As String)
Public Sub New(ByVal port As Integer)
_udpSocket = New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Dgram, Net.Sockets.ProtocolType.Udp)
_udpSocket.Bind(New System.Net.IPEndPoint(System.Net.IPAddress.Any, port))
_readThread = New System.Threading.Thread(AddressOf ReadForIncoming)
_readThread.IsBackground = True
_readThread.Start()
End Sub
Private Sub ReadForIncoming()
Dim buffer(_udpSocket.ReceiveBufferSize) As Byte
Dim bytesRead As Integer
Do
bytesRead = _udpSocket.Receive(buffer)
RaiseEvent Received(System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead))
Loop
End Sub
Public Sub SendData(ByVal text As String, ByVal endpoint As System.Net.IPEndPoint)
Dim data() As Byte
data = System.Text.Encoding.UTF8.GetBytes(text)
_udpSocket.SendTo(data, System.Net.Sockets.SocketFlags.None, endpoint)
End Sub
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.
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
|