Hi all,

I am having some issues with sending and receiving data to and from a
socket.

This is the class I put together from an example on the web:

[code]
package com.kinrou.android.socket_test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

import android.os.AsyncTask;
import android.util.Log;

public class NetworkTask extends AsyncTask<Void, byte[], Boolean> {
    Socket nsocket; //Network Socket
    InputStream nis; //Network Input Stream
    OutputStream nos; //Network Output Stream

    @Override
    protected void onPreExecute() {
        Log.i("AsyncTask", "onPreExecute");
    }

    @Override
    protected Boolean doInBackground(Void... params) { //This runs on
a different thread
        boolean result = false;
        try {
            Log.i("AsyncTask", "doInBackground: Creating socket");
            SocketAddress sockaddr = new InetSocketAddress("some.url",
9339);
            nsocket = new Socket();
            nsocket.connect(sockaddr); //10 second connection timeout
            if (nsocket.isConnected()) {
                nis = nsocket.getInputStream();
                nos = nsocket.getOutputStream();

                Log.i("AsyncTask", "doInBackground: Socket created,
streams assigned");
                Log.i("AsyncTask", "doInBackground: Waiting for inital
data...");
                byte[] buffer = new byte[4096];
                int read = nis.read(buffer, 0, 4096); //This is
blocking
                while(read != -1){
                    byte[] tempdata = new byte[read];
                    System.arraycopy(buffer, 0, tempdata, 0, read);
                    publishProgress(tempdata);
                    Log.i("AsyncTask", "doInBackground: Got some
data");
                    read = nis.read(buffer, 0, 4096); //This is
blocking
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.i("AsyncTask", "doInBackground: IOException");
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
            Log.i("AsyncTask", "doInBackground: Exception");
            result = true;
        } finally {
            try {
                nis.close();
                nos.close();
                nsocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            Log.i("AsyncTask", "doInBackground: Finished");
        }
        return result;
    }

    public void SendDataToNetwork(String cmd) { //You run this from
the main thread.
        try {
            if (nsocket.isConnected()) {
                Log.i("AsyncTask", "SendDataToNetwork: Writing
received message to socket ");
                //nos.write(cmd.getBytes());
            } else {
                Log.i("AsyncTask", "SendDataToNetwork: Cannot send
message. Socket is closed");
            }
        } catch (Exception e) {
            Log.i("AsyncTask", "SendDataToNetwork: Message send
failed. Caught an exception");
        }
    }

    @Override
    protected void onProgressUpdate(byte[]... values) {
        if (values.length > 0) {
            Log.i("AsyncTask", "onProgressUpdate: " + values[0].length
+ " bytes received.");
          //  textStatus.setText(new String(values[0]));
        }
    }
    @Override
    protected void onCancelled() {
        Log.i("AsyncTask", "Cancelled.");
    }
    @Override
    protected void onPostExecute(Boolean result) {
        if (result) {
            Log.i("AsyncTask", "onPostExecute: Completed with an
Error.");
        } else {
            Log.i("AsyncTask", "onPostExecute: Completed.");
        }
    }
}
[/code]


the this the way I have am using it:

in onCreate I have this
networktask = new NetworkTask();
networktask.execute();


in onClick I have this
networktask.SendDataToNetwork("som data to the server");


What I am trying to achieve to connect to the socket, send some data
to this socket, when connected the socket would send some data back
which relates to the data sent in the 1st place.

the above class logs the following output at start up

04-19 11:31:40.561: INFO/AsyncTask(6978): onPreExecute
04-19 11:31:40.641: INFO/AsyncTask(6978): doInBackground: Creating
socket
04-19 11:31:40.971: INFO/AsyncTask(6978): doInBackground: Socket
created, streams assigned
04-19 11:31:40.971: INFO/AsyncTask(6978): doInBackground: Waiting for
inital data...

--- when the button is pressed
04-19 11:29:53.591: INFO/AsyncTask(6978): SendDataToNetwork: Writing
received message to socket


I however never get anything back from the server or if I do, I am not
capturing it...

Any help welcome.

Seb

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to