Hi,

I'm would like to connect to android phones with raw sockets but i
can't get it to work. I use the following simple code...


[android activity server]
package org.androidapp.threadserver;

import java.io.IOException;
import java.net.*;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Server extends Activity {
        /** Called when the activity is first created. */

        TextView tv;
        ServerSocket ss;
        Socket s;
        int port = 20000;

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                tv = (TextView) findViewById(R.id.info);

                Thread thread = new Thread(new Runnable() {
                        public void run() {
                                try {
                                        ss = new ServerSocket(port);

                                        tv.setText("Server listening on port "+ 
port +"...\n");
                                        s = ss.accept();
                                        tv.append("Connection established!\n");

                                        // TODO Server-side logic
                                        ss.close();
                                        s.close();
                                        tv.append("Connection closed.");
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        tv.setText(e.toString());
                                }
                        }
                });

                thread.start();
        }

}



[android activity client]

package org.androidapp.threadclient;

import java.io.IOException;
import java.net.*;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Client extends Activity {
    /** Called when the activity is first created. */

        TextView tv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView)findViewById(R.id.info);

    }

    public void onStart(){
        super.onStart();
                Thread thread = new Thread(new Runnable() {
                        public void run() {
                                try {
                                        Socket s = new Socket("10.0.2.2",10000);
                                        s.getKeepAlive();
                                        tv.setText("Connection established!\n");
                                        // TODO Client-side logic
                                        s.close();
                                        tv.append("Connection closed.\n");
                                } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        tv.setText(e.toString());
                                }
                        }
                });

                thread.start();
        }

}

I always get the server activity to stop unexpectedly. what am i doing
wrong? could anyone show me some simple android code for establishing
a tcp socket connection between two android's?
Thank you for the help.

Gonzalo.

pd. i do add tcp redirection (redir add tcp:10000:20000). the client
seems to connect but that make the server stop unexpectedly.

--~--~---------~--~----~------------~-------~--~----~
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
[EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to