Android的TCP监听器不工作在3G / 4G? [英] Android TCP Listener is not working on 3G/4G?

查看:288
本文介绍了Android的TCP监听器不工作在3G / 4G?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个应用程序的工作,我已经做了TCP听者这是工作的WiFi网络上很好,但是当我连接到3G / 4G或移动网络不work.Can一些身体帮我在这里?
我的code是这里

I am working on an app,I have made a TCP Listner which is working fine on wifi network but when i am connected to 3G/4G or mobile network it does not work.Can some body help me here? my code is here

public class ListenerService  extends Service {

Socket socket;
private ServerSocket serverSocket;
BufferedReader in = null;
static String message=null;
int portNo=1619;
boolean flag=true;
final static String MY_ACTION = "MY_ACTION";
@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    new Task().execute();

}

    private class Task extends AsyncTask <Void, String, String> {
        @Override
        protected String doInBackground(Void... params) {    
        try {
            serverSocket = new ServerSocket(portNo);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        while (flag) {
            try {

                socket = serverSocket.accept();
                in = new BufferedReader(new InputStreamReader(
                        socket.getInputStream()));
                //try {
                    StringBuilder total = new StringBuilder();
                    String line;
                    while ((line = in.readLine()) != null) {
                        total.append(line);
                    }
                    message = total.toString();
                    Log.d("NETWORK-RECEIVE", "Message!:" + message);



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

        try {

            serverSocket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return message;
        }
   }

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    Log.d("Server Startd","Listener Serverice is running");
    //Toast.makeText(getApplicationContext(),"Service Started", Toast.LENGTH_LONG).show();
    return super.onStartCommand(intent, flags, startId);
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

}

一些能告诉我什么M我在这里丢失?

can some tell me what m i missing here?

推荐答案

您正试图两台设备在连接到两个不同子网的TCP连接的。这不可能。你是pretty多问一个问题:我的朋友,为什么能听到我,当他站在我的面前,但是如果他不是在另一个房间为使这项工作的唯一方法是打开大门。但是,你必须实现这个门,这就是所谓的NAT穿越或打孔,真的是一个不同的通信概念(检查<一href=\"http://stackoverflow.com/questions/10014515/android-p2p-direct-connection-over-the-internet-behind-nat\">Android P2P(直接连接)通过互联网(NAT后面)和安卓?NAT穿越)。

You are trying to connect two devices over TCP connected to two different subnets. That's impossible. You are pretty much asking the question: "Why can my friend hear me when he's standing in front of me, but not if he's in another room?" The only way to make that work is to open the door. But you'd have to implement this "door", which is called NAT traversal or "hole punching" and really is a different communication concept (check Android P2P (direct-connection) over the Internet (behind NAT) and Android: NAT Traversal?).

在年底,有三种解决方案,您的问题:

In the end, there are three solutions to your problem:


  1. 实施自己的服务器要连接(非常乏味)的设备之间放。

  2. 使用第三方的库如 sip2peer 或其他任何你能找到适合你。

  3. 请不要打扰。有时,它只是不值得麻烦。

  1. Implement your own server to put between those devices you want to connect (very tedious).
  2. Use a 3rd-party library like sip2peer or whatever else you can find that suits you.
  3. Don't bother. Sometimes it's just not worth the trouble.

编辑:结果这听起来像你正试图连接到运行code以上的Andr​​oid设备的Web服务器。这将是选项1,不过,Android设备无法通过互联网到达。据我所知,你必须把周围:设备将自身注册服务器上,使服务器可以种其他设备连接到它


It sounds like you have a web server that is trying to connect to an Android device running the code above. That would be option 1, but still, an Android device can't be reached over the Internet. As far as I know, you have to turn that around: The device has to register itself on the server so that the server can kind of "connect" other devices to it.

这篇关于Android的TCP监听器不工作在3G / 4G?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆