Trouble with TCP Client/Server

  • Replies:13
JohanJarvi
  • Forum posts: 16

Oct 30, 2017, 2:14:53 AM via Website

Hey guys,

I wrote this basic TCP Client in Android with a C++ Server running in Linux.

Now the thing is this worked through Ethernet with a Rooted Samsung Note 2, however, with a brand spanking new Samsung Galaxy Note 8, it no longer works. Everything works great on the Rooted Note 2. But when I try to open up the connection to the server on the Note 8 the app crashes.

Granted the Note 8 phone is not rooted, however, I didn't think it would be necessary since it supports Ethernet natively.

Basically my question is, does any of you guys have any experience with sockets and TCP communications on the Note 8 through an Ethernet connection, or have any idea of what might be causing the app to crash when I try to connect on the Note 8?

The server still establishes the connection in C++, but each time it does the app crashes on Android.

Cheers!

Reply
James Watson
  • Forum posts: 1,584

Oct 30, 2017, 2:52:09 AM via Website

Have you granted the 'full network access' permission to your APP on note 8?

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Reply
JohanJarvi
  • Forum posts: 16

Oct 30, 2017, 3:30:29 AM via Website

Well I have this in the manifest of the app itself:

But, I'm not sure if I have "full network access" on the APP from inside the phone, I'll look into how to do that and see if it fixes the problem. Thanks!

EDIT:
Looking at the APP permissions on the phone itself it says that it requires no permissions, but I have no clue how to enable "full network access" on that particular app.

— modified on Oct 30, 2017, 3:37:55 AM

Reply
James Watson
  • Forum posts: 1,584

Oct 30, 2017, 4:54:12 AM via Website

JohanJarvi

Well I have this in the manifest of the app itself:

But, I'm not sure if I have "full network access" on the APP from inside the phone, I'll look into how to do that and see if it fixes the problem. Thanks!

EDIT:
Looking at the APP permissions on the phone itself it says that it requires no permissions, but I have no clue how to enable "full network access" on that particular app.

Settings - Apps - your APP - Permissions,
tap the 'More' icon at upper-right corner, select "full network access" .

Hope this will work for you.

— modified on Oct 30, 2017, 4:55:43 AM

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Reply
JohanJarvi
  • Forum posts: 16

Oct 30, 2017, 5:15:46 AM via Website

So basically when I try to do that this is what I get. Thanks for trying to help but it's still not working out for me.

imageUser uploaded photo
User uploaded photo

Reply
James Watson
  • Forum posts: 1,584

Oct 30, 2017, 6:17:51 AM via Website

It shows that your app requires no permissions. I think this maybe a problem for a network app.

So, please double-check your manifest file, or post the code asking for permissions in your manifest file here.

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Reply
JohanJarvi
  • Forum posts: 16

Oct 30, 2017, 6:46:11 AM via Website

<?xml version="1.0" encoding="utf-8"?>

package="--UNDISCLOSED ON PURPOSE--">

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.NoActionBar">
    <activity android:name=".MainActivity"
        android:screenOrientation="landscape"
        android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".StreamActivity"
        android:launchMode="singleTop"
        android:parentActivityName=".MainActivity"
        android:banner="@style/Theme.AppCompat.NoActionBar"
        android:screenOrientation="landscape" />
</application>

That is my entire manifest file code right there. Is that what the problem is? Thanks for your help.

Had to edit out the part with the hyperlink cause it won't let me post "links"

Reply
James Watson
  • Forum posts: 1,584

Oct 30, 2017, 1:53:48 PM via Website

Then, connect the note 8 to your development PC, step debug, catch the exception when connecting to tcp server, and then view messages in logcat.

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Reply
JohanJarvi
  • Forum posts: 16

Oct 30, 2017, 11:09:39 PM via Website

The problem there is that I'm using the OTG cable for the ethernet adapter in order to communicate with the linux server,. I would have to set them both to a shared wifi connection and then run it through wifi through android studio in order to see the debug messages. But I guess that's what I'll have to do in order to get some sort of solution to this problem.

I was hoping it would be something simple. I'll get back to you when I've got some error messages.

Reply
JohanJarvi
  • Forum posts: 16

Oct 30, 2017, 11:20:28 PM via Website

10-31 08:17:58.624 11031-11031/au.com.httppraesidiumglobal.another_attempt E/AndroidRuntime: FATAL EXCEPTION: main
Process: "HIDDEN ON PURPOSE", PID: 11031
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1303)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
at java.net.SocketOutputStream.write(SocketOutputStream.java:157)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:295)
at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:141)
at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:229)
at java.io.BufferedWriter.flush(BufferedWriter.java:254)
at java.io.PrintWriter.newLine(PrintWriter.java:482)
at java.io.PrintWriter.println(PrintWriter.java:629)
at java.io.PrintWriter.println(PrintWriter.java:740)
at au.com.httppraesidiumglobal.cam_tcp_ss.TCPClient.sendMessage(TCPClient.java:34)
at au.com.httppraesidiumglobal.cam_tcp_ss.StreamActivity$1.run(StreamActivity.java:95)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6823)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1557)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)

Reply
JohanJarvi
  • Forum posts: 16

Oct 31, 2017, 2:06:23 AM via Website

Actually this problem was solved by adding the following lines to the MainActivity:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);

So if anyone else encounters a similar issue. This might help you.

Reply
James Watson
  • Forum posts: 1,584

Oct 31, 2017, 2:46:17 AM via Website

After studying your error message and Google-searching, I maybe have found the error.

You cannot perform network I/O on the UI thread on Honeycomb or later system(for example, note8). Technically, it is possible on earlier versions of Android(note2).

You'll need to run a background process or use AsyncTask to perform your network transaction on a background thread. You can use a Service or IntentService instead of AsyncTask.

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Reply
JohanJarvi
  • Forum posts: 16

Oct 31, 2017, 2:47:51 AM via Website

Read above, I fixed it using those two lines of code. Turns out it was the "StrictMode" causing me issues.

Reply
JohanJarvi
  • Forum posts: 16

Oct 31, 2017, 2:48:15 AM via Website

Thanks for all your help though!

Reply