SocketConnection & Listener
jjames
Posts: 2,908
I'm quite embarrassed to ask as I consider myself a "decent" programmer and can usually figure things out, theory isn't my strong point and instead rely on real world usage. Once I kinda of understand the basic flow of things and syntax, I'm usually set to go.
However, Duet (Java rather) is throwing me for a loop - particularly how it goes about using sockets (TCP as of now, haven't jumped into receiving a multicast yet). I'm trying to use AMXTools' SocketConnection & SocketConnectionListener classes - but I'm not understanding how to open the connection, write to the device and receive the response.
I guess I've been spoiled with the whole ip_client_open() / close() and string events that make IP control over devices super easy. Heck - even C# wasn't that hard in doing stuff over IP.
Anyway, if anyone has an example as to how to open / use a socket, receive the string, and close it - that would be super great! Most everything else I can figure out it seems with Duet - but this is just a huge stumper for some reason.
However, Duet (Java rather) is throwing me for a loop - particularly how it goes about using sockets (TCP as of now, haven't jumped into receiving a multicast yet). I'm trying to use AMXTools' SocketConnection & SocketConnectionListener classes - but I'm not understanding how to open the connection, write to the device and receive the response.
I guess I've been spoiled with the whole ip_client_open() / close() and string events that make IP control over devices super easy. Heck - even C# wasn't that hard in doing stuff over IP.
Anyway, if anyone has an example as to how to open / use a socket, receive the string, and close it - that would be super great! Most everything else I can figure out it seems with Duet - but this is just a huge stumper for some reason.
0
Comments
Now - here's my next problem: in initConnection, one of the parameters is a SocketConnectionListener and am defining it like so in my class without any other modifications:
My problem is how and what do I populate it with? I've tried "client = this", although it compiles it just kills the module completely and it won't even run. I think that if I get this part I should be good to go.
An example would still be great though if anyone is willing to share. Thanks!
<parameter list> will be vary depending on which constructor you use. One of the parameters though is a SocketConnectionListener which is what you want to pass in as 'this' (or whatever class you're using for parsing guff coming from the socket).
The other way you can do this is to use the default constructor to create the object then use initConnection() to set up the socket.
Which one of these you use is up to you and how the rest of your code is structured.
I'm getting these errors now:
Here's my code. First, I know it's not clean, and probably not optimal, but - for whatever reason client (SocketConnection) was becoming null at some point after I had created it - so I just decided to recreate at the time of sending my string - this appears to be dandy. For whatever reason though - it's either not reading, or not writing the string I want sent.
I think this NetLinx programmer is about to give up on Duet . . . I've spent nearly a week trying to figure just this out. Maybe I should give it a whirl with a serial module . . . sheesh.
Socket comms are nice and straight forward in java once you get you're head around them. There's a good little tutorial on reading and writing to a socket that's a good start if you want to look at rolling your own class. Also, the nice thing is you're now playing with a 'real world language' which means there is much larger community out there waiting to help. Hit up Stack Overflow or Google for any questions you have.