I am creating a client socket connection between my computer and android.
Here is the code for the server:
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
class Server {
  public static void main(String[] args) {
    String ip = getIpAddress();
    if (ip != "false") {
        try {
            ServerSocket srvr = new ServerSocket(4444);
        Socket client = null;
        try {
            client = srvr.accept();
            PrintWriter out = new PrintWriter(client.getOutputStream(), true);
            System.out.print("Sending ip address: '" + ip + "'\n");
            out.print(ip);
            out.close();
            client.close();
            srvr.close();
        } catch(Exception e) {
            System.out.print(String.format("Accept failed: %s",e));
        }
        } catch (Exception e) {
            System.out.print(String.format("Could not listem on port: %s",e));
        }
    }
    else
    {
        System.out.print("Could not get ip address");
    }
  }
  public static String getIpAddress() {
    InetAddress host;
    String ipString = "false";
    try {
        host = InetAddress.getLocalHost();
        ipString = host.getHostAddress();
    } catch (UnknownHostException e) {
        System.out.println(e);
    }
    return ipString;
  }
}
When I run the client on the computer everything works. Here is the code for the computer client:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
public class Client {
  public static void main(String[] args) {
    System.out.print(RunSocketClient());
  }
  public static String RunSocketClient() {
    try {
        Socket clnt = new Socket("localhost",4444);
        BufferedReader in = new BufferedReader(new InputStreamReader(clnt.getInputStream()));
        String fromServer;
        fromServer = in.readLine();
        return fromServer;
    } catch (IOException e) {
        return "nothing";
    }
  }
}
When I run the this client code in android the system stops working, what is the error? Here is the android client code:
package com.example.clientapp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LinearLayout lView = new LinearLayout(this);
    TextView myText = new TextView(this);
    String ip = RunSocketClient();
    Log.d("ClientApp",ip);
    myText.setText(ip);
    lView.addView(myText);
    setContentView(lView);
  }
  public static String RunSocketClient() {
    try {
        Socket clnt = new Socket("localhost",4444);
        BufferedReader in = new BufferedReader(new InputStreamReader(clnt.getInputStream()));
        String fromServer;
        fromServer = in.readLine();
        return fromServer;
    } catch (IOException e) {
        return "nothing";
    }
  }
}
Here is the logcat of the application crash:
04-13 00:10:31.411: D/AndroidRuntime(13978): Shutting down VM
04-13 00:10:31.411: W/dalvikvm(13978): threadid=1: thread exiting with uncaught exception (group=0x410ab300)
04-13 00:10:31.411: E/AndroidRuntime(13978): FATAL EXCEPTION: main
04-13 00:10:31.411: E/AndroidRuntime(13978): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.clientapp/com.example.clientapp.MainActivity}: android.os.NetworkOnMainThreadException
04-13 00:10:31.411: E/AndroidRuntime(13978):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at android.os.Looper.loop(Looper.java:137)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at android.app.ActivityThread.main(ActivityThread.java:4745)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at java.lang.reflect.Method.invokeNative(Native Method)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at java.lang.reflect.Method.invoke(Method.java:511)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at dalvik.system.NativeStart.main(Native Method)
04-13 00:10:31.411: E/AndroidRuntime(13978): Caused by: android.os.NetworkOnMainThreadException
04-13 00:10:31.411: E/AndroidRuntime(13978):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at java.net.Socket.tryAllAddresses(Socket.java:108)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at java.net.Socket.<init>(Socket.java:177)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at java.net.Socket.<init>(Socket.java:149)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at com.example.clientapp.MainActivity.RunSocketClient(MainActivity.java:34)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at com.example.clientapp.MainActivity.onCreate(MainActivity.java:23)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at android.app.Activity.performCreate(Activity.java:5008)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-13 00:10:31.411: E/AndroidRuntime(13978):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-13 00:10:31.411: E/AndroidRuntime(13978):    ... 11 more
                In your application the NetworkOnMainThreadException is occurred. From the doc you ca see that
The exception that is thrown when an application attempts to perform a networking operation on its main thread.
So you have to call your RunSocketClient method from another thread rather than the main thread. Use a handler or asynctask or different thread to perform this operation.
To know about good designing you can see this link
http://developer.android.com/training/articles/perf-anr.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With