This is a simple class I use in a few of my applications. I figured I\'d put it here in case anybody needed something like it. Let me know if there are any problems. It is written in VB.Net and uses system.net and system sockets for communication. Though UDP isn\'t as \"Reliable\" as TCP (So they say), it is for certain much faster and ideal for fast paced games in my opinion. Mainly because TCP checks to ensure all packets are received, and can spend a great deal of time in doing this. But in many games, a person needs to know what is happening then and there, without needing to double check what happened 10 minutes ago. However, depending on how it is used, it can be just as reliable, if a person is willing to make the effort to verify important packets of information.
Listen(port): Opens a port and starts listening for incoming messages in a separate thread.
IsListening: Returns true if the object is listening.
StopListening: Stops the listening thread.
Send(string,ip,port): Sends a data-gram in the form of a Byte to the specified location.
ListenStream: Where the data received will be stored on each pass.
Here is an example of a simple Console based server:
Imports System.Threading
Module Module1
Public net As New UDP_Plugin.Net
Sub Main()
net.Listen(2000)
Do
If net.ListenStream = \"IsListening\" Then Console.WriteLine(net.IsListening.ToString)
If net.ListenStream = \"StopListening\" Then
net.StopListening()
End If
Console.WriteLine(net.ListenStream())
If net.ListenStream = \"Repeat\" Then net.Send(net.ListenStream, \"127.0.0.1\", 1000)
Thread.Sleep(1)
Loop
End Sub
Note that UDP is connectionless, so I usually create a \"Handshake\" routine to get the IP/Ports and other information from the clients that i\'m communicating with. I\'ll generally store this information into an array upon first connecting so that it can quickly be recalled when needed.
I had actually wanted to make this a plugin for Darkbasic Professional, but I\'m not sure how to convert a dll created in vb.net for use in DBP. There was a good article in the DLL Talk forum, but all the links within in are pretty much dead. If somebody has some insight into this, i\'d love to hear about it.