amindotb
amindotb

Reputation: 15

Android Socket Error or crash

I'm using the following code to send data to a tcp server but I got errors. Any idea? Do I need to use a thread? Can I run it directly on a function ?

The Msg & IPo & Proto are Editboxes in layout. I think my problem is using the socket!

    Socket Clientsocketo;
String MsgOut="Nothing Happend";
TextView ad;

public void SendData(View v) throws IOException {
    EditText aa = (EditText)findViewById(R.id.EditText01);
    String Msg = aa.getText().toString();

    EditText ab = (EditText)findViewById(R.id.editText);
    String IPo = ab.getText().toString();

    EditText ac = (EditText)findViewById(R.id.editText2);
     int Porto  = Integer.parseInt(ac.getText().toString());

     InetAddress serverAddress = InetAddress.getByName(IPo);
    try{
        Clientsocketo = new Socket(serverAddress,Porto);
        ObjectOutputStream oos = new ObjectOutputStream(Clientsocketo.getOutputStream());
        oos.writeObject("Hello World");

        ObjectInputStream ois = new ObjectInputStream(Clientsocketo.getInputStream());
        String massagedf = (String)ois.readObject();
        ad.setText(massagedf);
        // mHandler.sendMessage(serverMessage);

        oos.close();
        ois.close();
    }
    catch(Exception e){
        ad.setText("Error");
    }

}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ad = (TextView) findViewById(R.id.textView);
}

Upvotes: 0

Views: 64

Answers (1)

Helix
Helix

Reputation: 144

Network operations must be handled in an AsyncTask. This allows network operations to continue without blocking the main thread. The best way to use an AsyncTask is to create a sub-class. Something like this:

public class SendDataTask extends AsyncTask<Void, Void, Void> {

    Socket clientSocketTo;

    String msg;
    String ipo;
    int porto;
    InetAddress serverAddress;

    public SendDataTask(View v){
        EditText aa = (EditText) v.findViewById(R.id.EditText01);
        msg = aa.getText().toString

        EditText ab = (EditText) v.findViewById(R.id.editText);
        ipo = ab.getText().toString();

        EditText ac = (EditText) v.findViewById(R.id.editText2);
        porto = Integer.parseInt(ac.getText().toString());
        serverAddress = InetAddress.getByName(ipo);

    }

    protected void doInBackground(Void... arg){
        try{
            clientSocketTo = new Socket(ServerAddress,Porto);
            ObjectOutputStream oos = new ObjectOutputStream(clientSocketTo.getOutputStream());
            oos.weriteObject("Hello World")

            ObjectInputStream ois = new ObjectInputStream(clientSocketTo.getInputStream());
            String massagedf = (String) ois.readObject();
            ad.setText(massagedf);

            oos.close();
            ois.close();

        }catch (Exception e){

        }

    }

    protected void onPostExecute(Void result){

    }

}

To call this you would instantiate the class passing your view and then call the execute method:

new SendDataTask(yourView).execute();

None of this code has been tested... Hope it helps!

More Information: http://developer.android.com/reference/android/os/AsyncTask.html http://androidsrc.net/android-client-server-using-sockets-client-implementation/

Upvotes: 1

Related Questions