Hi all

I want to program a server on android phone. I use socket.
Is the android emulator's IP address the same to the PC's?
I got exceptions from the client which I program on the same PC.

the exceptions is:
java.net.ConnectException: Connection refused: connect
java.lang.NullPointerException


the code is below:

java client:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;


public class JavaClient {

        public static void main(String argv[])
        {
        Socket ClientSocket = null;

        DataOutputStream os= null;
        DataInputStream is = null;

        try {
            // Retrieve the ServerName
            InetAddress serverAddr = InetAddress.getByName(SERVERIP);

            /* Create new UDP-Socket */
            ClientSocket = new Socket(serverAddr,SERVERPORT);

            os= new DataOutputStream(ClientSocket.getOutputStream());
            is = new DataInputStream(ClientSocket.getInputStream());
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host: hostname");
        } catch (IOException e) {
            System.err.println(e);
        }

        try {

            os.writeBytes("hello");

            byte[] input = new byte[100];
            while(is.read(input, 0, 10)!=-1)
            {
                System.out.println(new String(input));
            }

            os.close();
            is.close();
            ClientSocket.close();

       } catch (Exception e) {
           System.out.println(e);

       }
        }

    public static final String SERVERIP = "142.19.128.114"; //
'Within' the emulator!
    public static final int SERVERPORT = 4444;

}


android server:
public class AndroidServer extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        DataOutputStream os=null;
        DataInputStream is = null;
        ServerSocket socket = null;
        Socket ClientSocket = null;


        try {
            /* Create new UDP-Socket */
            socket = new ServerSocket(SERVERPORT);
            ClientSocket = socket.accept();

            os = new DataOutputStream(ClientSocket.getOutputStream());
            is = new DataInputStream(ClientSocket.getInputStream());

            /* By magic we know, how much data will be waiting for us
*/
            byte[] buf = new byte[100];

            while(is.read(buf, 0, 10)!=0)
            {
                Log.d("TCP", new String(buf));
                os.writeBytes("end");

            }

            os.close();
            is.close();
            socket.close();
            ClientSocket.close();


       } catch (Exception e) {
           System.out.println(e);
       }
        }


    public static final int SERVERPORT = 4444;
}

Regards,
Lei

--~--~---------~--~----~------------~-------~--~----~
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