Unable to receive UDP Packets from a specific IP - android

I am trying to connect to a datalink that sends UDP packet. These packets are not 'broadcasted.' (I understand that there is two ways to send a UDP backet, to broadcast such that any machine can receive on the network, or to send to a specific IP...my understanding is limited to this, but I know that they are sent to a specific IP).
I am unable to receive the packets on my Android app I am testing with (Galaxy S3...internet permission has been enabled in the Manifest). I am binding to the host machine (IP) and its associated port. The packet traffic is seen in WireShark.
Here is my code. Anything glaring I am omitting?
try {
Log.d("UDP", "1");
DatagramSocket serverSocket = new DatagramSocket();
Log.d("UDP", "2");
InetAddress address = InetAddress.getByName(UDP_IP);
Log.d("UDP", "2a");
serverSocket.connect(address, UDP_SERVER_PORT);
Log.d("UDP", "2b");
serverSocket.setSoTimeout(5000); //5 sec wait for the client to connect
Log.d("UDP", "3");
byte[] data = new byte[1024];
Log.d("UDP", "4");
DatagramPacket packet = new DatagramPacket(data, data.length);
Log.d("UDP", "5");
Log.d("UDP", "S: Receiving...");
serverSocket.receive(packet);
//lock.release();
retVar = 1;
Log.d(" Received Packet", "Good");
}
catch (Exception e) {
Log.e("UDP", "S: Error", e);
}

Related

UDP packets sent by Android application over wifi sometimes not received by android devices

I have built a client-server application in Android(Compiling with java-6) to send and receive udp packets.
My client code is as follow:
DatagramSocket socket =new DatagramSocket();
socket.setBroadcast(true);
DatagramPacket packet = new DatagramPacket(dgram, dgramLength, host, port);
socket.send( packet );
socket.close();
My server code is as follows:
DatagramSocket socket =new DatagramSocket(this.port);
byte[] data=new byte[512];
DatagramPacket packet = new DatagramPacket(data, data.length) ;
// Wait for a response from the server
try
{
socket.receive(packet) ;
}
catch (Exception e)
{}
The issue that I am facing is that when I am working on a wired network then all the sent packets are being received by master properly but when I am running the same code over wifi then sometimes sent packets are being received and sometimes not. I want to know that whether it is the general behavior of a wifi network or not.
Any kind of help is appreciated.

Receive on UDP socket and send reply

I am working on a client/server project, the client being Android and the server being C#.
What I am trying to do is the Android app listens on a UDP socket and the C# server sends a message on that port. Android will then receive the message from the reply, and send back a response.
This is the code I have:
public void run()
{
Log.d(TAG, "Heartbeat manager thread starting");
try
{
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
DatagramSocket socket = new DatagramSocket(HEARTBEAT_PORT);
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
while (!cancelThread)
{
socket.receive(packet);
final String xml = new String(packet.getData(), 0, packet.getLength());
Log.v(TAG, xml);
XmlSettings xmlSettings = new XmlSettings();
xmlSettings.setIndent(true);
XmlWriter writer = new XmlWriter(xmlSettings);
writer.writeStartDocument();
writer.writeStartElement("HeartbeatManager");
writer.writeElementString("Command", Defines.ServerCommands.HeartbeatResponse.toString());
writer.writeEndElement();
buffer = writer.returnXmlOutput().getBytes();
DatagramPacket replyPacket = new DatagramPacket(buffer, buffer.length);
socket.send(replyPacket);
}
}
catch (SocketException ex)
{
Log.e(TAG, "Socket Exception Occurred: " + ex.toString());
}
catch (IOException ex)
{
Log.e(TAG, "IOException Occurred: " + ex.toString());
}
At the moment it throws an exception when I do the socket.send stating that the destination address is null.
You must set the destination address in the DatagramPacket before sending it.
The constructor you're using (without the address) is just for receiving, not for sending.
In UDP protocol, destination is set in the packet because there is not a connection (like TCP)
So in your code you are telling the socket, which has no concecpt of destination to send a packet without destination, so it'll throw and exception.
InetSocketAddress address = InetSocketAddress("www.google.com", 8080);
DatagramPacket replyPacket = new DatagramPacket(buffer, buffer.length, address);
socket.send(replyPacket);

Android device not capable of receive on DatagramSocket

I have two Android Devices, one with IP address 10.0.0.6 and another 10.0.0.9 I can ping both of them on my computer.
I send out a broadcast message from 10.0.0.6 UDP package and verified that with my wireshark,
the broadcast address is 10.0.0.255
DatagramSocket socket = new DatagramSocket(3400);
socket.setBroadcast(true);
byte[] buf = ("Hello from Android").getBytes();
DatagramPacket packet = new DatagramPacket(buf, buf.length, getBroadcastAddress(), 3400);
socket.send(packet);
However, my other android device didn't receive anything from that broadcast message.
DatagramSocket socket = new DatagramSocket(3400);
byte[] buf = new byte[30];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
It never come back from socket.receive.

Receive UDP packets on Android

I try to send and receive UDP packets continuously between an Android app and a server.
Is there any way to make that happen ?
My current networking code (running in a Thread) is shown bellow. The client is connected through 3G. The port configured client side is 1088.
The server just echo the packet to the client when received.
The server receive the packet correctly from the client but the client doesn't receive anything back.
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
Log.d(TAG, "S: Connecting...");
DatagramSocket socket = new DatagramSocket();
DatagramSocket receive_socket = new DatagramSocket(SERVERPORT, InetAddress.getByName("0.0.0.0"));
while(running) {
DatagramPacket packet_send = new DatagramPacket(msg, msg.length, serverAddr, SERVERPORT);
Log.d(TAG, "C: Sending: '" + new String(msg) + "'");
socket.send(packet_send);
// Prepare a UDP-Packet that can contain the data we want to receive
DatagramPacket packet = new DatagramPacket(buf, buf.length);
Log.d(TAG, "S: Receiving...");
// Receive the UDP-Packet
receive_socket.receive(packet);
Log.d(TAG, "S: Received: '" + new String(packet.getData()) + "'");
synchronized (this) {
wait(500);
}
}
I suspect the 3G connection is NATed (the server reports a port different from 1088).
If so, is there anything I can do to overcome that ?
Or is there anything wrong I do in my code ?
Turned out the code was working fine, the 3G service provider was blocking some UDP packets.

Sending data from android apps to TCP IP

I am able to connect to TCP IP netwrok but even if i am sending some
byte array but yet i am not seeing any output on Server console.
Server is C++ and i need to send data from my android apps. I am able
to connect but when i send data i am not getting any notification. the
followins is my code.
InetAddress serverAddr = InetAddress.getByName(serverIP);
SocketAddress socketadd= new InetSocketAddress(serverAddr, serverPort);
Log.d("TCP", "C: Connecting...");
//Socket socket = new Socket(serverAddr, serverPort);
Socket socket=new Socket();
try {
Log.d("TCP", "C: Sending: '" + msg + "'");
socket.connect(socketadd);
Log.e("Connect:", "Connect:");
DataOutputStream dataout = new
DataOutputStream(socket.getOutputStream());
dataout.flush();
byte haeader[]=new byte[6];
// String data="20110110,cswxerotest,cswxerotest";
// Packet p=new Packet();
//byte bdata[]=converttoCPP(getBytes());
byte bdata[]=getBytes();
//byte a[]=new byte[20];
//dataout.writeByte(5);
dataout.write(bdata,0,bdata.length);
dataout.flush();
I see you have a message msg to send, but I don't see that you convert it to bytes and send it anywhere (something like byte bdata[] = msg.getBytes() )

Categories

Resources