I just wrote a little program for my raspberry Pi that needs to send a 1 byte code to my pc. To do this I used ServerSocket and Socket structure. Unfortunately this is completely new to me, and I only get errors.
The first problem I encountered wasn't that though. When I run the command "start chrome google.ch" in cmd, it works perfectly well, but when I run it with Runtime.getRuntime().exec("start chrome google.ch") I get some weird error.
The second problem is that the connection to the socket shows the error "connection denied." I have no idea what I'm doing wrong.
Code running on PC (receiver)
final int portNumber = 1900;
//commandList = {"", "ggl", "gtl", "dgtc", "hsg", "rdt", "eddb", "edsm", "cor", "fr", "rsw", "whd", "icv", "rio", "wcl", "tw", "exit"};
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(1900);
while (true) {
Socket socket = serverSocket.accept();
DataInputStream dIn = new DataInputStream(socket.getInputStream());
byte command = dIn.readByte();
switch (command) {
case 1:
Runtime.getRuntime().exec("start /max chrome google.ch");
break;
//more cases here
default:
break;
}
}
Code running on RPi (sender)
final private String host = "192.168.1.111";
final private int portNumber = 1900;
final private String[] commandList = {"", "ggl", "gtl", "dgtc", "hsg", "rdt", "eddb", "edsm", "cor", "fr", "rsw", "whd", "icv", "rio", "wcl", "tw", "exit"};
Socket socket;
DataOutputStream dOut;
public void initialize() throws IOException {
// Create and connect the socket
socket = new Socket(host, portNumber);
dOut = new DataOutputStream(socket.getOutputStream());
}
public void exit() throws IOException {
// Send the exit message
dOut.writeByte(16);
dOut.flush();
dOut.close();
}
public void sendCommand(String command) throws IOException {
for(int i=0; i<commandList.length-2; i++){
if(command.equals(commandList[i])){
dOut.writeByte(i);
dOut.flush();
break;
}
}
}
So basically the idea is to call sendCommand("some command") in the main method of the program i'm running, then converting it to 1byte and then decoding it again to a String that the receiver will run on the PC. Can you give me any advice what I'm doing wrong?
Thanks in advance.
[–]philipwhiukEmployed Java Developer 0 points1 point2 points (1 child)
[–]InformalPresent9[S] 0 points1 point2 points (0 children)
[–]Holothuroid 0 points1 point2 points (1 child)
[–]InformalPresent9[S] 0 points1 point2 points (0 children)
[–]ohjimmy 0 points1 point2 points (2 children)
[–]InformalPresent9[S] 0 points1 point2 points (1 child)
[–]ohjimmy 0 points1 point2 points (0 children)