0

I wrote a simple java socket server. I can connect to it using my java client.

However when I try to connect an android device to it, it gives the following error : socket failed: eacces (permission denied)

Here is the code :

btnConvert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = txtText.getText().toString();
                try {
                    Socket socket = new Socket("192.168.0.110", 7575);
                    BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
                    out.println(text);
                    Toast.makeText(context, in.readLine(), Toast.LENGTH_SHORT).show();
                    in.close();
                    out.close();
                    socket.close();
                }
                catch(Exception e){
                    e.printStackTrace();
                }
            }
        });

this line : Socket socket = new Socket("192.168.0.110", 7575); seems to be the culprit.

I did some research and found the following links :

Error message 'java.net.SocketException: socket failed: EACCES (Permission denied)'

Android java.net.SocketException:socket failed: EACCES (Permission denied)

Android java.net.SocketException: socket failed: EACCES (Permission denied)

So, here is my manifest file :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.priyabrata.socketdemo" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>

</manifest>

So what is going wrong ??

2 Answers 2

1

You cannot create a Socket in an on click handler as it runs in the main thread.

Use a thread or AsyncTask to do Socket creation, reads and writes.

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

3 Comments

Out of inquisitiveness, why a socket cannot be created in the main thread ?
Google/Android forbids network operations on the main thread.
Because networking is slow and couls make the UI non-responsive
0

try to use 10.0.2.2 instead of 192.168.0.110 for your android client localhost.

maybe it wasn't the main problem but you can just try it.

Comments

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.