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 ??

Community
  • 1
  • 1
Priyabrata
  • 1,202
  • 3
  • 19
  • 57

2 Answers2

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.

greenapps
  • 11,154
  • 2
  • 16
  • 19
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.

MHSaffari
  • 858
  • 1
  • 16
  • 39