I need to send a command from an android phone, via wifi, to a router, through a wire, to a “cable box” <— just assume cable box for now. When sending the command from my laptop, I get a nice healthy flood of data on my wireshark reading. Also, the cable box responds and does what it is told. However, as soon as I try to put the App on my phone and use it, nothing happens. If I send the command from my phone to my computer I get 2 empty packets sent back and forth between my phone and computer. What am I doing wrong here? I’ve been struggling with this for 3 days now.
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;
public class TCPRunnable implements Runnable{
private final String ip;
private final int port;
private final ArrayList<String> commands;
//<editor-fold defaultstate="collapsed" desc="Constructors">
@Deprecated
public TCPRunnable(String ip,int port,ArrayList<String> Cmds){
this.ip=ip;
this.port=port;
this.commands = Cmds;
}
public TCPRunnable(String ip,int port,String Cmd){
this.ip=ip;
this.port=port;
this.commands = new ArrayList<String>();
this.commands.add(Cmd);
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Static Properties">
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Instance Properties">
//</editor-fold>
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(this.ip);
SocketAddress socketadd= new InetSocketAddress(serverAddr,port);
Log.d("TCP","R: Making the socket.");
Socket socket = new Socket();
//new Socket();//
Log.d("TCP","R: Connecting...");
socket.connect(socketadd, 1500);
// PrintWriter out = new PrintWriter (new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
InputStream socketReader = socket.getInputStream();
OutputStream socketWriter = socket.getOutputStream();
try {
for (int i = 0; i < commands.size();i++){
String Cmd = commands.get(i);
Log.d("TCP","R: Sending: '"+Cmd+"'");
byte [] iByte;
byte[] oByte = new byte[100];//in 32 bit signed integers
int CmdLen = 0;
CmdLen = commands.get(i).length();
oByte[0] = (byte) Integer.parseInt("AF", 16);
oByte[1] = (byte) Integer.parseInt("FA", 16);
oByte[2] = (byte) Integer.parseInt("F0", 16);
oByte[3] = (byte) (255-oByte[2]);
oByte[4] = (byte) (Cmd.length() +2);
oByte[5] = (byte) (255-oByte[4]);
oByte[6] = (byte) Integer.parseInt("07", 16);
iByte = Cmd.getBytes();
System.arraycopy(iByte, 0, oByte, 7, Cmd.length());
oByte[7+CmdLen] = (byte) Integer.parseInt("00",16);
oByte[8+CmdLen] = (byte) Integer.parseInt("00",16);
Object [] data = new Object [3];
data[0]=oByte;
data[1]=0;
data[2]=(9 + CmdLen);
//ping(IP,PORT);
//disconnect();
//connect();
socketWriter.write((byte[])data[0],(Integer)data[1],(Integer)data[2]);
socketWriter.flush();
Log.d("TCP","R: Sent.");
//Thread.sleep(500L);
}
}catch (Exception ex){
Log.e("TCP", "S: Sending failed: "+ex);
}finally{
socketWriter.flush();
socketWriter.close();
socket.close();
Log.d("TCP","R: Done.");
}
} catch (IOException ex) {
Log.e("IOEXCEPTION",""+ex);
}
}
//<editor-fold defaultstate="collapsed" desc="Static Methods">
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Instance Methods">
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Accessors">
//</editor-fold>
}
Edit:
I still can’t send data. but at least I know why ‘no data is sent’ now. It was because I was sending data to a laptop and not the machine itself.
Second Edit
where my code comments out //sleep(500L);, It should be uncommented. Otherwise you send data too quickly to receive a response.
Are those two packets like this?
Isn’t there a third ACK going back?
I see you issue a
flushon the stream, but I’m not sure if the OS honors that request. Can you try sending more data, like some kilobytes, and see if anything comes out? A wireshark capture would also be useful.Update
In the pastebin, you try to connect to .32 which is your laptop then. In the successful case, .32 connects to .42. Obviously if port 20036 is not open on your laptop and doesn’t run the service, you can’t connect to it.
There are some ways to work around this:
A) If using wireless connection on laptop and phone, you might be able to put the laptop into promiscuous mode to listen to all packages. Google helps here.
B) You may set up the laptop to redirect the connection to the box, so you can sniff on the laptop. This depends on the laptop OS.
C) Communicate directly with the box, and root your Android device then install a packet capture software directly on the Android device.
Update
rinetd for windows:
rinetd.conf:
Then: