| Author |  | 
      
        | MrGibbage Super User
 
  
  
 Joined: October 23 2006
 Location: United States
 Online Status: Offline
 Posts: 513
 | 
          I was testing my Android app to see if it could connect to the PH Socket Server. I haven't used the
           | Posted: October 24 2011 at 22:09 | IP Logged |   |  
           | 
 |  socket server before--this is the first time. The first time I tried it, it did not work. No surprise
 there (at least for me). Programming never works out right the first time for me. So I tried telnet
 
 c:\> telnet localhost 8500
 
 and I pasted in
 
 FORMULA\nAuthorization: Basic TXJHaWJ(secret)lOnN3b2RvZw\nContent-Length: 25\n\nph_macro('AUTO_ARM_STAY')
 
 And I got back
 
 PHSSP/1.0 500 Server Error
 Server: PowerHome Socket Server/2.1b
 Content-type: text
 Content-Length: 139
 
 Request Type FORMULA\nAuthorization: Basic TXJHaWJ(secret)lOnN3b2RvZw\nContent-Length:
 25\n\nph_macro('AUTO_ARM_STAY') not supported by server.
 
 I'm using the java class Base64.encode to do the encoding, but since I am not getting an authorization
 error, I don't think the problem is with that.
 
 Netstat does show an open TCP port on 8500
 
 
 __________________
 Skip
 | 
       
        | Back to Top |     | 
       
       
        |  | 
        | MrGibbage Super User
 
  
  
 Joined: October 23 2006
 Location: United States
 Online Status: Offline
 Posts: 513
 | 
          I figured out how to make the commands work on telnet. I have to enter each nugget of information and then hit enter
           | Posted: October 25 2011 at 22:41 | IP Logged |   |  
           | 
 |  between each piece. "\n" in telnet won't cut it. I should have known that. But it still wouldn't work from my android
 client, even when I put each piece of information in it's own println().
 
 I then upgraded to 2.14 which made things worse (see the other thread I started).
 
 Here's the java I was using:
 
 
 String SocketServerAddress = db.getPhSsServerAddress();
 Integer SocketServerPort = db.getPhSsServerPort();
 Log.d(MY_DEBUG_TAG, "Setting up Socket: " + SocketServerAddress + ":" + SocketServerPort);
 Socket s = new Socket(SocketServerAddress, SocketServerPort);
 OutputStream out = s.getOutputStream();
 PrintWriter output = new PrintWriter(out);
 String body = db.getActionSocketBody(currentPage, selectedText);
 String user = db.getSsUsername();
 String pass = db.getSsPassword();
 String auth = Base64.encodeToString((user + ":" + pass).getBytes(), Base64.DEFAULT);
 output.println(reqType);
 output.println("Authorization: Basic " + auth);
 output.println("Content-Length: " + body.length());
 output.println();
 output.println(body);
 
 
 It's hard to troubleshoot now until I get the kinks worked out from the upgrade, but if anyone sees where the error of
 my ways is, I would appreciate it.
 
 
 __________________
 Skip
 | 
       
        | Back to Top |     | 
       
       
        |  | 
        | MrGibbage Super User
 
  
  
 Joined: October 23 2006
 Location: United States
 Online Status: Offline
 Posts: 513
 | 
          I solved it. This code works in case anyone else comes across this:
           | Posted: October 26 2011 at 19:02 | IP Logged |   |  
           | 
 |  
 
 String SocketServerAddress = db.getPhSsServerAddress();
 Integer SocketServerPort = db.getPhSsServerPort();
 s = new Socket(SocketServerAddress, SocketServerPort);
 Log.d(MY_DEBUG_TAG, "Setting up Socket: " + SocketServerAddress + ":" + SocketServerPort);
 DataOutputStream out = new DataOutputStream(s.getOutputStream());
 DataInputStream in = new DataInputStream(new BufferedInputStream(s.getInputStream()));
 Log.d(MY_DEBUG_TAG, "Connected to: " + s.getInetAddress() + " on port " + s.getPort());
 
 String body = db.getActionSocketBody(currentPage, selectedText);
 String user = db.getSsUsername();
 String pass = db.getSsPassword();
 String auth = Base64.encodeToString((user + ":" + pass).getBytes(), Base64.DEFAULT);
 String command = reqType + "\nAuthorization: Basic " + auth + "Content-Length: " + body.length() + "\n\n" + body + "\n\n";
 Log.d(MY_DEBUG_TAG, "SocketServerCommand: " + command);
 out.write(command.getBytes());
 out.flush();
 Log.d(MY_DEBUG_TAG, "Bytes written: " + out.size());
 String st = in.readLine();
 
 Log.d(MY_DEBUG_TAG, "SocketServerResponse: " + st);
 
 
 Next question:
 Are there any other authentication types other than Basic? Base64 isn't what I would call secure.
 
 Finally, Dave, it would be nice if there was an option to log socket server comms.
 
 __________________
 Skip
 | 
       
        | Back to Top |     | 
       
       
        |  | 
        | dhoward Admin Group
 
  
  
 Joined: June 29 2001
 Location: United States
 Online Status: Offline
 Posts: 4447
 | 
          Skip,
           | Posted: October 26 2011 at 20:35 | IP Logged |   |  
           | 
 |  
 Great work!! I hadnt had a chance to really look into
 this but have been following your posts.  Glad you were
 able to get it worked out.
 
 Socket server security is next on my list after upping
 the webserver security.  I will probably do something
 similar to the webserver cookies but am open to
 suggestions.  Base64 is definitely weak.
 
 I remember not logging the socket server on purpose due
 to the load of transactions going into the database (at
 least in my system since I use it extensively and the
 remote clients use this as their comm mechanism).  I can
 add it though and a user can choose whether to log the
 info or not.  Or perhaps logging to a text file would be
 preferred.  Im open to suggestions to what would be best.
 
 Dave.
 
 | 
       
        | Back to Top |       | 
       
       
        |  | 
        | MrGibbage Super User
 
  
  
 Joined: October 23 2006
 Location: United States
 Online Status: Offline
 Posts: 513
 | 
          I am not a security expert, but it looks like the different implementations of AES are
           | Posted: October 26 2011 at 21:03 | IP Logged |   |  
           | 
 |  considered to be the strongest and most widely ported to different platforms. In java it seems
 to be pretty simple to encrypt/decrypt. Same for php. I don't know anything about PowerBuilder,
 but I am sure it could be figured out. Of course you will need to encrypt the username/password
 pair, and the actual traffic.
 
 While I was setting up the socket server and especially while I was having problems, I longed
 for a way of seeing if PH was receiving my messages. I was just about to fire up wire shark
 when someone posted an answer to my question on Stack Overflow which solved everything. Anyway,
 if it was an option, it would be nice.
 
 __________________
 Skip
 | 
       
        | Back to Top |     | 
       
       
        |  | 
        | MrGibbage Super User
 
  
  
 Joined: October 23 2006
 Location: United States
 Online Status: Offline
 Posts: 513
 | 
          How do we use the FORMULATRIGGER and TRIGGER1 - TRIGGER5 commands? What goes in the data field and
           | Posted: November 05 2011 at 09:28 | IP Logged |   |  
           | 
 |  what happens when the a message like that is received?
 
 __________________
 Skip
 | 
       
        | Back to Top |     | 
       
       
        |  | 
        | nick7920 Senior Member
 
  
 
 Joined: March 04 2008
 Location: United States
 Online Status: Offline
 Posts: 193
 | 
          I am NO java expert but I needed to send some info to
           | Posted: September 07 2015 at 13:02 | IP Logged |   |  
           | 
 |  PH and I tried below and it worked. in above Skip
 example may be because of board missing \n and did not
 work for me.I verified with wireshark what is being
 sent. any way mine is from command line.
 
 what you need is to install SDK java so you have
 access to java.exe and javac.exe and library. (you can
 search and find how to install and check Sample
 helloworld to check it is working.)
 
 this is just for information for others, code is not
 optimize and not using command line parameters. ip and
 other information is example and changed for obvious
 reason.
 ---------------------------------
 import java.io.*;
 import java.net.*;
 import java.util.Base64;
 import java.util.UUID;
 
 public class PHClient {
 public static void main(String[] args) throws
 IOException {
 
 String SocketServerAddress ="192.168.0.119";
 Integer SocketServerPort =9500;
 String body = "ph_macro('LED_ON_OFF')";
 String user = "abcdefgh";
 String pass = "ABCDEFGH";
 String reqType = "FORMULA";
 
 try {
 
 
 
 Socket s = new Socket(SocketServerAddress,
 SocketServerPort);
 System.out.println("Setting up Socket: " +
 SocketServerAddress + ":" + SocketServerPort);
 DataOutputStream out = new
 DataOutputStream(s.getOutputStream());
 BufferedReader in  = new BufferedReader(new
 InputStreamReader(s.getInputStream()));
 
 System.out.println("Connected to: " +
 s.getInetAddress() + " on port " + s.getPort());
 String auth =
 Base64.getEncoder().encodeToString((user+":"+pass).get
 Bytes("utf-8"));
 String command = reqType + "\nAuthorization: Basic " +
 auth + "\nContent-Length: " + body.length() + "\n\n" +
 body + "\n";
 System.out.println("SocketServerCommand: " + command);
 out.write(command.getBytes());
 out.flush();
 System.out.println("Bytes written: " + out.size());
 String st = in.readLine();
 System.out.println("SocketServerResponse: " + st);
 }
 catch (IOException e) {
 System.err.println("Couldn't   get I/O
 for
 the connection to " );
 System.exit(1);
 }
 }
 }
 
 
 ---------------------------------------------
 I am sending info to run one of the macro here is
 example of compiling and running.
 
 C:\java>javac PHClient.java
 
 C:\java>java PHClient
 Setting up Socket: 192.168.0.119:9500
 Connected to: /192.168.0.119 on port 9500
 SocketServerCommand: FORMULA
 Authorization: Basic ABCDabce12345678fhgdhsjk12xxxa==
 Content-Length: 22
 
 ph_macro('LED_ON_OFF')
 
 Bytes written: 105
 SocketServerResponse: PHSSP/1.0 200 OK
 
 C:\java>
 ----------------------------------------------
 
 
 
 
 Edited by nick7920 - September 07 2015 at 13:06
 | 
       
        | Back to Top |     | 
       
       
        |  |