Sending datagram - network unreachable - android

I'm trying to broadcast udp packets between two Android devices on a hotspot.
I have a client that set up the hotspot, and a server that is connected to the hotspot. Sending a package from the client and receiving that packet on the server is working. But when I try to reply from the server, I get a SocketException, saying network is unreachable. I've checked the IP address and port number and both should be correct (I'm assuming the port is correct because it was taken from the packet itself).
The code for the server and client is below. Can anyone see where the code is going wrong or if it's just a network connectivity issue?
Client:
try {
//Open a random port to send the package
c = new DatagramSocket();
c.setBroadcast(true);
byte[] sendData = "DISCOVER_MV_REQUEST".getBytes();
try {
InetAddress addr = getIpAddress();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, getBroadcast(addr), 8888);
c.send(sendPacket);
} catch (Exception e) {
c.close();
e.printStackTrace();
return null;
}
// Broadcast the message over all the network interfaces
Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface 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) {
c.close();
e.printStackTrace();
return null;
}
}
}
//Wait for a response
byte[] recvBuf = new byte[15000];
DatagramPacket receivePacket = new DatagramPacket(recvBuf, recvBuf.length);
c.receive(receivePacket);
//Check if the message is correct
String message = new String(receivePacket.getData()).trim();
if (message.equals("DISCOVER_MV_RESPONSE")) {
//DO SOMETHING WITH THE SERVER'S IP (for example, store it in your controller)
Log.v("SOCKET", "got correct response");
c.close();
return "correct";
} else {
c.close();
Log.v("SOCKET", "wrong response");
return "wrong";
}
} catch (Exception e) {
c.close();
e.printStackTrace();
return null;
}
Server:
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) {
//Receive a packet
byte[] recvBuf = new byte[15000];
DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.length);
socket.receive(packet);
//See if the packet holds the right command (message)
String message = new String(packet.getData()).trim();
if (message.equals("DISCOVER_MV_REQUEST")) {
byte[] sendData = "DISCOVER_MV_RESPONSE".getBytes();
//Send a response
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, packet.getAddress(), packet.getPort());
socket.send(sendPacket); //where the error happens
socket.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}

Related

Broadcasting on UDP while receiving data

Is there any good option to send broadcast message on UDP port while receiving data from other devices on the same port?
I have this currently:
First I initialize 1 DiagramSocket for both reading and sending:
private void startSocket(){
try {
if(socket == null) {
socket = new DatagramSocket(3040);
socket.setBroadcast(true);
socket.setReuseAddress(true);
}
} catch (Exception e) {
e.printStackTrace();
}
}
In first Thread I have reading/receiving for current socket:
public class Server implements Runnable {
public Server(iBroadcastResponse iBResponse){
BroadcastClass.this.iBResponse = iBResponse;
}
#Override
public void run() {
startSocket();
while (DataHandler.isEmergencyMode.get() ) {
try {
byte[] buf = new byte[256];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
if(socket == null)
return;
socket.receive(packet);
String command = stringFromPacket(packet);
Log.d(TAG, command);
addToBroadcastCalls(command);
if(iBResponse != null)
iBResponse.onResponse();
} catch (Exception e) {
Log.e(TAG, "Server: Error!\n");
}
}
Log.e(TAG, "Stop!");
}
String stringFromPacket(DatagramPacket packet) {
return new String(packet.getData(), 0, packet.getLength());
}
}
If I run this, it works normally, reads receiving data. But if I try to send data in another thread:
socketThread = new Thread(){
#Override
public void run() {
startSocket();
String message = "Message";
byte[] buf = message.getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length);
try {
if(!socket.isConnected())
socket.connect(InetAddress.getByName("255.255.255.255"), 3040);
if (socket != null) {
socket.send(packet);
}
Log.d(TAG, "Send: " +message);
} catch (Exception e) {
e.printStackTrace();
}
}
};
socketThread.start();
When .send() is called it stops receiving data from other devices so now it only send message but stops receiving it from other devices.
Is there any good solution to this problem?
This was the solution
https://stackoverflow.com/a/25520279/1088975
I only changed
System.log...
To
Log.w(TAG, "Message...");
And I changed IP to be always "255.255.255.255" instead of
getBroadcastAddress()
which returns broadcast IP for current WIFI, which didn't work on my android devices

Udp not receiving data from server (android client, pc java server)

i am making a chat application for android using UDP.
Everything works fine in lan devices, I can send and receive messages from client and from server.
When i connect to an other network with my android, i can send messages, the server receives them but the response is never received on android.
Client Receive code
Runnable periodicTask = new Runnable() {
public void run() {
// Invoke method(s) to do the work
System.out.println("Waiting for data");
byte[] receiveData = new byte[2028];
byte[] sendData = new byte[2028];
boolean portTaken=false;
DatagramSocket serverSocket=null;
ServerSocket socket = null;
try {
socket = new ServerSocket(6667);
} catch (IOException e) {
System.out.println("ERRORRRRRRRRRRRRRR");
} finally {
if (socket != null)
try {
socket.close();
serverSocket= new DatagramSocket(6667);
} catch (IOException e) { e.printStackTrace(); }
}
DatagramPacket receivePacket = new DatagramPacket(receiveData,receiveData.length);
try {
serverSocket.receive(receivePacket);
System.out.println("GOT THEM!");
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayInputStream in = new ByteArrayInputStream(receiveData);
ObjectInputStream is = null;
try {
is = new ObjectInputStream(in);
} catch (IOException e) {
e.printStackTrace();
}
Data student = null;
try {
student = (Data) is.readObject();
l.add(student.toString());
mHandler.sendEmptyMessage(0);
System.out.println(student);
serverSocket.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
};
Server Send Code
for (DatagramPacket packet:clients){
if (!packet.getAddress().equals(receivePacket.getAddress())){
System.out.println("current ip address " + receivePacket.getAddress());
System.out.println("Sending to " + packet.getAddress());
DatagramSocket s = new DatagramSocket();
InetAddress local =packet.getAddress();//editTextAddress.getText().toString());//InetAddress.getByName("192.168.1.74");
System.out.println(local.toString());
//int msg_length =student.//sentence.length();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(outputStream);
os.writeObject(student);
byte[] data2 = outputStream.toByteArray();
int msg_length =data2.length;
DatagramPacket p = new DatagramPacket(data2, msg_length, local,
6667);
s.send(p);
s.close();
}
What might be the case ?
Thank you for your time

Broadcasting an UDP packet from an Android app and detecting it from Node.js

My Android app needs to know the ip of the machine running the server on my local network. My plan is to broadcast an UDP packet in my app and to send a packet back from the server, so the app know the ip.
This is my code on Android:
...
new SendUDPTask().execute();
...
/************ getBroadcastAddress: know where to send the signal to find the server ***********/
private InetAddress getBroadcastAddress() {
//setup
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcp = wifiManager.getDhcpInfo();
//complicated stuff
int broadcast = (dhcp.ipAddress & dhcp.netmask) | ~dhcp.netmask;
byte[] quads = new byte[4];
for (int k = 0; k < 4; k++)
quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);
//return result
InetAddress result = null;
try {
result = InetAddress.getByAddress(quads);
} catch (UnknownHostException e) {
e.printStackTrace();
}
return result;
}
/************ sendUDP: here e send the packet that will allow us to find the server ***********/
private DatagramPacket sendUDP(String req, int port) throws IOException {
DatagramSocket socket = new DatagramSocket(port);
socket.setBroadcast(true);
InetAddress broadcastAddress = getBroadcastAddress();
DatagramPacket packet = new DatagramPacket(req.getBytes(), req.length(), broadcastAddress, port);
socket.send(packet);
byte[] buf = new byte[1024];
packet = new DatagramPacket(buf, buf.length);
socket.setSoTimeout(3000);
//We wouldn't want to think our own message is the server's response
String myAddress = getMyAddress();
socket.receive(packet);
while (packet.getAddress().getHostAddress().contains(
myAddress))
{
socket.receive(packet);
}
socket.close();
return packet;
}
/***************************** getMyAddress: the device's address *****************************/
public String getMyAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress())
return inetAddress.getHostAddress();
}
}
}
catch (SocketException e) {
e.printStackTrace();
}
return null;
}
private class SendUDPTask extends AsyncTask<String, Void, DatagramPacket> {
private Exception exception;
protected DatagramPacket doInBackground(String... params) {
try {
DatagramPacket packet = sendUDP("Ping", 31415);
Log.d("GJ", packet.getAddress().getHostAddress().toString());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(DatagramPacket packet) {
}
}
This is my code on my computer (Node.js):
var PORT = 31415;
var HOST = '127.0.0.1';
var dgram = require('dgram');
var server = dgram.createSocket('udp4');
server.on('listening', function () {
var address = server.address();
console.log('UDP Server listening on ' + address.address + ':' + address.port);
});
server.on('message', function (message, remote) {
console.log(remote.address + ':' + remote.port + ' - ' + message);
});
server.bind(PORT, HOST);
I am not sending back anything yet, but I should see the console.log. I don't see anything, which must mean the packet didn't arrive.
Does anyone have an idea?
Thanks a lot
If you bind the socket to loopback (127.0.0.1) interface you won't receive messages broadcasted in the network (192.168.1.255 in this case). In the nodejs server bind the socket to INADDR_ANY.

Android and Arduino socket

I have created a server and client with Android and Arduino but I have a problem. Android reads only one time. Why? this is my code:
Client Android:
new Thread(new ClientThread()).start();
}
class ClientThread implements Runnable {
#Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName("192.168.1.240");
socket = new Socket(serverAddr, 8888);
if(socket == null)System.out.println("SOCKET NULL");
PrintWriter out = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream())),true);
inFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
msgFromServer = inFromServer.readLine();
System.out.println(msgFromServer);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (socket != null) {
System.out.println("STOP SOCKET");
// close socket
}
}
}
}
Arduino Server:
void loop() {
YunClient client = server.accept();
sensorValue = analogRead(sensorPin);
String myString = String(sensorValue);
if (client) {
String command = "none";
command = client.readString();
Serial.println(sensorValue);
client.print(myString+"\n");
}
}
LOGCAT:
07-24 11:44:24.468: D/OpenGLRenderer(19693): Enabling debug mode 0
07-24 11:44:25.363: I/System.out(19693): 121
121 is the value from Arduino. But this is showing only once.
It works only once. I want receive data from the Arduino every second.
Thank you guys!
You need to take the accept out of the loop. otherwise it send a string and wait for another connect from client.
YunClient client = server.accept();
void loop() {
sensorValue = analogRead(sensorPin);
String myString = String(sensorValue);
if (client) {
String command = "none";
command = client.readString();
Serial.println(sensorValue);
client.print(myString+"\n");
}
}
Also, I don't see where the client sends something to the server. Instead of System.out.println should't it be out.println?

A UDP receiver on android

I want to execute a udp receiver on eclipse. But its not working. The udp sender works properly and the packets are sent through the specific port. But emulator is not able to receive any packets through a udp sender. Help needed.
i don't know what your scenario is but according to my scenario i just setup a UDP server on my system(Windows 7) using php script and successfully sended and received UDP packets from android emulator with the following code.
String receivedString="";
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
sendData = stringToBeSended.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData,
sendData.length, IPAddress, port);
DatagramSocket clientSocket;
try {
clientSocket = new DatagramSocket();
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
clientSocket.receive(receivePacket);
receivedString = new String(receivePacket.getData());
clientSocket.close();
} catch (SocketException e) {
Log.v("SocketExceptionOccured", e.toString())
e.printStackTrace();
} catch (IOException e) {
Log.v("IOExceptionOccured", e.toString())
e.printStackTrace();
}
Toast.makeText(getBaseContext(), receivedString, Toast.LENGTH_LONG).show();

Categories

Resources