Android : My device is not visible on Lan

on Thursday, October 9, 2014


I have a server running on a local pc and I try to broadcast a udp packet from my phone to find the server. Until a few days the app was working perfect. But now when I try to find the server I get the packets on my pc but when I try to send back the response to my phone i cannot reach it.


Also if I try to ping my phone from my pc I get a "Reply from 192.168.1.3: Destination host unreachable."


BUT!! If i ping from my phone my pc then the phone becomes reachable ( I can ping and communicate with the server) for a while and then unreachable again...


What is going on?


Here is the Server code:



DatagramSocket socket;
try {
// Keep a socket open to listen to all the UDP trafic that is
// destined for this port
socket = new DatagramSocket(8888, InetAddress.getByName("0.0.0.0"));
socket.setBroadcast(true);

while (true) {
System.out.println(">>>Ready to receive broadcast packets!");
// Receive a packet
byte[] recvBuf = new byte[15000];
DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.length);
socket.receive(packet);
// Packet received
System.out.println(">>>Discovery packet received from: " + packet.getAddress().getHostAddress());
System.out.println(">>>Packet received; data: " + new String(packet.getData()));

// See if the packet holds the right command (message)
String message = new String(packet.getData()).trim();
if (message.equals("DISCOVER_FUIFSERVER_REQUEST")) {
byte[] sendData = "DISCOVER_FUIFSERVER_RESPONSE".getBytes();
// Send a response
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, packet.getAddress(), packet.getPort());
socket.send(sendPacket);
System.out.println(">>>Sent packet to: " + sendPacket.getAddress().getHostAddress());
}

}
} catch (IOException ex) {
}


and the client code:



@Override
protected InetAddress doInBackground(Void... params) {
InetAddress serverAdress = null;

// Find the server using UDP broadcast
try {
// Open a random port to send the package
DatagramSocket c = new DatagramSocket();
c.setSoTimeout(10000);
c.setBroadcast(true);

byte[] sendData = "DISCOVER_FUIFSERVER_REQUEST".getBytes();

// Try the 255.255.255.255 first
try {
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, InetAddress.getByName("255.255.255.255"), 8888);
c.send(sendPacket);
} catch (Exception e) {
}

// Broadcast the message over all the network interfaces
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface networkInterface = interfaces.nextElement();

if (networkInterface.isLoopback() || !networkInterface.isUp()) {
continue; // Don't want to broadcast to the loopback
// interface
}

for (InterfaceAddress interfaceAddress : networkInterface.getInterfaceAddresses()) {
InetAddress broadcast = interfaceAddress.getBroadcast();
if (broadcast == null) {
continue;
}

// Send the broadcast package!
try {
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, broadcast, 8888);
c.send(sendPacket);
} catch (Exception e) {
}

}
}
Log.v("this", "waiting for response");
// Wait for a response
byte[] recvBuf = new byte[15000];
DatagramPacket receivePacket = new DatagramPacket(recvBuf, recvBuf.length);
c.receive(receivePacket);
Log.v("this", "got response");

// We have a response

// Check if the message is correct
String message = new String(receivePacket.getData()).trim();
if (message.equals("DISCOVER_FUIFSERVER_RESPONSE")) {
// DO SOMETHING WITH THE SERVER'S IP (for example, store it in
// your controller)
serverAdress = receivePacket.getAddress();
}

// Close the port!
c.close();
} catch (IOException ex) {
Log.v("this", "exception");
}
return serverAdress;
}


the client stops at the "waiting for a response" log and then after 10 secs goes to the exception....


0 comments:

Post a Comment