I am writing an Android Application which has a broadcast receiver secured with permissions. The broadcast receiver on receiving requests from authenticated application scans port 80 of a hardcoded IP address. It returns the result of the socket connection attempt. In my code I have the socket connection called from OnReceive ( )
method. I am able to get to the receiver as i can see the toast messages. However, socket connection is caught by Exception E
. Here is my code for the receiver :
package com.example.naveenbreceive;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.os.Bundle;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;
public class breceiver extends Activity {
BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String Target="107.20.112.24";
int i=80;
int timeout=1000;
Toast.makeText(getApplicationContext(),"Intent Received",Toast.LENGTH_LONG).show();
Log.i("INFO","toast launched");
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(Target, i), timeout);
if (socket.isConnected())
{Toast.makeText(getApplicationContext(), "Connected to port 80", Toast.LENGTH_LONG).show();
socket.close();
}
else
{Toast.makeText(getApplicationContext(), "Unable to Connect port 80", Toast.LENGTH_LONG).show();
socket.close();
}
}
catch (UnknownHostException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(),"UnknownHost Exception",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(),"IO Exception",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(),"Exception",Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
IntentFilter filterView = new IntentFilter();
filterView.addAction("com.example.HACKING");
registerReceiver(mReceiver, filterView,"com.example.permission.naveen", null);
Toast.makeText(getApplicationContext(),"Inside Receiver",Toast.LENGTH_LONG).show();
}