1

I'm trying to create an android client app that connects to a server on my desktop.
I successfully telneted to the server from my laptop.
I have put INTERNET and NETWORK permissions in manifest file.
The program crashes when creating the socket and eclipse debuuger gets me to socket.class source file.

UPDATE: here's my complete code

public class EsmFamilActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.esm_famil, menu);
    return true;
}

//handler for sendtoserver
public void sendToServer(View view){

    ...


    String host = "192.168.1.7";
    int port = 8888;
    try{
        Socket s = new Socket(host, port);
        Writer out = new OutputStreamWriter(s.getOutputStream());
        out.write(data + "\n");
        out.flush();
        s.close();
    }catch (UnknownHostException ex) {
        ex.printStackTrace();
    }catch(IOException ex){
        ex.printStackTrace();
    }

}

}

and here is my logcat

01-18 21:43:06.908: D/TextView(1608): Constructor -- Got mEditTextBackgroundColor
01-18 21:43:09.658: W/IInputConnectionWrapper(1608): beginBatchEdit on inactive InputConnection
01-18 21:43:09.668: W/IInputConnectionWrapper(1608): endBatchEdit on inactive InputConnection
01-18 21:43:11.448: W/IInputConnectionWrapper(1608): beginBatchEdit on inactive InputConnection
01-18 21:43:11.448: W/IInputConnectionWrapper(1608): endBatchEdit on inactive InputConnection
01-18 21:43:46.018: D/AndroidRuntime(1608): Shutting down VM
01-18 21:43:46.018: W/dalvikvm(1608): threadid=1: thread exiting with uncaught exception (group=0x42f02500)
01-18 21:43:46.038: E/AndroidRuntime(1608): FATAL EXCEPTION: main
01-18 21:43:46.038: E/AndroidRuntime(1608): java.lang.IllegalStateException: Could not execute method of the activity
01-18 21:43:46.038: E/AndroidRuntime(1608):     at android.view.View$1.onClick(View.java:3678)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at android.view.View.performClick(View.java:4300)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at android.view.View$PerformClick.run(View.java:18026)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at android.os.Handler.handleCallback(Handler.java:730)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at android.os.Looper.loop(Looper.java:150)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at android.app.ActivityThread.main(ActivityThread.java:5162)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at java.lang.reflect.Method.invokeNative(Native Method)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at java.lang.reflect.Method.invoke(Method.java:525)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at dalvik.system.NativeStart.main(Native Method)
01-18 21:43:46.038: E/AndroidRuntime(1608): Caused by: java.lang.reflect.InvocationTargetException
01-18 21:43:46.038: E/AndroidRuntime(1608):     at java.lang.reflect.Method.invokeNative(Native Method)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at java.lang.reflect.Method.invoke(Method.java:525)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at android.view.View$1.onClick(View.java:3673)
01-18 21:43:46.038: E/AndroidRuntime(1608):     ... 11 more
01-18 21:43:46.038: E/AndroidRuntime(1608): Caused by: android.os.NetworkOnMainThreadException
01-18 21:43:46.038: E/AndroidRuntime(1608):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at libcore.io.IoBridge.connect(IoBridge.java:112)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at java.net.Socket.startupSocket(Socket.java:566)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at java.net.Socket.tryAllAddresses(Socket.java:127)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at java.net.Socket.<init>(Socket.java:177)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at java.net.Socket.<init>(Socket.java:149)
01-18 21:43:46.038: E/AndroidRuntime(1608):     at com.example.myfirstapp.EsmFamilActivity.sendToServer(EsmFamilActivity.java:61)
01-18 21:43:46.038: E/AndroidRuntime(1608):     ... 14 more
01-18 21:43:53.518: I/Process(1608): Sending signal. PID: 1608 SIG: 9
01-18 21:43:53.668: D/TextView(2467): Constructor - Got Res id for appearance for textColorPrimaryInverse
01-18 21:43:53.668: W/ResourceType(2467): Skipping entry 0x1060085 in package table 0 because it is not complex!
01-18 21:43:53.668: D/TextView(2467): Constructor - Got appearance for textColorPrimaryInverse
01-18 21:43:53.668: D/TextView(2467): Constructor -- Got mEditTextBackgroundColor
01-18 21:43:53.998: D/libEGL(2467): loaded /vendor/lib/egl/libEGL_POWERVR_SGX544_115.so
01-18 21:43:54.078: D/libEGL(2467): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX544_115.so
01-18 21:43:54.088: D/libEGL(2467): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX544_115.so
01-18 21:43:54.088: E/IMGSRV(2467): :0: PVRDRMOpen: TP3, ret = 42
01-18 21:43:54.098: E/IMGSRV(2467): :0: PVRDRMOpen: TP3, ret = 45
01-18 21:43:54.098: E/IMGSRV(2467): :0: PVRDRMOpen: TP3, ret = 46
01-18 21:43:54.098: E/IMGSRV(2467): :0: PVRDRMOpen: TP3, ret = 46
01-18 21:43:54.098: E/IMGSRV(2467): :0: PVRDRMOpen: TP3, ret = 46
01-18 21:43:54.108: E/IMGSRV(2467): :0: PVRDRMOpen: TP3, ret = 48
01-18 21:43:54.138: D/OpenGLRenderer(2467): Enabling debug mode 0
Pouria P
  • 565
  • 1
  • 8
  • 21
  • "_The program crashes_" What error messages are you getting? Post your logcat. – takendarkk Jan 18 '14 at 17:52
  • 1
    Look up [`AsyncTask`](http://developer.android.com/training/basics/network-ops/connecting.html#AsyncTask) you're getting a `NetworkOnMainThreadException`. – zapl Jan 18 '14 at 17:57
  • You are right noloader, my question is a duplicate of the question you mentioned. I couldn't find the OnMainThreadException in the logcat. sorry. – Pouria P Jan 18 '14 at 18:26

1 Answers1

0

I understood that the android app is running in the emulator on the server desktop. So first of all you need to change that LAN IP 192.168.x.x

Blockquote When working with the Android emulator, you may frequently need to access data hosted on the local web server using localhost. For example, your own web services are likely to be hosted on your local computer during development, and you’ll want to test them on the same development machine you use to write your Android applications. In such cases, you should use the special IP address of 10.0.2.2 (not 127.0.0.1) to refer to the host computer’s loopback interface. From the Android emulator’s perspective, localhost (127.0.0.1) refers to its own loopback interface.

cited from http://p2p.wrox.com/book-beginning-android-application-development-631/

Then if the android app is not in running in the server I guess you need some extra lines to avoid errors:

serverAddress =
InetAddress.getByName(“192.168.1.142”);
//--remember to change the IP address above to match your own-- and also the port
socket = new Socket(serverAddress, 500);

And also I'm asumming you're doing inside the extends AsyncTask class.

Community
  • 1
  • 1
mario ruiz
  • 880
  • 1
  • 11
  • 28
  • No i am running the app from my asus fonepad tablet connected to my desktop. I have tested using InetAddress but it didn't work. – Pouria P Jan 18 '14 at 18:19