0

I'm trying to write an app that connects to a local server. I wrote the server side in JAVA, and I ran it with a client I implemented in JAVA. Im trying now to connect to the server through the app, i.e. with my anrdroid device, but for some reason I get an error.

Im using sockets, and this is what I'm trying to do (remember that it worked with a JAVA file):

String hostName = "localhost";
int portNumber = 5667;
private Socket echoSocket;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
        echoSocket = new Socket(hostName, portNumber);

    } catch (UnknownHostException e) {
        System.err.println("Don't know about host " + hostName);
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Couldn't get I/O for the connection to "
                + hostName);
        System.exit(1);
    }

I ran the server on my computer through local host. Any idea why I get the error?

EDIT:

I can't get the socket to work. Let's put aside the UI for a moment. I found many posts about socket communication but I still cant get it to work. I wrote a server side in JAVA and ran it on some arbitrary port, and now I want my device to communicate with the server on local host. That's what I tried to do:

public class MainActivity extends Activity {
String hostName = "10.0.0.1";
int portNumber = 5667;
private Socket echoSocket;


private void runSecondThread() {

    new Thread() {

        @Override
        public void run() {
            try {
                Log.i("try", "enter try");
                InetAddress serverAddr = InetAddress.getByName(hostName);
                Log.i("try", "InetAddress");
                echoSocket = new Socket(serverAddr, portNumber);
                Log.i("taggg", "echoSocket");

            } catch (UnknownHostException e) {
                Log.i("error","Don't know about host " + hostName);
                System.exit(1);
            } catch (IOException e) {
                Log.i("error","Couldn't get I/O for the connection to "+ hostName);
                System.exit(1);
            }
        }
    }.start();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    runSecondThread();

}

I get in the log "enter try" and "InetAddress", but not "echo socket". I dont get any exception so I really dont know what I did wrong.

itaied
  • 6,827
  • 13
  • 51
  • 86
  • possible duplicate of [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – Chris Stratton Apr 18 '14 at 18:29
  • 1
    In addition to the android threading violation referenced in the question yours is a duplicate of, your use of "localhost" as the machine to contact is erroneous and unworkable. "localhost" only ever refers to the machine on which the program is running, ie, your android device will attempt to contact itself. You must use an IP address or resolvable hostname, corresponding to the identify of the server on a network actually reachable from the Android device. Typically that means both are on local wifi, or your server is visible from the public Internet. – Chris Stratton Apr 18 '14 at 18:31
  • Thanks for you answer. I used an asyncTask as shown in the post you attached in your comment. I created the socket in the asyncTask class and executed it. Now my socket is defined only in that task, which is not an activity, and therefor I cannot use many form elements such as buttons and textViews. How can I return the socket from the doInBackground function? – itaied Apr 19 '14 at 12:57
  • You have to do all of the networking in a different thread. To do UI operations as a result, use runOnUiThread or a handler. – Chris Stratton Apr 19 '14 at 15:28
  • I have edited my question. Be glad if you could help me Chris. – itaied Apr 20 '14 at 07:18

1 Answers1

0

Spawn a new thread and instantiate your socket there.

Erik B
  • 2,810
  • 1
  • 34
  • 38