0

When I want to make connection with my server my android application is crashing on the line where a make a new socket.

The code where I make the connection in my ClientAppl.java looks like this.

public class ClientAppl {
private InetAddress host;
private int port;
private Socket link = null;
ObjectInputStream istream;
ObjectOutputStream ostream;
private static MainActivity mainActivity;
private static ClientAppl instance = new ClientAppl(mainActivity);


private ClientAppl(MainActivity frm){
ClientAppl.mainActivity = frm;
}

public static ClientAppl getInstance(){
return instance;
}

public void makeConnection(String sIP, int port) throws IOException, java.net.ConnectException{
    if(link == null){

    System.out.println("Make connection...");
    this.host = InetAddress.getByName(sIP);
        this.port = port;
        link = new Socket(host, port);

        System.out.println("Inputkanaal & outputkanaal vastleggen...");
        ostream = new ObjectOutputStream(link.getOutputStream());
        System.out.println("OK - output");
        istream = new ObjectInputStream(link.getInputStream());
        System.out.println("OK - input");

        AcceptMessageHandler amh = new AcceptMessageHandler();
        Thread t = new Thread(amh);
        t.start();
    }
}

In MainActivity.java I added this code to start a connection.

txtServerIP = (EditText)findViewById(R.id.txtServerIP);
txtServerPoort = (EditText)findViewById(R.id.txtServerPoort);
try {
ClientAppl.getInstance().makeConnection(txtServerIP.getText().toString(),    Integer.parseInt(txtServerPoort.getText().toString()));
} catch (NumberFormatException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (ConnectException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

And this is my manifest-file. What's wrong? :(

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="be.howest"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
    <activity
        android:name="project.client.pc.view.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

2
  • What are the values you are passing to the socket constructor? Are you sure they are valid hostname and port number? Commented May 14, 2012 at 13:58
  • Oh yeah, forgotten to post the logcat. The values are the ip-address of the server and a portnumber. They are correct because my java version of the client work with the same values and give no errors. The Logcat: docs.google.com/document/d/… Commented May 14, 2012 at 15:59

2 Answers 2

1
5-14 15:55:28.669: E/AndroidRuntime(572): android.os.NetworkOnMainThreadException

You have always been warned against doing networking operations on the main (UI) thread, and you are now effectively prohibited from doing so by pro-active checks which will cause this fatal exception.

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

Comments

1

As Chris points out, you'll need to do all of your TCP socket interactions on threads other than the UI thread. In my code, I establish the socket in an AsyncTask, which sounds scary but if I can do it, you certainly can. Here's a decent tutorial:

http://www.vogella.com/articles/AndroidPerformance/article.html

Subsequently, spawn a new thread for each socket "conversation" -- that's not too difficult either.

1 Comment

Something else I've discovered about Android TCP socket comms: The precise nature of the restriction is that you can't do POTENTIALLY lengthy operations on the UI thread. I believe others have pointed this out as well. The interesting thing about this is that ONLY socket receive operations are potentially lengthy (from the application point of view). Apparently, you can SEND Tcp data from the UI thread without restriction. Of course, "If you don't expect a reply, why use Tcp?" is certainly a valid question.

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.