I am actually trying to make a game that is played on a pc , controlled by an android tablet. UDP Packets from the android device (ma nexus 7) never reach the PC . Here is the code. Im so fcking mad btw…
Android:
final int bn = b.num;
Log.i(tag, "creating");
Thread t = new Thread(){
@Override
public void run(){
try {
sendPacket(bn);
} catch (SocketException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
private void sendPacket(int i) throws SocketException, UnknownHostException{
Log.i(tag, "creating buffer");
String t = ""+1;
byte[] buf = t.getBytes();
Log.i(tag, "creating address");
InetAddress ip = InetAddress.getByName(Dataholder.ip);
Log.i(tag, "creating socket");//EADDRNOTAVAIL error here
DatagramSocket s = new DatagramSocket(port , ip);
Log.i(tag, "creating packet");
DatagramPacket p = new DatagramPacket(buf , buf.length, ip,port);
try {
Log.i(tag, "sending");
s.send(p);
} catch (IOException e) {
}finally{
s.close();
}
}
};
Log.i(tag, "running");
t.run();`
PC:
byte[] data = new byte[1];//I only send 1 digit integers
@SuppressWarnings("resource")//nevermind closing the socket :D
DatagramSocket s = new DatagramSocket(4442);
DatagramPacket p = new DatagramPacket(data, data.length);
System.out.println("Now monitoring...");
while(true){
s.receive(p);
System.out.println("Button Pressed! --> "+new String(p.getData(),0,p.getData().length));//this works the best (I tested it myself)
}
It just stays at monitoring stage^
I am using the port 4442 on both of the machines. Also , the IP that is used on the android part is this one: