10 /** Class IoTSocketClient is a communication class
11 * that provides interfaces to connect to either
12 * Java or C++ socket endpoint
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 class IoTSocketClient {
30 BufferedInputStream input;
31 BufferedOutputStream output;
36 static int BUFFSIZE = 128000; // how many bytes our incoming buffer can hold
41 public IoTSocketClient(int _port, String _address, int rev) throws IOException
45 sock = new Socket( InetAddress.getByName(_address), port );
46 input = new BufferedInputStream(sock.getInputStream(), BUFFSIZE);
47 output = new BufferedOutputStream(sock.getOutputStream(),BUFFSIZE);
49 catch ( IOException e ) {
52 data = new byte[BUFFSIZE];
53 // now we want to tell the server if we want reversed bytes or not
60 * sendBytes() sends an array of bytes
62 public void sendBytes(byte vals[]) throws IOException
64 int len = vals.length;
67 output.write(vals, 0, len);
75 * receiveBytes() receives an array of bytes
77 public byte[] receiveBytes(byte val[]) throws IOException
82 // Read the maxlen first
83 int maxlen = (int)input.read();
85 System.out.println("IoTSocketClient/Server: Sending more bytes then will fit in buffer!");
86 val = new byte[maxlen];
87 while (totalbytes < maxlen)
89 numbytes = input.read(data);
90 // copy the bytes into the result buffer
91 for (i=totalbytes; i<totalbytes+numbytes; i++)
92 val[i] = data[i-totalbytes];
93 totalbytes += numbytes;
95 // we now send an acknowledgement to the server to let them
105 * Close socket connection
107 public void close() throws IOException
116 private void sendAck() throws IOException
128 private void receiveAck() throws IOException
131 ack = (int) input.read();