Receive UDP packets with RMXP - Printable Version +- Save-Point (https://www.save-point.org) +-- Forum: Games Development (https://www.save-point.org/forum-4.html) +--- Forum: Code Support (https://www.save-point.org/forum-20.html) +--- Thread: Receive UDP packets with RMXP (/thread-724.html) |
Receive UDP packets with RMXP - Charlie Fleed - 02-18-2010 I'm trying to make two (or more) RPG Maker XPs communicate over the network through UDP. The idea is to implement a basic hello/discovery protocol. I'm using the "module Win32/module Winsock/class Socket/class TCPSocket" library that you find in any netplay-related project. Plus, I added a UDPSocket class: Code: #=============================================================================== As you can see it's almost identical to the TCPSocket class, but it uses SOCK_DGRAM and IPPROTO_UDP, and doesn't connect the socket (it's initialized without arguments). I have managed to send unicast and broadcast to a listener written in ruby that runs outside RPG Maker XP (I have installed Ruby on my Windows machine), but I couldn't make RPG Maker XP receive UDP packets of any kind. This code sends UDP broadcast packets from RMXP and it works perfectly: Code: BROADCAST_ADDR = "255.255.255.255" In order to receive broadcast packets, a stand-alone ruby app would do something like this: Code: require 'socket' (ignore all the unnecessary code) which, inside RPG Maker XP, based on the API documentation, would be something like Code: # Broadcast where recvfrom is defined as: Code: class Socket with also: Code: module Winsock (ignore all the unnecessary code) Well, it doesn't work, that is @insocket.ready? returns true, but @insocket.recvfrom returns a string of "\000" values. The exact behavior is that: a) when an external app is actually broadcasting data that string is returned at the first received packet and then it is returned immediately for the following calls; b) when no message is being broadcasted, @insocket.ready? returns true anyway, @insocket.recvfrom does not return at all and of course I get the "the script is hanging" message. The problem seems to be in the decoding phase, because the difference between a) and b) tells me that RMXP is receiving those packets. Also, @insocket.ready?, which is based on the dll function select, does not work as expected. So... HEEEEELPPPP!!!! Update: it appears that this behavior of select with udp socket is a problem for many. [Resolved] Receive UDP packets with RMXP - Charlie Fleed - 02-23-2010 Time to bump this. I managed to receive packets by using recv and not recvfrom, but I can't read the sender's address with recv, and I need that. select is actually working correctly. recvfrom is to blame. Update: i found out that recvfrom was returning -1 but the SocketError class was failing to raise an exception, I guess beacuse it didn't recognize it. So, by checking the error number myself, i found out it was a 10014 error, which means some of the buffer lenghts I was passing weren't correct. I turns out the correct call to recvfrom must be: Code: #-------------------------------------------------------------------------- Now I can receive the packet. I'm still trying to extract the sender's address. Update2: (am I talking to myself?) Wrong, not Code: buf2 = "\0" * 2 Code: buf2 = "\0" * 20 however, it still gives error 10014 sometimes. Update3: And I managed to get the sender's address too. Be sure to play my online game when it will come out. :) This solo thread can be closed. |