连接插座使用两部手机 [英] Connecting two phones using socket

查看:141
本文介绍了连接插座使用两部手机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能使用套接字编程连接两部手机? 我想下面的code,但没有奏效。

 保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    serverStatus =(TextView中)findViewById(R.id.server_status);

    SERVERIP = getLocalIpAddress(); //公共函数来获得IP地址,它是//工作正常

    螺纹FST =新主题(新ServerThread());
    fst.start();
}

公共类ServerThread实现Runnable {

    公共无效的run(){
        尝试 {
            如果(SERVERIP!= NULL){
                handler.post(新的Runnable(){
                    公共无效的run(){
                        serverStatus.setText(听的IP:+ SERVERIP);
                    }
                });
                ServerSocket的=新的ServerSocket(8087);
                   } 其他 {
                handler.post(新的Runnable(){
                    公共无效的run(){
                        serverStatus.setText(无法​​检测到的网络连接。);
                    }
                });
            }
        }赶上(例外五){
            handler.post(新的Runnable(){
                公共无效的run(){
                    serverStatus.setText(错误);
                }
            });
            e.printStackTrace();
        }
    }
}
 

解决方案

是的,这是可能的,但我认为你应该做的第一件事就是阅读上的的Java Socket编程因为有一些问题,你的code,它让我觉得你没有完全得到它的把握呢。存在的主要问题有:

  1. 的ServerSocket 从来不接受一个连接,因此从来没有真正听。
  2. 即使在听,如果这$ C $,C是这两款手机上运行他们两个只是听,而不是积极寻求与各-其他的连接。

您需要实现在一个手机客户端,并在其他类似@Deepak服务器显示。

另外,您可能想看看的AsyncTask 中的这篇文章更新从一个非UI线程(而不是处理)。意见

最后,确保你的应用程序,包括 android.permission.INTERNET对许可的Andr​​oidManifest.xml

Is it possible to connect two phones using socket programming? I tried following code but it didn't work

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    serverStatus = (TextView) findViewById(R.id.server_status);

    SERVERIP = getLocalIpAddress();//Public function to get ip address to it is //working fine 

    Thread fst = new Thread(new ServerThread());
    fst.start();
}

public class ServerThread implements Runnable {

    public void run() {
        try {
            if (SERVERIP != null) {
                handler.post(new Runnable() {
                    public void run() {
                        serverStatus.setText("Listening on IP: " + SERVERIP);
                    }
                });
                serverSocket = new ServerSocket(8087);
                   } else {
                handler.post(new Runnable() {
                    public void run() {
                        serverStatus.setText("Couldn't detect internet connection.");
                    }
                });
            }
        } catch (Exception e) {
            handler.post(new Runnable() {
                public void run() {
                    serverStatus.setText("Error");
                }
            });
            e.printStackTrace();
        }
    }
}

解决方案

Yes, it is possible but I think the first thing you should do is read up on Java Socket programming as there are a few problems with your code that make me think you haven't quite got a grasp of it yet. The main problems are:

  1. Your ServerSocket never accepts a connection and is therefore never actually 'listening'.
  2. Even if it was listening, if this code is running on both phones they would both only be listening and not actively seeking a connection with each-other.

You will need to implement a client on one phone and a server on the other like @Deepak has shown.

Also, you may want to check out AsyncTask in this article for updating views from a non-UI thread (instead of a handler).

Finally, make sure your app includes the android.permission.INTERNET permission in AndroidManifest.xml.

这篇关于连接插座使用两部手机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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