Varda Elentári
Varda Elentári

Reputation: 2322

Android application initiating socket not working

This is a simple client-server android application. The server is a simple java server running on local pc and the client is running on an emulator. here's the client's code:

package com.example.streamer;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.net.Socket;

 class Connecting implements Runnable
{
    private Socket sock;

    private BufferedReader r;

    private BufferedWriter out;
    public Connecting ()
    {
        Thread th = new Thread(this);
        th.start();

    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        try
        {
            System.out.println("************************ trying to initiated *****************");
        sock = new Socket("10.0.2.2",12344);

        System.out.println("************************ socket initiated *****************");

        r = new BufferedReader(new InputStreamReader(sock.getInputStream()));

        System.out.println("************************ buffer reader initiated *****************");

        out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));

        System.out.println("************************ buffer writer initiated *****************");

        }

        catch (IOException ioe) { }


    }

}

public class MainActivity extends Activity {

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

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





    @Override

    public void onResume() {

        super.onResume();

        System.out.println("************************ on resume *****************");
        Connecting c = new Connecting();

    }



}

anyone knows why the instruction sock = new Socket("10.0.2.2", 12344); isn't executing?

Upvotes: 1

Views: 3658

Answers (3)

Srb
Srb

Reputation: 229

add these two condition in your manifest file

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

it will work now...for any other help plz comment

Upvotes: 0

Fingolfin
Fingolfin

Reputation: 5533

The problem might be related to the fact that you haven't set the permissions for your application right.

You should edit the AndroidManifest.xml file which contains, among other things, the permissions that your application will ask for.

Edit the file like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.app.myapp" >
    <uses-permission android:name="android.permission.INTERNET" />
    ...
</manifest>

The line <uses-permission android:name="android.permission.INTERNET" /> would make the application ask for the permission to connect to the Internet.

Upvotes: 1

Gabe Sechan
Gabe Sechan

Reputation: 93542

That command tries to connect a socket to an external host. It can take a long time. On many versions of Android it will even crash, as you aren't supposed to do networking calls on the main thread. Move that into an AsyncTask or thread.

Upvotes: 1

Related Questions