Doubts regarding receiving port based SMS using Socket connection in Android - android

I am trying to receive port based SMS with the below piece of code.
serverSocket = new ServerSocket(SERVERPORT);
Socket client = serverSocket.accept();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
line = null;
while ((line = in.readLine()) != null) {
Log.d("ServerActivity", line);
System.out.println("Reading Line is>>>>>>>>>>>>>"+line);
break;
}
} catch (Exception e) {
System.out.println("Exception While Reading SMS>>>>>>>>>>"+e);
}
Will it wait in the line of serverSocket.accept(); until it gets the port based SMS,Is this correct behaviour or I am making any issue which hangs at that place.I am not able to move beyond it.
I am not able to test fully,I am not having option of testing it here,sending the port message.
Did anyone came across this issue.Any Info regarding this will be useful.

I think you could try adding the while statement
serverSocket = new ServerSocket(SERVERPORT);
while(true){
Socket client = serverSocket.accept();
try {
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
line = null;
while ((line = in.readLine()) != null) {
Log.d("ServerActivity", line);
System.out.println("Reading Line is>>>>>>>>>>>>>"+line);
break;
}
} catch (Exception e) {
System.out.println("Exception While Reading SMS>>>>>>>>>>"+e);
}
}
and for as long as it's true, it will wait for the client to send a message. It's been a while since i last did one of these Working with Datagrams

Related

How to run arp command on an android phone

I have connected three cameras to my phone and i want to know their ip addresses. In windows and linux its with the arp command but this command does not run on the android terminal . How can i run arp command on android or it there any other way to achieve the same?
use this code work for me.
try {
Process process = Runtime.getRuntime().exec("cat /proc/net/arp");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
String output = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
Log.d("*************", ""+output);
}
catch (IOException e) {
e.printStackTrace();
System.out.println(e);
}
return "";

Simplest way to download text from URL

Is there a simplest way to download small text string from URL like this one:"http://app.georeach.com/ios/version.txt"
In iOS its pretty simple. But for android em not finding something good. what is the method for getting text like that from the above URL??
I used this code in onCreate of hello app,n app crashed:
try {
// Create a URL for the desired page
URL url = new URL("http://app.georeach.com/ios/version.txt");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
StringBuilder sb = new StringBuilder(100);
while ((str = in.readLine()) != null) {
sb.append(str);
// str is one line of text; readLine() strips the newline character(s)
}
in.close();
tv.setText(sb.toString());
} catch (MalformedURLException e) {
tv.setText("mal");
} catch (IOException e) {
tv.setText("io");
}
You have to create a new class extended from AsyncTask. You can't do network stuff in the main thread. It could work but you may not want to do that. Take a look at this link : http://developer.android.com/reference/android/os/AsyncTask.html
Also don't forget to add Internet permissions to your AndroidManifest.xml.
Try this:
URL url = new URL("http://bla-bla...");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream in = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
// your text is here
String text = sb.toString()
Do not forget to catch and handle IOException and close all streams.
An "easier" way would be this:
String url2txt = null;
try {
// Being address an URL instance
url2txt = new Scanner(address.openStream(), "UTF-8").useDelimiter("\\A").next();
} catch (IOException e) { ... }
The thing is what you consider "easier". As far as code goes, probably this is the shortest way, but it depends on what you want to do afterwards with the obtained text.

Android: Quick web requests

For my application I need to have the latest data from an webpage that is hosted on a server on my local network.
So I request the latest page with a HTTP GET and when the data is received, I send another request.
With my current implementation I reach around the 100 - 120 ms per request. Is there a possibility to make this quicker because it's the same url that is requested.
For example keep the connection open to the page and grep the latest data without setting up a new connection?
This page is around the 900-1100 bytes.
HTTP get code:
public static String makeHttpGetRequest(String stringUrl) {
try {
URL url = new URL(stringUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(300);
con.setConnectTimeout(300);
con.setDoOutput(false);
con.setDoInput(true);
con.setChunkedStreamingMode(0);
con.setRequestMethod("GET");
return readStream(con.getInputStream());
} catch (IOException e) {
Log.e(TAG, "IOException when setting up connection: " + e.getMessage());
}
return null;
}
Reading inputstream
private static String readStream(InputStream in) {
BufferedReader reader = null;
StringBuilder total = new StringBuilder();
try {
String line = "";
reader = new BufferedReader(new InputStreamReader(in));
while ((line = reader.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
Log.e(TAG, "IOException when reading InputStream: " + e.getMessage());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return total.toString();
}
As I know there isn't an implementation like you are asking for. I've been dealing a lot with http requests and the best thing you can do is your code. There is another thing which need some attention...your connection maybe slow and depending on that connection time can be more or in some cases which I've been dealing a lot the connection's timeout isn't enough big, but that's server problem.
In my opinion you should use what you have now.

Getting atomic time with Android

I am using the following code to connect and retrieve the UTC time from an AtomicTime server from an Android device:
public static final String ATOMICTIME_SERVER="http://132.163.4.101:13";
BufferedReader in = null;
try
{
URLConnection conn = new URL(ATOMICTIME_SERVER).openConnection();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String atomicTime;
while (true)
{
if ( (atomicTime = in.readLine()).indexOf("*") > -1)
{
break;
}
}
... do something
}
catch ...
It does not return any data. When accessing the URL from a browser, we get the following:
55884 11-11-19 07:40:22 00 0 0 824.5 UTC(NIST)
Can anyone help?
String atomicTime = "";
try
{
Socket socket = new Socket("132.163.4.101", 13);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
in.readLine(); // Ignore leading blank line
atomicTime = in.readLine();
socket.close();
}
catch....
This is because there is no HTTP service on TCP port 13. There is daytime service. You should use Socket instead of URLConnection. Or maybe find some NTP implementation for Android.

Android: BluetoothSocket readLine timeout

I should insert a timeout on a readLine for a bluetooth input stream.
BluetoothDevice device = BluetoothAdapter.getDefaultAdapter()
.getRemoteDevice("00:00:00:00:00:00");
sock = device.createInsecureRfcommSocketToServiceRecord(UUID
.fromString(insecureUUID));
sock.connect();
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
String line = in.readLine(); //if no answer from device..i'll wait here forever
do { [...]
} while ((line = in.readLine()) != null);
The connection works fine, but i've got a bluetooth serial converter linked to another device. If the second one is turned off i'll wait forever on the readLine. Any chance i can throw an exception or a timeout?
Thanks!!
I had the same problem and i solved it by creating a ResponderThread that extends Thread. This thread waits a certain amount of time and after that it checks if the input stream variable have changed.
try {
bufferedReader = new BufferedReader(new InputStreamReader(
bluetoothSocket.getInputStream()));
responderThread = new ResponderThread(bluetoothSocket, ACCESS_RESPONSE);
responderThread.start();
response= bufferedReader.read();
} catch (IOException ioe) {
// Handle the exception.
}
In my case the responderThread closes the socket if there is no response within 3 seconds and the execution goes into the catch block of the class where i create the responderThread. Then the exception is handled.

Categories

Resources