10 /** Class IoTSocket is the basic class for IoT RMI
11 * socket communication. This class will be extended
12 * by both IoTSocketServer and IoTSocketClient
14 * Adapted from Java/C++ socket implementation
16 * @see <a href="https://www.keithv.com/software/socket/</a>
18 * @author Rahmadi Trimananda <rtrimana @ uci.edu>
22 public abstract class IoTSocket {
27 protected byte data[];
29 protected Socket sock;
30 protected BufferedInputStream input;
31 protected BufferedOutputStream output;
36 protected static int BUFFSIZE = 128000; // how many bytes our incoming buffer can hold
41 protected IoTSocket(int _port) throws IOException
44 data = new byte[BUFFSIZE];
49 * sendBytes() sends an array of bytes
51 public void sendBytes(byte vals[]) throws IOException
53 int len = vals.length;
56 output.write(vals, 0, len);
64 * receiveBytes() receives an array of bytes
66 public byte[] receiveBytes(byte val[]) throws IOException
71 // Wait until input is available
72 while(input.available() == 0);
73 // Read the maxlen first
74 int maxlen = (int)input.read();
76 System.out.println("IoTSocketClient/Server: Sending more bytes then will fit in buffer!");
77 val = new byte[maxlen];
78 while (totalbytes < maxlen)
80 numbytes = input.read(data);
81 // copy the bytes into the result buffer
82 for (i=totalbytes; i<totalbytes+numbytes; i++)
83 val[i] = data[i-totalbytes];
84 totalbytes += numbytes;
86 // we now send an acknowledgement to the server to let them
96 * Close socket connection
98 public void close() throws IOException
107 public void sendAck() throws IOException
119 public void receiveAck() throws IOException
122 ack = (int) input.read();