[SOLVED~] UdpSocket example or any working code?

I just can make any code that works with UdpSocket, I get all kind or runtime errors just trying to get the server to listen. I’m currently working with flash p2p to test how hard is to add multiplayer, but our final game is probably going to run in native, so we cant use that solution.

I try compiling to neko and windows, I always get a crashing error

Any working piece of code helps!

Thank you

I was able to create a test, the “fun” part now is that I need to use threads to make it work…

In the client you run something like this

var s:UdpSocket = new UdpSocket();
var h:Host = new Host(Host.localhost());
s.connect(h, 9002);
s.output.writeString(“Hello world”);
s.close();

And in the server you run something like this

host = new Host(Host.localhost());
connection = new UdpSocket();
connection.bind(host, 9002);

to read you need to call

connection.input.readString(11);

If you call readLine you get an Eof error.

Now the tricky part is that all the reading functions of the input are blocking functions so you cant just call it in the update without freezing the app.
You can set the connection.setBlocking(false); but you get an exception every time you try to read and theirs nothing in the input.

I think it’s a bad idea to have a try/catch in the update function, so probably the best idea is to have a second thread that handles the network stuff.

Nooooooo :’(
apparently UdpSocket has no implementation in native

I’m pretty C++ in Haxe includes a Socket implementation, otherwise there is a socket class in OpenFL that may work

Thank you Joshua, but Socket is implemented using the TCP protocol. Making an action(Towerfall like) p2p game using TCP is not a good idea because its too slow. TCP is awesome for reliable connections, but in p2p you want the packages to arrive as fast as they can. I want the data to get as quickly as possible from client to server without having to wait for lost data to be resent.

I found this https://github.com/andyli/hxudp, I made a test and apparently its working. The api is more limited, but I don’t care as long as I can send bytes and receive them. It works fine between windows/windows and windows/android so I guess I’m not going to have any problem, beside my network programming limitations =)

If I manage to make it work in a simple example(jumping boxes) I’m going to make it into a small tutorial, to inspire someone to finish the implementation of haxe’s UdpSocket XD