Networking

Monkey Forums/Monkey Programming/Networking

cgrossi(Posted 2016) [#1]
Hello. Does anyone have any sample code to do networking? A simple one. :-)

Thanks


ImmutableOctet(SKNG)(Posted 2016) [#2]
If you want to go low-level, and get into the gritty details yourself, there's some great examples in your Monkey installation already.

Check out the "bananas/mak/echoserver_udp" and "bananas/mak/echoserver_tcp" folders. You can find these in your Monkey installation's folder as well as the links I posted. Those show you how to create a client and server pair, and how to send messages between them (Echo it back).

For something more simplistic, but higher-level (Also requires the use of git), checkout my 'regal.networking' module. To see just how simple that one gets, checkout the examples, or view the docs, here.

If you're looking for a good introductory thread, click here. I go over my networking module in that thread as well, and in better detail. For a bit of networking history in Monkey, take a look at this thread.

If you have any questions, feel free to ask.


cgrossi(Posted 2016) [#3]
hi..
ok, indeed I've already saw your post and saw the samples, but, as this topic is new for me in monkey-x, I couldn't understand well. so, what I'm looking for is something very basic. what I need, in the first part, is to make a listener that will be checking a port to receive messages and checking another port to send messages. having this done, I can move on with the rest of the code. could I make myself clear?


cgrossi(Posted 2016) [#4]
hello... anybody??


ImmutableOctet(SKNG)(Posted 2016) [#5]
@cgrossi: The echo server examples literally have the code you're looking for. In addition to this, you're not entitled to anything from me. On top of celebrating my birthday with family, I was at the gym, then I had to do this crazy thing called sleeping afterward (Probably a significantly different time zone), along with having other obligations. I don't always have the time to write code for other people. I'm sorry I wasn't able to post another example.

If you want something with raw sockets, where you need to use 'DataStreams' and 'DataBuffers' as fixed-size containers, then you just need to use the aforementioned 'Stream' class. You could also use a pool-based system, and write to buffers and streams, then send them out. This is preferred, since you're not technically allowed to use a 'DataBuffer' when it's in transit (Being sent asynchronously). You'll also need to keep in mind the networking guidelines of the targets you're working on, because some require the use of callbacks (Asynchronous commands).

The point I'm getting at is that this isn't going to be simple the bigger the software stack gets. If you're okay with building your own, then please use 'brl.socket'. The echo-server examples I posted about initially have two classes each, they handle the server and client portions, and are great points to start.

Now, if you want to do something simpler, where the codebase already exists, but you have to use an existing protocol/format underneath, then try 'regal.networking' or 'PushButton'. If you're interested in these, you'll probably want git. I can't say much for 'PushButton', but the other higher-level choice is my own module, and I know it works. I've already spent plenty of my time documenting that, and making examples for it. The docs are available online, and locally, assuming you rebuild your docs (Under "Help" in Ted).

There's not much else I can say, unless you want me to make a tutorial. I've already gone over the basic concepts in my other post, so it's not out of the question. I just don't always have time. I'm sorry I wasn't able to make another example for your specific case, but I'm just not able to right now.

If someone else could help further, I would appreciate it.


PixelPaladin(Posted 2016) [#6]
@ImmutableOctet: Happy Birthday!

Nice modules. Why aren't they in the module registry? I have searched for something like this for quite a while …


ImmutableOctet(SKNG)(Posted 2016) [#7]
@PixelPaladin: Oh yeah, that does exist, doesn't it? I'd only have the main "module" (Basically a namespace) listed if I were to ask, anyway. Glad you like them, and thank you.


cgrossi(Posted 2016) [#8]
@ImmutableOctet: Thank you a lot for your time. I'll take a look more closely to the samples. But the most difficult thing I saw so far is to receive a message. Some how I'm not been able to receive a message..


ImmutableOctet(SKNG)(Posted 2016) [#9]
Alright, I'm working on a demo, give me a bit a while to sort it out.


Sicilica(Posted 2016) [#10]
If socket programming seems pretty new and foreign, I would recommend just making your own simple echo server or other example in C first. The brl module just wraps the POSIX socket functions exposed by your OS, and imho it's easier to understand if you do it in native C first to get the concepts down because the Monkey implementation is not nearly as intuitive (although it's obvious what it's doing if you understand the original POSIX spec).


ImmutableOctet(SKNG)(Posted 2016) [#11]
Okay, so, longer than expected, but I created a new module, and demo for this. It's called 'transport', and it's meant to abstract the specifics of storage and connection. From there, it's completely unmanaged. Currently, the API isn't final, and only supports TCP. You can download the latest version, and try it out for yourself by clicking here.

I intend to work on it more over time, but this should get people who are interested started with networking in Monkey. Just keep in mind that I already have 'regal.networking', so there's no need to take a higher level approach with this module. That's up to the user.

The source provided is partially commented, but isn't fully explained. That's definitely on the list of things to do, though. What's available now works, but it's a proof of concept for the final product, which may or may not be merged into 'regal.networking' in some way. Everything was written using the 'brl' modules (And possibly other official modules) only. There's no dependency on 'regal' at this time.

This is a much smaller and much more understandable codebase for someone getting into this, so I hope it helps. If you have any further questions, feel free to ask.


cgrossi(Posted 2016) [#12]
Hi, ImmutableOctet.

Thank you very much for your time and for your sample. I'll take a look, although I'm looking for something way too more simple, just to understand how to send and receive messages.
But, anyway, I'm very appreciate for your dedication and time spent on this subject.


ImmutableOctet(SKNG)(Posted 2016) [#13]
@cgrossi:

I actually forgot to commit the latest version of "transport.monkey", so you might want to grab that. At any rate, I'm glad you like the sample.

The pattern is more or less this simple, though:

* New Server(Port, Application) -> Check 'IsOpen'.
* New Client(IP, Port, Application) -> Check 'IsOpen'.

To send a message:


Then, when you get the message:


And of course, call 'UpdateAsyncEvents' in 'OnUpdate'. You may also want to change the suspend-mode of Mojo in your configuration-file, but that may not be necessary.

There's a few other details, but that's basically it. You can use mostly the same code on each end, just use 'Client' or 'Server'. Messages can be written to like any other stream. 'Packets' are given back automatically if you use 'SendAsync', and if not, you can use 'Free' on it when you're done. Using 'brl.socket' manually would be more hassle. Just implement a few callbacks, and it works. The source is available along with it, and should be straight forward enough. To set up the demo, just move the file into the main directory, then rename the import at the top from 'regal.transport' to just be 'transport'. It'll run with no issues.

If you're having trouble understanding callbacks, they're just methods that the 'Server' and 'Client' types call. Again, this is about as low level as it gets without following bad practices, or dealing with 'brl.socket' directly. For something nicer, you'll need to use 'regal.networking' and co.

Well, whatever option you choose, I hope this helps as an example.


Kaltern(Posted 2016) [#14]
Hijacking old thread :P

I'm using the regal networking module, and I'm getting an error I can't figure out in the 'engine' source - the line:
' Constructor(s) (Public):
	Method New(PacketSize:Int=Default_PacketSize, PacketPoolSize:Int=Default_PacketPoolSize, FixByteOrder:Bool=Default_FixByteOrder, PingFrequency:Duration=Default_PingFrequency, MaxPing:NetworkPing=Default_MaxPing, MaxChunksPerMegaPacket:Int=Default_MaxChunksPerMegaPacket, PacketReleaseTime:Duration=Default_PacketReleaseTime, PacketResendTime:Duration=Default_PacketResendTime) ' LaunchReceivePerClient:Bool=Default_LaunchReceivePerClient

is throwing up an error:
Error : Type 'Duration' not found

Is this an error due to the Monkey version being too new for these modules, or am I missing something obvious?


ImmutableOctet(SKNG)(Posted 2016) [#15]
Hi, Kaltern. If I recall correctly, the 'Duration' type is defined in 'regal.eternity', meaning you need to install the modules by following this guide, then running "Update_Submodules_Pull.bat" if the sub-modules are outdated.

With that said, judging by your other post, I'm guessing you're looking for a lower-level solution. I'll be posting a response shortly.


Kaltern(Posted 2016) [#16]
Hey good to see you're still around! I believe I have followed the installation instructions and tried that pull request but still getting the error. Looking at the logs it shows that it can't find modules within the regal folder, it's very recusrive (I check one module, and another throws up an error), so it's obviously a pathing issue... I'm just not sure where I've gone wrong.

I will also reply to my other thread (I don't like to open more than one thread, but I thought resurecting this one might be more relevant; I would probably like to use Transport as is is easier to get my head around, so I'll explain the issue over there :)