Android UDP Timeout not responsing - android

When I execute this code in android emulator
dataByte = new byte[1024];
ds = new DatagramSocket();
ds.setSoTimeout(3000);
dp = new DatagramPacket(dataByte, dataByte.length);
ds.connect(InetAddress.getByName(params[0]), Integer.valueOf(params[1]));
dp.setData("remdroid_test".getBytes());
ds.setSoTimeout(3000);
ds.receive(dp);
It results in not responding, what should i do?
Alternative suggestions on how to test a udp connection using Android are welcome.

If you are executing this code inside an Activity, then you are blocking the UI thread. If you block the UI for too long, then Android will force close your application.
One solution is to use an AsyncTask. The best option is probably to run it on a Thread from within a Service though.

Related

Android MulticastSocket closing implicitly

I seem to be having a small problem with MulticastSocket on Android: writing an SSDP-related application. The socket works just fine when I set everything up the first time, but when I stop discovery, and try to restart things, I just get a SocketException: Socket Closed. I'm not closing the socket, I'm simply stopping the Kotlin Coroutine that is responsible for calling socket.receive() in a loop. Example:
fun listenForPackets(): Flow<DatagramPacket> {
return flow {
multicastSocket.use {
val incomingBuffer = ByteArray(MULTICAST_DATAGRAM_SIZE)
while (true) {
val incomingPacket = DatagramPacket(incomingBuffer, incomingBuffer.size)
it.receive(incomingPacket)
emit(incomingPacket)
incomingPacket.length = incomingBuffer.size
}
}
}
}
The problem
So the problem is that when I try to call that function again, I get a SocketException: Socket Closed. The socket initialization code is run once, meaning that toggling discovery on/off will use the same socket multiple times; the following code is run once throughout the whole application:
multicastSocket = MulticastSocket(MULTICAST_PORT)
multicastSocket.reuseAddress = true
multicastSocket.joinGroup(multicastGroup)
multicastLock.acquire()
What I have tried
My first thought was that I was not cancelling the Kotlin Coroutine correctly. As a result, I switched to using typical Java Threads, to no avail. Starting the thread the first time works, but, restarting discovery yields the same problem. I have also tried to not leave the group, and keep the multicastLock acquired - same problem.
What works
What works is having the initialization code (where I assign the socket, join the group, and acquire lock) run every time I need to start a scan. At the end of the scan, I reset all of the variables (leave group, release lock, close socket). So my question becomes - is this the correct approach? Or am I simply doing something else wrong?
Just to re-iterate, I'm discovering packets just fine, the issue is with restarting the discovery. Thank you in advance for any help!

Android Socket, AsyncTask, Handler memory leak

I'm feeling frustrating. After few day, I raise the white flag and ask for help.
I've used this code sample:
https://github.com/sht5/Android-tcp-server-and-client
But every time I try to establish a connection the memory uses is increased by about 500KB, so if the client disconnects and then reconnects without closing the application after many attempts the memory becomes saturated.
I have already tried various methods including
cancel (true) in AsyncTask's doInbackground,
close and force the socket and in, out objects to null
call System.gc ()
but none of this worked.
I'm not sure, but try to invoke AsyncTask as
new InitTCPClientTask().execute(new Void[0]);
instead of
InitTCPClientTask task = new InitTCPClientTask();
task.execute(new Void[0]);

Read logs from Logcat without killing the tablet

I have to read the logs from Logcat and send them to a server through UDP.
For this task I have used this code: https://github.com/chemik/logcatudp
The main problem of that code is that the async Thread that is launched enters a while(true) loop that drains the tablet's battery on the long run.
Is there a way to get the logs in real time but without using a busy wait like that? Hopefully without adding some sleep(some_milliseconds) to reduce the problem?
It would be great to use some sort of event listener but I haven't found one. I have searched in every similar library but without any success.
The code is the following:
while (true) {
String sendingLine = "";
// assume that log writes whole lines
if (bufferedReader.ready()) {
logLine = bufferedReader.readLine();
sendingLine += logLine + System.getProperty("line.separator");
DatagramPacket packet = new DatagramPacket(sendingLine.getBytes(), sendingLine.length(),
InetAddress.getByName(mConfig.mDestServer), mConfig.mDestPort);
try {
mSocket.send(packet);
...
Any idea? Thanks.
Finally the answer was to put a Thread.sleep(10) in the while(true) loop.
It may seem really strange, but also with only 10ms of sleep it reduces the battery usage from almost 40% to 1%.

How do I open up a ServerSocket in my android application?

Ok so I am doing something that seems like it should be very easy. I am basically trying to open up a ServerSocket connection and then wait for the client to connect.
Here is my code.
ServerSocket serverSocket = new ServerSocket(6543);
Socket clientSocket = serverSocket.accept();
Whenever my code hits, serverSocket.accept();, I am throwing the following exception.
bind failed: EADDRINUSE (Address already in use)
So obviously my next step was to check and see if another port would work, it did not. Next I restarted the device and tried running the app and I got the same exception. I have given my app INTERNET permission and the device is rooted.
Here are my network interfaces that show up.
lo: ::1%1
lo: 127.0.0.1
eth0: //IPV6 address
eth0: 192.168.1.127
EDIT 1
Here is the serverSocket object info that I get when debugging.
ServerSocket[addr=192.168.1.121/192.168.1.121,port=0,localport=1234]
EDIT 2
I have the following available constructors in my ServerSocket
new ServerSocket(int port)
new ServerSocket(int port, int backlog)
new ServerSocket(int port, int backlog, InetAddress localAddress)
I tried using the 3rd constructor and same exception.
new ServerSocket(4567, 0, InetAddress.getLocalHost());
Alright so I finally figured out why my application was throwing an exception and if anyone has some feedback on this or would like to provide an answer as to why this was happening. I would greatly appreciate it.
So in my Thread that I created I need to postback to the main thread to update some TextView's based upon what was happening with the server connection. Well since I can't update the main thread from my self created thread I needed to add a Handler object to that Thread in order to update the UI. Removing that Handler completely allowed my application to successfully create the ServerSocket without throwing an exception.
Does the Handler somehow run the code before hand or what was happening here?
The code.
RemoteServerRunnable test = new RemoteServerRunnable();
Handler handler = new Handler();
handler.post(test);
test.start();

SocketS in Android

Here is a sample of my code
TextView textStatus = (TextView) findViewById(R.id.editText1);
ServerSocket serverSocket = new ServerSocket(SERVERPORT);
.
.
Socket client = serverSocket.accept();
.
.
textStatus.append("TEXT");
client.close();
This makes the android application force close. If I remove textStatus.append, than it works. Again, if I remove Socket client = ... and client.close(), it displays the TEXT on the screen.
So, the socket client = serverSocket.accept(); is affecting textStatus variable in some way.
Can anyone tell me what is wrong ?
textStatus is probably null. Check to make sure that R.id.editText1 is the actual id that you're using in your XML file.
Also, any potentially long running task should not be done on the UI thread. You're going to have very bad app preformance. Please see the Painless Threading article and try moving your server communication off to a different thread. Or consider using an IntentService instead. I prefer the IntentService.

Categories

Resources