Page 245 - Hacking Roomba
P. 245
226 Part III — More Complex Interfacing
Listing 11-1 Continued
int port = -1;
Socket socket;
InputStream input;
OutputStream output;
public RoombaCommTCPClient() {
super();
}
// portid is “host:port”
public boolean connect(String portid) {
String s[] = portid.split(“:”);
if( s.length < 2 ) {
logmsg(“bad portid “+portid);
return false;
}
host = s[0];
try {
port = Integer.parseInt(s[1]);
} catch( Exception e ) {
logmsg(“bad port “+e);
return false;
}
logmsg(“connecting to ‘“+host+”:”+port+”’”);
try {
socket = new Socket(host, port);
input = socket.getInputStream();
output = socket.getOutputStream();
} catch( Exception e ) {
logmsg(“connect: “+e);
return false;
}
}
public boolean send(int b) { // will also cover char
try {
output.write(b & 0xff); // for good measure
output.flush();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return false;
}
// ...other methods to implement RoomabComm...
}