2

I am having trouble connecting my android emulator to my computer. I can send information to the android but when sending back it just fails.

Here is what I have so far:

public class sendDataToRegion extends AsyncTask<String,Void,List<String> >{

        final TextView who = (TextView)findViewById(R.id.txtWho);
        final TextView what = (TextView)findViewById(R.id.txtWhat);
        final TextView when = (TextView)findViewById(R.id.txtWhen);
        final TextView where = (TextView)findViewById(R.id.txtWhere);
        final TextView actionTaken = (TextView)findViewById(R.id.txtActionTaken);
        final TextView lengthOfTime = (TextView)findViewById(R.id.txtLengthOfTime);

        public List<String> dataSend2 = new ArrayList<String>();


        @Override
        protected List<String> doInBackground(String... params) {
            try
            {
                System.out.println("Mobile Server Program");

                String whoString = who.getText().toString();
                String whatString = what.getText().toString();
                String whenString = when.getText().toString();
                String whereString = where.getText().toString();
                String actionString = actionTaken.getText().toString();
                String lengthString = lengthOfTime.getText().toString();

                dataSend2.add(whoString);
                dataSend2.add(whatString);
                dataSend2.add(whenString);
                dataSend2.add(whereString);
                dataSend2.add(actionString);
                dataSend2.add(lengthString);


                int port = 4444;

                ServerSocket server = new ServerSocket(port);

                Socket socket=server.accept();

                DataOutputStream network = new DataOutputStream(socket.getOutputStream());


                for(int i = 0; i< dataSend2.size();i++){
                    network.writeUTF(dataSend2.get(i));
                }


            }
            catch (Exception e) {

            Log.e("TCP", "S: Error", e);

            }

            return dataSend2;
        }

        protected void onPostExecute( ) {
            System.out.println("Thread Finished " + dataSend2.size());


        }

    }//End of inner class

And it gets as far as creating the server socket, but nothing after that. Could someone point me in the right direction?

Thanks

UPDATE

here is the client:

try
        {

            String ip = "146.176.230.192";
            System.out.println("IP connected");

            int port = 4444;
            System.out.println("port connected");

            // Connect to the server
            Socket sock = new Socket(ip, port);
            System.out.println("socket created");

            // Create the incoming stream to read messages from
            DataInputStream network = new DataInputStream(sock.getInputStream());

            // Display our address
            System.out.println("Address: " + sock.getInetAddress());
            String line;

            while ((line = network.readUTF()) != null)
            {


                System.out.println(line);
            }



            sock.close();
        }
        catch (IOException ioe)
        {
            System.out.println("Connection failed");


        }
5
  • do you have a client connecting your server, and if so, can you post its code ? Commented Nov 15, 2012 at 15:52
  • If you can send info from the phone, why are you creating a server socket? Sockets are bidirectional, use the same socket. My bet is that there's a firewall at your carrier that would prevent incoming connections, and I doubt you have an IP to yourself. Commented Nov 15, 2012 at 15:53
  • don't you have to bind you serversocket ? Commented Nov 15, 2012 at 15:53
  • You only have to bind DatagramSockets, TCP sockets are bound on creation. Commented Nov 15, 2012 at 15:57
  • Did you try to forward ports between PC and Android? Commented Nov 15, 2012 at 15:58

1 Answer 1

3

use port 7612 and run this command to forward ports before you run your program:

adb forward tcp:7612 tcp:7612

Client side: default IP: 127.0.0.1

Server side: IP address is the wildcard address

Server side:

    private ServerThread mServer;
    ....
    java.net.ServerSocket s = new java.net.ServerSocket();

// bind, only port used 
java.net.InetSocketAddressendPoint = new InetSocketAddress(port);


  if( !s.isBound() ){
    s.bind(7612);
  }
      ....

Client side

 private           Socket mSocket;
 private static final int mConnectTimeout = 2500; // 2.5 seconds

 ....
 mSocket = new Socket();
 InetSocketAddress remoteAddr = new InetSocketAddress(127.0.0.1, 7612); 
    mSocket.connect(remoteAddr, mConnectTimeout); 
 ....

ServerThread class

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;



public class ServerThread implements Runnable{
public ServerThread(ServerSocket socket, OnConnectItf onConnect) // OnConnectItf is some callback
   {
    mServer   = socket;
    mCallback  = onConnect;
    mCancel    = false;
}

public void cancel(){
    mCancel = true;
}


public void run() {
    while (true){
        try {
            Socket s = mServer.accept();
            if (mCancel == true) break;

             ....
        }
        catch (IOException e) {
            // optional: implement on error handler
            break;
        }
    }

    return;
}

private ServerSocket mServer;
private OnConnectItf mCallback;
private boolean      mCancel;

}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.