Home AMX User Forum Duet/Cafe Duet

SocketConnection & Listener

jjamesjjames 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.

Comments

  • jjamesjjames Posts: 2,908
    Okay - figured out that when they say "This class is always used in conjunction with the SocketConnection class and must be implemented by any class wishing to receive TCP socket events." in regards to the SocketConnectionListener - that you actually use the "implements" keyword for the class you trying to use SocketConnection with. (Duh!)

    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:
    private SocketConnection client = null;
    

    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!
  • PhreaKPhreaK Posts: 966
    You're on the right track with having your class implement the listener - this will let you catch the events coming from the socket. To create the socket itself you need to create a SocketConnection object using the new keyword.
    private SocketConnection client = null;
    
    client = new SocketConnection(<parameter list>)
    

    <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.
    private SocketConnection client = null;
    
    client = new SocketConnection()
    
    client.initConnection(<parameter list>)
    

    Which one of these you use is up to you and how the rest of your code is structured.
  • jjamesjjames Posts: 2,908
    Talk about a freakin' pain!

    I'm getting these errors now:
    Line      3 (05:15:57.325)::  SocketConnection: [null]SocketConnection(10.24.42.69, 49749, 2000)
    Line      4 (05:15:57.335)::  SocketConnection: [null]initConnection(10.24.42.69, 49749, 2000)
    Line      5 (05:15:57.369)::  SocketConnection: [null]connect()
    Line      6 (05:15:57.370)::  Memory Available = 6896824 <72080>
    Line      7 (05:15:57.387)::  ERROR: SocketConnection: [null]write: Invalid output stream
    Line      8 (05:15:57.401)::  SNAPIRouter ERROR: Exception thrown! java.lang.NullPointerExceptionModuleComponent: Exception thrown! java.lang.NullPointerExceptio
    Line      9 (05:15:57.413)::  SocketConnection: [null]run(): Getting host address...
    Line     10 (05:15:57.414)::  ModuleComponent: 
    Line     11 (05:15:57.430)::  SocketConnection: [/10.24.42.69]run(): Creating socket...
    Line     12 (05:15:57.446)::  SocketConnection: [/10.24.42.69]createSocket: creating socket
    Line     13 (05:15:57.455)::  SocketConnection: [/10.24.42.69]createSocket(): initializing...
    Line     14 (05:15:57.559)::  SocketConnection: [/10.24.42.69]createSocket: socket created
    Line     15 (05:15:59.586)::  SocketConnection: [null]run(): InterruptedIOException - java.net.SocketTimeoutException: Read timed out
    Line     16 (05:15:59.593)::  SocketConnection: [null]disconnect()
    

    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.
    public void sendString(String command)
    	{
    		// constructing string here.....
    
    		if (client == null)
    		{
    			client = new SocketConnection(ipAddress, 49749, 8192, listener,
    					timeout);
    		}
    
    		if (client != null)
    		{
    			client.connect();
    			client.write(sb.toString().getBytes());
    		}
    
    		else
    		{
    			log(1, "Client was null");
    		}
    
    	}
    

    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.
  • PhreaKPhreaK Posts: 966
    It looks like you're trying to write to the socket before it's open. Personally I haven't played with the AMX SocketConnection classes but as it sets up a thread remember that you are playing with concurrency ie. your code doesn't stop and wait for the socket to connect before moving onto the write statement. If you're wanting to use the AMX classes you'll need to wait for the SocketConnection.CONNECTED status to come back to you're listener's handleSocketStatus() method before trying to use/abuse it.

    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.
Sign In or Register to comment.