Question

I am having some major problems with my new project. I have a client app on my PC that is sending a simple String of data to the phone over the WiFi LAN connection. When recieved it should be split to its component parts and then an SMS sent.

I'll be honest, this is my first Android project but I'm determined to get this working.

When I send the command from my client, the app is failing and being caught in the catch labeled with Log.w("ServerRunnable", "Exception 2"); below. Id really appreciate some advice on this? I don't know why it won't work!

public class ListeningService extends Service {

private int iNotificationIcon = 1;
Handler hHandler;
Thread thServerThread = null;
private ServerSocket oServerSocket = null;

private static final String _S_DELIMITER = "##t2tDELIMITER##";
private static final int _I_SERVER_PORT = 50001;

@Override
public IBinder onBind(Intent arg0) {
    Toast.makeText(this, "Service Bound", Toast.LENGTH_LONG).show();
    return null;
}

@Override
public int onStartCommand(Intent oIntent, int iFlags, int iStartID) {
    addNotification(getString(R.string.app_name), "Server listening on xxx.xxx.xxx.xxx:ppppp", iNotificationIcon, true);

    /* Server Threading */      
    /*hHandler = new Handler() {
        @Override
        public void handleMessage(Message mMsg) {
            // TODO Auto-generated method stub
            showMessageBox(mMsg.getData().toString(), "Data Bundle", true);
            super.handleMessage(mMsg);
        }
    };*/

    this.thServerThread = new Thread(new ServerRunnable());
    //showToast("Thread Created but not started yet", false);
    this.thServerThread.start();

    return START_STICKY;
}
public class ServerRunnable implements Runnable  {
    public ServerRunnable() {
        /**/
    }

    public void run() {
        Socket oSocket;
        try {
            oServerSocket = new ServerSocket(_I_SERVER_PORT);
            Log.w("ServerRunnable", "oServerSocket created");
        } catch (Exception e) {
            e.printStackTrace();
            Log.w("ServerRunnable", "Exception 1");
        }
        while (!Thread.currentThread().isInterrupted()) {
            try {
                oSocket = oServerSocket.accept();
                CommsThread commThread = new CommsThread(oSocket);
                new Thread(commThread).start();
                Log.w("ServerRunnable", "oSocket created");
            } catch (Exception e) {
                e.printStackTrace();
                Log.w("ServerRunnable", "Exception 2");
            }
        }
    }       
}

public class CommsThread implements Runnable {

    private Socket oClientSocket;
    private BufferedReader brInput;

    public CommsThread(Socket oSocket) {
        this.oClientSocket = oSocket;
        try {
            this.brInput = new BufferedReader(new InputStreamReader(this.oClientSocket.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void run() {
        while (!Thread.currentThread().isInterrupted()) {
            try {
                String sRead = brInput.readLine();
                //hHandler.post(new SendSMSThread(sRead));
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

public class SendSMSThread implements Runnable {

    String sPhoneNumber = "";
    String sTextMessage = "";
    int iMessageLength = 0;

    public SendSMSThread(String sRead) {

        /* Received */
        //showMessageBox(sRead, "Raw Data", true);

        int iDelimiterStart = sRead.indexOf(_S_DELIMITER);
        int iDelimiterLen = _S_DELIMITER.length();

        /* Split the input */
        sPhoneNumber = sRead.substring(0, (iDelimiterStart+1));
        sTextMessage = sRead.substring((iDelimiterStart + iDelimiterLen));

        //showMessageBox(sTextMessage, sPhoneNumber, true);
    }

    public void run() {
        if ((sPhoneNumber.length() == 11) && (sTextMessage.length() > 0)) {
            sendSMS(sPhoneNumber, sTextMessage, true);
        }
    }
}

StackTrace:

01-23 23:48:42.673: W/ServerRunnable(15567): Exception 2
01-23 23:48:42.673: W/StackTrace(15567): java.lang.NullPointerException
01-23 23:48:42.673: W/StackTrace(15567):    at uk.co.tip2tail.wintextserver.ListeningService$ServerRunnable.run(ListeningService.java:78)
01-23 23:48:42.673: W/StackTrace(15567):    at java.lang.Thread.run(Thread.java)

This only seems to be occuring after I have stopped and restarted the service. The initial running of the service seems to be ok, although it still does nothing when the data is recieved! Im totally lost. Every book ive read has code like i do but this just wont work!

Any help appreciated.

Was it helpful?

Solution

I had over complicated things.

I have since went back and completely changed the code for the Service, only running 2 Threads.

And it now works!

Thank you for the constructive comments above. :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top