无法在不同位置与不同服务提供商的两个不同Android设备之间建立套接字连接

时间:2014-08-14 08:43:20

标签: java android sockets

我正在尝试在不同位置的两个不同设备之间建立套接字连接。我在我的Android设备上使用Airtel SIM卡。我在设备上运行以下代码:

public class MainActivity extends Activity
{
    Context context;
    TextView info, infoip, msg;
    String message = "";
    ServerSocket serverSocket;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        Sql s = new Sql(context);
        info = (TextView) findViewById(R.id.info);
        infoip = (TextView) findViewById(R.id.infoip);
        msg = (TextView) findViewById(R.id.msg);
        new GetPublicIPTask().execute();
    }
    private class SocketServerThread extends Thread
    {

        static final int SocketServerPORT = 8080;
        int count = 0;

        @Override
        public void run()
        {
            try
            {               
                serverSocket = new ServerSocket(SocketServerPORT);
                MainActivity.this.runOnUiThread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        info.setText("Port: " + serverSocket.getLocalPort());
                    }
                });

                while (true)
                {
                    Socket socket = null;
                    try
                    {
                        socket = serverSocket.accept();
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                    count++;
                    message += "#" + count + " from " + socket.getInetAddress() + ":" + socket.getPort() + "\n";

                    MainActivity.this.runOnUiThread(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            msg.setText(message);
                        }
                    });

                    SocketServerReplyThread socketServerReplyThread = new SocketServerReplyThread(socket, count);
                    socketServerReplyThread.run();

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

    }

    private class SocketServerReplyThread extends Thread
    {

        private Socket hostThreadSocket;
        int cnt;

        SocketServerReplyThread(Socket socket, int c)
        {
            hostThreadSocket = socket;
            cnt = c;
        }

        @Override
        public void run()
        {
            OutputStream outputStream;
            String msgReply = "Hello client " + cnt;

            try
            {
                outputStream = hostThreadSocket.getOutputStream();
                PrintStream printStream = new PrintStream(outputStream);
                printStream.print(msgReply);
                printStream.close();

                message += "replied: " + msgReply + "\n";

                MainActivity.this.runOnUiThread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        msg.setText(message);
                    }
                });
            }
            catch (IOException e)
            {
                e.printStackTrace();
                message += "Exception! " + e.toString() + "\n";
            }

            MainActivity.this.runOnUiThread(new Runnable()
            {
                @Override
                public void run()
                {
                    msg.setText(message);
                }
            });
        }
    }

    public class GetPublicIPTask extends AsyncTask<String, Integer, String>
    {
        ProgressDialog progressDialog;
        String serverResponse = "";

        public GetPublicIPTask()
        {
            progressDialog = new ProgressDialog(context);
        }

        @Override
        protected void onPreExecute()
        {
            if (!NetWorkInfo.isOnline(context))
            {
                Toast.makeText(context, "No Internet Connection", Toast.LENGTH_LONG).show();
                return;
            }
            progressDialog.setMessage("Getting IP");
            progressDialog.setCancelable(false);
            progressDialog.show();
        }

        @Override
        protected String doInBackground(String... sUrl)
        {
            BufferedReader in = null;
            int TIME_OUT = 1000 * 60 * 10;

            HttpClient httpclient = new DefaultHttpClient();
            HttpParams params = httpclient.getParams();
            HttpConnectionParams.setConnectionTimeout(params, TIME_OUT);
            HttpConnectionParams.setSoTimeout(params, TIME_OUT);

            HttpGet httppost = new HttpGet("http://checkip.dyndns.org");
            httppost.setHeader("Content-Type", "application/json");
            try
            {
                HttpResponse response = httpclient.execute(httppost);
                in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
                StringBuffer sb = new StringBuffer("");
                String line = "";
                while ((line = in.readLine()) != null)
                {
                    sb.append(line);
                }
                serverResponse = sb.toString();
                return serverResponse;
            }
            catch (Exception ex)
            {
                Log.d("Socket Server", "StackTrace : " + ex.getStackTrace().toString());
            }
            finally
            {
                try
                {
                    if (in != null)
                    {
                        in.close();
                    }
                }
                catch (IOException e)
                {
                    throw new RuntimeException(e);
                }
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result)
        {
            if (progressDialog != null && progressDialog.isShowing())
            {
                progressDialog.dismiss();
            }
            infoip.setText(serverResponse);
            Thread socketServerThread = new Thread(new SocketServerThread());
            socketServerThread.start();
            super.onPostExecute(result);
        }
    }
}

从终端我正在点击移动应用程序: telnet publicIpAddress 8080 相同的过程在本地网络中工作,但不在移动网络中工作。

0 个答案:

没有答案