phone-as-client Socket connections time out in 3GS mode, but work fine in wifi

by Samsyn » Mon, 15 Mar 2010 09:03:13 GMT


Sponsored Links
 I wonder if this could be a Verizon thing.  What strikes me as odd is
that the UrlConnection works fine, and that must also be using tcp, so
is fundamentally the same as my subsequent Socket connection, which
fails.

The differences are:

* different server.. .MAYBE my server doesn't like the 3gs 'ip'
address of the client...
* server port 80.. MAYBE android or verizon is making a special
exemption for what looks like a web request, while applying extra
'security' for the more generic tcp connection
* maybe I need to do something to enable the socket-over-3gs (bind to
a nic?)  That UrlCconnection is smartly doing, but I am dumbly not.






--



phone-as-client Socket connections time out in 3GS mode, but work fine in wifi

by Samsyn » Thu, 18 Mar 2010 08:13:16 GMT


 adly, no joy.  I get the same behaviour trying to connect to three
different non-port-80 servers.

Recapping

* using Socket, TCP connection, originated by phone
* Have the "INTERNET" privilege

code section:

[code]

try {
mServerSocket = new Socket( );
SocketAddress adr = new InetSocketAddress( (String)
mServerAddr, mServerPort );
mServerSocket.connect(adr, 30*1000);
} catch (UnknownHostException e) {
Log.v(TAG, "Unknown host" + mServerAddr );
mLastError = "host: " + e.getMessage();
failed = true;
} catch (IOException e) {
Log.v(TAG, "Couldn't get I/O for the connection to: " +
mServerAddr );
mLastError = "IO: " + e.getMessage();
failed = true;
}
[/code]


* Emulator connects just fine,whether wifi is on or off (though
obviously the emulator uses host internet in both cases)
* Real-Phone connects fine, in WIFI mode
* Real-Phone fails, in MOBILE (3gs) mode, on verizon.

Failure is the IOException "Socket is not connected" (and appears
after the expected timeout)

--- However, THIS works on both WIFI and MOBILE connections (initiated
by real-phone on verizon)

[code]
URL myURL = new URL( "http://..." + mUrlArgs );
// Open a connection to that URL.
URLConnection ucon = myURL.openConnection();
[/code]

even to the same machine as the server that I can't connect to, but
this is a port 80 http request.

----

I'm not sure how to get deeper logging from the phone (and it only
fails on the phone) on the off chance that some logging would actually
tell me what it was complaining about.



On Mar 15, 9:26pm, Samsyn <d...@synthetic-reality.com> wrote:

--


Sponsored Links


phone-as-client Socket connections time out in 3GS mode, but work fine in wifi

by Samsyn » Fri, 19 Mar 2010 11:11:18 GMT


 K, some success, as it were.

Let the wookiie win

--

I changed my server to listen on port 80 instead and now it works fine
in both WIFI and MOBILE modes.

* server ping is now 800ms instead of 100ms
* I can not, of course, run a web server on the same machine now,
which is a small pain.

But hey, if it works for port 80, maybe it will work for some other
ports, and maybe I just was using the One Illegal Verizon Port In the
Entire World.

ooo. 950 ms ping... I can see why people say "plays better over wifi"

Well, at least I wasn't crazy, they really WERE blocking me.

- Dan


On Mar 17, 5:13pm, Samsyn <d...@synthetic-reality.com> wrote:

--



phone-as-client Socket connections time out in 3GS mode, but work fine in wifi

by Samsyn » Sat, 20 Mar 2010 10:17:45 GMT


 n Mar 18, 8:11pm, Samsyn <d...@synthetic-reality.com> wrote:
OK OK, apparently I just picked the world's worst port number (4444)
and was SO SURE that was an innocent number that I didn't even try any
others. Well, 4444 is apparently blocked by verizon/android (looks
like Oracle and others use it for admin ports)

So I will just use a nice high number that isn't on any lists I can
find.

So, my apologies for spamming the forum, but if this helps anyone
else, avoid port 4444 :-)

I hereby mark this issue resolved.


--



phone-as-client Socket connections time out in 3GS mode, but work fine in wifi

by Carlo » Thu, 22 Apr 2010 09:51:35 GMT


 i,

Any chance you have succeeded with another port than port 80 ?

We are also receiving report from user not able to reach our server
running on port 8080 from their mobile network, is there any
(recommended) list of server port that can be use ?

thanks


On Mar 20, 11:17am, Samsyn <d...@synthetic-reality.com> wrote:

--



Other Threads

1. A new Android forum for Greek developers...

in Greek language : www.androidingreece.com! Take a look!!

-- 

2. Need help preventing ImageView from Overlapping TextView

My application was supposed to be easy way to learn how to do android
programming but this has me stuck. The images I am grabbing are mostly
different sizes so only some of the images are overlapping the
TextView. I have tried settinga Maxheight but this only works when the
screen is in vertical orientation but not when changed to horizontal.
Here is the code:
//main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
        android:id="@+id/screen"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
        >
        <android.gesture.GestureOverlayView
        android:id="@+id/gestures"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1.0"
        >
    <LinearLayout
        android:id="@+id/screen"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView
        android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="40px"
                android:text=""
                android:paddingBottom="10px"
                android:layout_weight="1.0"
        />
        <EditText
                android:id="@+id/number"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:text=""
        />
        </LinearLayout>
        <ImageView
                android:id="@+id/picview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:cropToPadding="true"
                android:layout_gravity="bottom"
        />

        </android.gesture.GestureOverlayView>


</LinearLayout>

JavaFile
package com.xkcd;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Configuration;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.method.KeyListener;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class xkcd extends Activity {
        private URL u;
        private URL imageURL;
        private InputStream is;
        private String s;
        private JSONObject json;
        private BufferedReader reader;
        private ImageView picView;
        private TextView title;
        private TextView number;
        private GestureOverlayView gestures;
        private GestureLibrary gLib;
        private int maxComic;

        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                gLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
                json = new JSONObject();
                final String TAG = "com.hascode.android.gesture";
                if (!gLib.load()) {
                        Log.w(TAG, "could not load gesture library");
                        finish();
                }
                title = (TextView) findViewById(R.id.title);
                number= (TextView) findViewById(R.id.number);
                number.setKeyListener(new KeyListener(){

                        @Override
                        public void clearMetaKeyState(View arg0, Editable arg1, 
int arg2) {
                                // TODO Auto-generated method stub

                        }

                        @Override
                        public int getInputType() {
                                // TODO Auto-generated method stub
                                return 0;
                        }

                        @Override
                        public boolean onKeyDown(View view, Editable text, int 
keyCode,
                                        KeyEvent event) {
                                // TODO Auto-generated method stub
                                return false;
                        }

                        @Override
                        public boolean onKeyOther(View view, Editable text, 
KeyEvent event)
{
                                // TODO Auto-generated method stub
                                return false;
                        }

                        @Override
                        public boolean onKeyUp(View view, Editable text, int 
keyCode,
                                        KeyEvent event) {
                                // TODO Auto-generated method stub
                                if(event.getKeyCode() == 66){
                                        
createURL(Integer.parseInt(number.getText().toString()));
                                }
                                return false;
                        }

                });
                picView = (ImageView) findViewById(R.id.picview);
                picView.setAdjustViewBounds(true);
                Display display = getWindowManager().getDefaultDisplay();
                int width = display.getWidth();
                int height = display.getHeight();
                picView.setMaxHeight(height-100);
                picView.setOnClickListener(new OnClickListener(){
                        @Override
                        public void onClick(View v) {
                                // TODO Auto-generated method stub
                                displayHover();
                        }
                });
                gestures = (GestureOverlayView) findViewById(R.id.gestures);
                gestures.addOnGesturePerformedListener(handleGestureListener);
                launch();
        }

        public void createURL(int number){
                String prefix = "http://xkcd.com/";
                String suffix = "/info.0.json";
                URL jsonURL;
                String stringURL = prefix + number + suffix;
                try {
                        jsonURL = new URL(stringURL);
                        createJSON(jsonURL);
                } catch (MalformedURLException e) {
                        e.printStackTrace();
                }
        }

        public void displayHover(){
                AlertDialog alertDialog = new 
AlertDialog.Builder(this).create();
                alertDialog.setMessage(json.get("alt").toString());
                alertDialog.setButton("OK", new 
DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog, int which) {
                          return;
                    } });
                alertDialog.show();
        }

        public void createJSON(URL url){
                try {
                        is = url.openStream();
                        reader = new BufferedReader(new InputStreamReader(is, 
"UTF-8"));
                        s = reader.readLine();
                        is.close();
                        reader.close();
                        json = (JSONObject)new JSONParser().parse(s);
                        displayComic();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }

        public void displayComic(){
                //Set the title of the app to the title of the comic and its 
number
                String comicTitle= (String)json.get("title") + " (Comic# " +
json.get("num") + ")";
                title.setText(comicTitle);
                number.setText(json.get("num").toString());
                try {
                        imageURL= new URL ((String) json.get("img"));
                        picView.setImageBitmap(getImage(imageURL));

                } catch (MalformedURLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }

        private Bitmap getImage(URL url){
        Bitmap bitmap = null;
        InputStream in = null;
        try {
            in=url.openStream();
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return bitmap;
    }

        public void launch(){
                        //By default get the most recent XKCD comic
                        try {
                                u = new URL("http://xkcd.com/info.0.json");
                        } catch (MalformedURLException e) {
                                e.printStackTrace();
                        }
                        //Using the JSON string from the URL, create a JSON 
object from 3rd
party code.
                        createJSON(u);

                        maxComic=Integer.parseInt(json.get("num").toString());
        }

        private OnGesturePerformedListener handleGestureListener = new
OnGesturePerformedListener() {
                @Override
                public void onGesturePerformed(GestureOverlayView
gestureView,Gesture gesture) {
                        ArrayList<Prediction> predictions = 
gLib.recognize(gesture);
                        // one prediction needed
                        if (predictions.size() > 0) {
                                Prediction prediction = predictions.get(0);
                                // checking prediction
                                if (prediction.score > 1.0) {
                                        String action= prediction.name;
                                        ComicThread newComic;
                                        if (action.equals("Left")){
                                                
if(Integer.parseInt(json.get("num").toString())==1){
                                                        newComic= new 
ComicThread(maxComic);
                                                        newComic.start();
                                                }else
                                                        newComic= new
ComicThread((Integer.parseInt(json.get("num").toString())-1));
                                                        newComic.start();
                                        }
                                        if (action.equals("Right")){
                                                
if(Integer.parseInt(json.get("num").toString())==maxComic){
                                                        newComic= new 
ComicThread(1);
                                                        newComic.start();
                                                }else
                                                        newComic= new
ComicThread((Integer.parseInt(json.get("num").toString())+1));
                                                        newComic.start();
                                        }
                                }
                        }
                }
        };


        private class ComicThread extends Thread{
                int number;
                ProgressDialog dialog;

                public ComicThread(int number){
                        this.number=number;
                        dialog= ProgressDialog.show(xkcd.this, "","Loading 
Comic #" +number
+ ". Please wait...", true);
                }

                public void run(){
                        handler.sendEmptyMessage(0);
                }

                private Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                createURL(number);
                dialog.dismiss();
            }
        };
        }


}

-- 

3. call web service every 50 seconds

4. javax.net.ssl.SSLException: Not trusted server certificate

5. ARTIS Package

6. Phone call action in the webView

7. bitmap size problem