Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8930665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:56:23+00:00 2026-06-15T08:56:23+00:00

Basically…… I am trying to connect to a device over a TCP/IP connection. I

  • 0

Basically……

I am trying to connect to a device over a TCP/IP connection.

I have a TextEdit field to enter the IP Address of the device and then on button click send the IP address through an intent to another class. Then connect to the device using the IP address and a fixed port of 32.

The connection itself does not require any authentication, but once connected the device asks for a username and password (in this case they are both root).

So once the connected I need to automatically send “root” wait for a response, send “root” again, and when a response of “SNX_COM>” (This is what I get when logged on) wait for further commands.

The further commands will come from button clicks.

The problem I have is that when I enter my IP address and press connect the app just force closes.

Please help……..

My IP Entry page (XML)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".IPEntry" >

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="79dp"
    android:ems="10"
    android:ellipsize="start"
    android:gravity="center_horizontal"
    android:hint="@string/enterip" />

<Button
    android:id="@+id/connect"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="14dp"
    android:text="@string/ipconbut" />

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/connect"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="21dp" />

</RelativeLayout>

My IP Entry page (Java Class)

package com.smarte.smartipcontrol;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;

public class IPEntry extends Activity {
public final static String ACTUALSMARTIP = "com.smarte.smartipcontrol.ACTU_IP";

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

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

/** Called when the user clicks the SendIP button */
public void sendip (View view) {
    Intent intent = new Intent(this, IPControl.class);
    EditText editText = (EditText) findViewById(R.id.smartipaddress);
    String actu_ip = editText.getText().toString();
    intent.putExtra(ACTUALSMARTIP, actu_ip);
    startActivity(intent);
}
}

My IP Connection and Command send page (XML)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".IPControl" >

<TextView
    android:id="@+id/textStatus"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/getModel"
    android:text="@string/status"
    android:textSize="24sp" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:ems="10"
    android:hint="@string/command" >

    <requestFocus />
</EditText>

<Button
    android:id="@+id/getModel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/editText1"
    android:text="@string/Modelbutton" />

</RelativeLayout>

My IP Connection and Command send page (Java class)

package com.smarte.smartipcontrol;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class IPControl extends Activity {

private Socket socket;
private String serverIpAddress;
private static final int REDIRECTED_SERVERPORT = 32;
public PrintWriter out;
public BufferedReader in;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Get the message from the intent
    Intent intent = getIntent();
    serverIpAddress = intent.getStringExtra(IPEntry.ACTUALSMARTIP);
    createConnection();

}

public void getModel(View view) {
    try {
        out.println("[c,l#,o13,i13crlf\r\n");
        //System.out.print("root\r\n");
        while(!in.ready());
        readBuffer();

    } catch(IOException e) {}
}

public void createConnection() {
    try {
         InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
         socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
      } catch (UnknownHostException e1) {
         e1.printStackTrace();
      } catch (IOException e1) {
         e1.printStackTrace();
      }
    try {
        out = new PrintWriter(new BufferedWriter(new         OutputStreamWriter(socket.getOutputStream())),true);
        in = new BufferedReader(new         InputStreamReader(socket.getInputStream()));
        while(!in.ready());
        readBuffer();
        out.println("root\r\n");
        //System.out.print("root\r\n");
        while(!in.ready());
        readBuffer();
        out.println("root\r\n");
        //System.out.print("root\r\n");
        while(!in.ready());
        readBuffer();
    } catch(IOException e) {}
}

private String readBuffer() throws IOException {
    String msg = "";

    while(in.ready()) {
        msg = msg + (char)in.read();
    }
    //System.out.print(msg);
    if(msg.indexOf("SCX_COM> ") != -1) return msg.substring(0,            msg.indexOf("SCX_COM> "));
    else return msg;
}
}

Android Manifest

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

<uses-permission android:name="android.permission.INTERNET"> </uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"> </uses-permission>


<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.smarte.smartipcontrol.IPEntry"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
<activity android:name="com.smarte.smartipcontrol.IPControl" />

</application>

</manifest>

……finally the strings (just incase)

<string name="app_name">SmartiP Control</string>
<string name="enterip">Enter IP Address</string>
<string name="menu_settings">Settings</string>
<string name="ipconbut">Connect</string>
<string name="hello_world">Hello world!</string>
<string name="title_activity_ipcontrol">IPControl</string>
<string name="Modelbutton">Model</string>
<string name="command">Enter Command</string>
<string name="status">Status</string>

LogCat..

12-01 03:19:50.977: E/AndroidRuntime(1537): FATAL EXCEPTION: main
12-01 03:19:50.977: E/AndroidRuntime(1537): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.smarte.smartipcontrol/com.smarte.smartipcontrol.IPControl}: android.os.NetworkOnMainThreadException
12-01 03:19:50.977: E/AndroidRuntime(1537):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at android.os.Looper.loop(Looper.java:137)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at android.app.ActivityThread.main(ActivityThread.java:5039)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at java.lang.reflect.Method.invokeNative(Native Method)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at java.lang.reflect.Method.invoke(Method.java:511)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at dalvik.system.NativeStart.main(Native Method)
12-01 03:19:50.977: E/AndroidRuntime(1537): Caused by: android.os.NetworkOnMainThreadException
12-01 03:19:50.977: E/AndroidRuntime(1537):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at libcore.io.IoBridge.connect(IoBridge.java:112)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at java.net.Socket.startupSocket(Socket.java:566)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at java.net.Socket.<init>(Socket.java:225)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at com.smarte.smartipcontrol.IPControl.createConnection(IPControl.java:50)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at com.smarte.smartipcontrol.IPControl.onCreate(IPControl.java:33)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at android.app.Activity.performCreate(Activity.java:5104)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-01 03:19:50.977: E/AndroidRuntime(1537):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-01 03:19:50.977: E/AndroidRuntime(1537):     ... 11 more

Please help me……….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:56:24+00:00Added an answer on June 15, 2026 at 8:56 am

    Chris Stratton in comments above.

    Thanks Chris….

    “Caused by: android.os.NetworkOnMainThreadException” is your problem. There is ample documentation of that issue online, so please do some research and then restructure your program to be compatible with Android’s UI responsiveness goals.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, I have a UIImageView that will loop through 8 PNGs over 0.5 seconds.
Basically, I have a list of delivery checkboxes one for deliver to this address
Basically, I have code where the user clicks a link, and then a div
Basically I would like to have some dictionary that is an abstaction over legacy
Basically, I have a button that when you press it, it runs its action
Basically I've made a form in Cakephp 2.1 with a jQuery button that appends
Basically I have a large set of data in excel, and I was wondering
Basically I have converted a tab delimited txt file into a list containing a
Basically, I have a main page (parent page) with a link that opens up
basically I have a function that resizes elements accordingly with jquery triggering the function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.