Adding DlinkAlarm as a driver for D-Link alarm/siren - replacing EspAlarm board
[iot2.git] / iotjava / iotruntime / IoTTCP.java
index ada53bd..f67c4d3 100644 (file)
@@ -6,6 +6,7 @@ import java.net.UnknownHostException;
 import java.net.SocketException;\r
 import java.net.Socket;\r
 import java.net.InetAddress;\r
+import java.net.InetSocketAddress;\r
 import java.net.ServerSocket;\r
 import java.io.InputStream;\r
 import java.io.OutputStream;\r
@@ -30,10 +31,15 @@ public final class IoTTCP {
        protected IoTTCP(Socket _socket) {\r
                socket = _socket;\r
        }\r
-\r
-       /**\r
-        * Class constructor\r
+       \r
+    /**\r
+        * Class constructor - no binding at first\r
         */\r
+    public IoTTCP() {\r
+        socket = new Socket();\r
+    }\r
+\r
+\r
        public IoTTCP(IoTDeviceAddress iotDevAdd) throws UnknownHostException, IOException {\r
 \r
                String strHostAddress = iotDevAdd.getHostAddress();\r
@@ -42,6 +48,22 @@ public final class IoTTCP {
 \r
                socket = new Socket(strHostAddress, iDstPort, InetAddress.getLocalHost(), iSrcPort);\r
        }\r
+       \r
+       /**\r
+        * bindAndConnect() method\r
+        */\r
+       public void bindAndConnect(IoTDeviceAddress iotDevAdd, boolean bindToLocal) throws UnknownHostException, IOException {\r
+\r
+               String strHostAddress = iotDevAdd.getHostAddress();\r
+               int iDstPort = iotDevAdd.getDestinationPortNumber();\r
+\r
+        // Check if we need to bind to a specific port locally\r
+        if (bindToLocal) {\r
+                       int iSrcPort = iotDevAdd.getSourcePortNumber();\r
+               socket.bind(new InetSocketAddress(InetAddress.getLocalHost(), iSrcPort));\r
+        }\r
+       socket.connect(new InetSocketAddress(strHostAddress, iDstPort));\r
+       }\r
 \r
        /**\r
         * getInputStream() method\r