Android UDP inbound traffic - android

I'm trying to keep a listening UDP port on 23000, the app works well in local environment.
But switching to 3G there are no chances, traffic is blocked.
I tried 'opening' the carrier channel sending 4bytes of data on the same port before receiving:
InetAddress serverAddr = InetAddress.getByName(SOULISSIP);
DatagramChannel channel = DatagramChannel.open();
socket = channel.socket();
//socket = new DatagramSocket();
socket.setReuseAddress(true);
//InetSocketAddress ia = new InetSocketAddress("localhost", SERVERPORT);
InetSocketAddress sa = new InetSocketAddress(SERVERPORT);
socket.bind(sa);
DatagramPacket holepunh = new DatagramPacket(new byte[]{0,1,2,3},4, serverAddr, SERVERPORT);
socket.send(holepunh);
// create a buffer to copy packet contents into
byte[] buf = new byte[200];
// create a packet to receive
DatagramPacket packet = new DatagramPacket(buf, buf.length);
Log.d("UDP", "***Waiting on packet!");
socket.setSoTimeout((int) opzioni.getDataServiceInterval());
// wait to receive the packet
socket.receive(packet);
UDPSoulissDecoder.decodevNet(packet);
socket.close();
is there a simple way to open the channel and receive UDP datagrams on port 23000?

So, the answer was in the request, while I was struggling with receiving socket. I had to bind the sender socket to the local port used for receiving datagrams:
//send
serverAddr = InetAddress.getByName(SOULISSIP);
DatagramChannel channel = DatagramChannel.open();
sender = channel.socket();
sender.setReuseAddress(true);
//amateur hole punch
InetSocketAddress sa = new InetSocketAddress(SERVERPORT);
sender.bind(sa);
//stuff to send
List<Byte> macaco = new ArrayList<Byte>();
macaco = Arrays.asList(pingpayload);
ArrayList<Byte> buf = buildVNetFrame(macaco, prefs);
//someone help me here...
byte[] merd = new byte[buf.size()];
for (int i = 0; i < buf.size(); i++) {
merd[i] = (byte) buf.get(i);
}
packet = new DatagramPacket(merd, merd.length, serverAddr, SOULISSPORT);
sender.send(packet);
The receiver and the sender are on separate threads, using same local port. .setReuseAddress(true) permitted this.

Related

Receive Datagram on USB/NCM Android Socket

I'm trying to receive a UDP Broadcast on the NCM0 Interface. The NCM Connection through USB is successfully established, but I don't receive anything.
I've been trying to bind the Socket to the NCM interface, but I'm getting invalid argument exceptions:
ArrayList<NetworkInterface> allInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for(Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();){
NetworkInterface intf = en.nextElement();
if (intf.getName().equals("ncm0")){
InetSocketAddress addr = new InetSocketAddress(intf.getInetAddresses(), 28500);
}
DatagramSocket socket = new DatagramSocket();
socket.bind(addr);
socket.setBroadcast(true);
I've been trying to receive the Datagram like such:
byte[] buf = new byte[1024];
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
What am I doin wrong?

Android receive DatagramPacket

I saw many pages, and try many advices, but still can't receive data via UDP on device (nexus 4). I work in my LAN, over wifi (3G is off).
I have a client, on PC (192.168.1.5) and consumer on Android device (192.168.1.3:54445)
Here is a client code:
String string = "hello udp";
DatagramSocket socket = null;
try {
address = InetAddress.getByName("192.168.1.3");
socket = new DatagramSocket();
DatagramPacket packet = new DatagramPacket(buf, buf.length, address, 54445);
socket.send(packet);
Here is a consumer code:
DatagramSocket socket = null;
stop = false;
InetAddress address = null;
try {
socket = new DatagramSocket(54445);
while (!stop) {
byte[] buffer = new byte[minBufSize];
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
Log.d(this.getClass().getSimpleName(), String.valueOf(packet.getLength()));
Debugger stop on socket.receive(packet). Consumer work in AsyncTask.
Thank you!
It's my fault, at first check you router settings.

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.

android UDP connection, not receiving any data

Im a newbie in this so please escuse me if i ask dumb questions.
Im trying to make a UDP connection between Eclipse's PC Emulator & a android phone
(or between two android phone devices).
I have a router and the phone connects to the internet thru router's wifi network. The PC is on same network also (direct cable router-PC connection). Im trying to send some text data from Server thread to Client thread but nothing is received/sent. :(
The Server java class (RE-EDITED, Server receives msg. from Client):
public class server implements Runnable
{
// the Server's Port
public static final int SERVERPORT = 6000;
// running Server thread.
public void run()
{
Log.d("redwing","server thread started.");
DatagramSocket serverSocket = null;
try
{
// Open Server Port
serverSocket = new DatagramSocket(server.SERVERPORT);
byte[] receiveData = new byte[32];
byte[] sendData = new byte[32];
// loop until "server_finished" becomes False.
while(createserver.server_finished)
{
if(renderer.gGL!=null) // ignore me, just a null pointer checker
{
// waiting for the incoming client's message packet
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
renderer.recv = new String(receivePacket.getData());
Log.d("server","packet received from client, ETA " + timing.getNow() + " " + renderer.recv); // timing getNow - just returns current system minute & second.
// server is replying to the client back with a message.
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
sendData = new String("server msg").getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
renderer.sent = new String(sendData, 0, sendData.length);
Log.d("server","packet sent to client, ETA " + timing.getNow() + " " + renderer.sent); // timing getNow - just returns current system minute & second.
}
}
// close the socket
if(serverSocket!=null) serverSocket.close();
serverSocket = null;
}
catch (Exception e)
{
Log.e("server", "Error", e);
if(serverSocket!=null) serverSocket.close();
serverSocket = null;
}
finally
{
if(serverSocket!=null) serverSocket.close();
serverSocket = null;
}
Log.d("redwing","server thread terminated.");
}
}
and the Client java class (RE-EDITED, Client does not receive msg from Server) :
public class client implements Runnable
{
public static final int CLIENTPORT = 5000;
// running Client thread.
public void run()
{
Log.d("redwing","client thread started.");
DatagramSocket clientSocket = null;
try
{
// getting local address
clientSocket = new DatagramSocket(server.SERVERPORT);
InetAddress IPAddress = InetAddress.getByName("192.168.0.100");
// displaying IP & hostname.
Log.d("client", "IP: " + IPAddress.getHostAddress() + " Name: " + IPAddress.getHostName());
byte[] sendData = new byte[32];
byte[] receiveData = new byte[32];
// Loop until client_finished becomes False.
while(createclient.client_finished)
{
if(renderer.gGL!=null) // ignore me, just a null pointer checker
{
// sending a message to the server
sendData = timing.getNow().getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, client.CLIENTPORT);
clientSocket.send(sendPacket);
renderer.sent = new String(sendData,0,sendData.length);;
Log.d("client","packet sent to server, ETA " + timing.getNow());
// waiting for the server packet message.
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
renderer.recv = new String(receivePacket.getData());
Log.d("client","packet received from server, ETA " + timing.getNow());
}
}
// close the socket
if(clientSocket!=null) clientSocket.close();
clientSocket = null;
}
catch (Exception e)
{
Log.e("client", "Error", e);
if(clientSocket!=null) clientSocket.close();
clientSocket = null;
}
finally
{
if(clientSocket!=null) clientSocket.close();
clientSocket = null;
}
Log.d("redwing","client thread terminated.");
}
}
the Permisions are enabled in the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<user-permission android:name="android.permission.NETWORK" />
<uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
I'm running the Server on the android PC Emulator, and the Client on my android Phone.
Both Client & Server threads run just fine but ... the problem is that i dont get any data exchanged between them. The client doesn't receive anyting and the server doesnt receive anything. The packets are sent but nothing received .
What im doing wrong ?
Please help me.
Thank you in advance.
After running your emulator, type it in command prompt - "telnet localhost ", then type "redir add udp:5000:6000". Connect client with port number 5000 and open udp server with port number 6000. Then you can receive client message from udp server.
Take a look for details
clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("<pc ip>"); // instead of "localhost"
public static final String SERVERIP = "127.0.0.1"; // 'Within' the emulator!
public static final int SERVERPORT = 6000;
/* Retrieve the ServerName */
InetAddress serverAddr = InetAddress.getByName(SERVERIP);
DatagramSocket socket = new DatagramSocket(SERVERPORT, serverAddr);

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