使用Wi-Fi Direct进行点对点数据共享 [英] Peer to peer data sharing using Wi-Fi direct

查看:105
本文介绍了使用Wi-Fi Direct进行点对点数据共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Wi-Fi direct开发一个Android多人游戏. 使用Wi-Fi Direct,我能够在对等设备之间建立连接.该代码还能够将数据从客户端设备发送到服务器设备. (根据Android文档,Wi-Fi直接使用客户端-服务器模型)

I am developing an Android multiplayer game using Wi-Fi direct. Using Wi-Fi direct, I am able to establish connection between peer devices. Also the code is able to send the data from client device to server device. (According to Android documentation, the Wi-Fi direct uses client-server model)

问题:

我无法使用Wi-Fi Direct将数据从服务器设备共享到客户端设备.

I am not able to share the data from server device to client device using Wi-Fi direct.

我有以下问题:

  1. 是否还有其他方法可以在两个之间传输数据(bi-directional) 通过Wi-Fi Direct连接的Android设备?
  2. 在进行在线研究期间,我了解到要从服务器发送数据 设备到客户端设备,服务器需要知道客户端的IP 地址.如何使用此客户的IP地址发送数据 服务器设备到客户端设备? (我可以获取客户的IP 地址)
  1. Is there any other way to transfer data (bi-directional) between two Android devices which are connected through Wi-Fi Direct?
  2. During my online research I understood that to send data from server device to client device, server needs to know the client’s IP address. How to use this client’s IP address to send data from server device to client device? (I am able to fetch the client’s IP address)

对于这些查询的任何建议,我们将不胜感激.预先谢谢你.

I'd appreciate any suggestions on these queries. Thank you in advance.

代码: 服务器端

    public  class DataTransferAsyncTask extends AsyncTask<Void,Void,String>{
    ServerSocket serverSocket;

    Socket client;
    InputStream inputstream;
    Context context = mActivity.getApplicationContext();
    String s;
    InetAddress client_add;

    @Override
    protected String doInBackground(Void... voids) {

        try{

             serverSocket = new ServerSocket(9999);
            Log.e("hello", "Server: Socket opened");
            client = serverSocket.accept();
            Log.e("hello", "Server: connection done");

            inputstream = client.getInputStream();
        //  OutputStream outputStream = serverSocket.getO

            //getting data from client
            byte[] address = new byte[12];
            if(client.isConnected())
            inputstream.read(address);

             s = new String(address);
            String only_add = new String();
            only_add = s.substring(0,12);

             client_add = InetAddress.getByName(only_add);

            Log.e("hello", "Server: clients ip 1 " + only_add);
            Log.e("hello", "Server: converted address 1 " + client_add + " \n is connected"+
                    client.isConnected());




            //send data to client

            OutputStream stream = client.getOutputStream();

             stream.write(s.getBytes());
            Log.e("hello","context value "+context);




        //  cancel(true);



        }catch (IOException e){

        }
        return null;
    }

}

客户端:

@override
protected void onHandleIntent(Intent intent) {
    Log.e("hello","client socket");
    Toast.makeText(this,"client socket",Toast.LENGTH_LONG).show();
    Context context = getApplicationContext();
    if(intent.getAction().equals(action_send_data)){
        String host = intent.getStringExtra(group_owner_address);
        Socket socket = new Socket();
        int port = intent.getIntExtra(group_owner_port,9999);


        //binding connection
        try{

            String x="hello";
            Log.e("hello","opening client socket");
            byte[] address = getLocalAddress();
            String ipAdd = getDottedDecimalIP(address);

            socket.bind(null);
            socket.connect(new InetSocketAddress(host,port),socket_timeout);

            Log.e("hello","device socket address "+ socket.getInetAddress() );



            Log.e("hello","client socket is connected"+socket.isConnected());
            Log.e("hello","device address  :"+ipAdd + "  byte "+ address);

            //sending data to server
            OutputStream stream = socket.getOutputStream();

            stream.write(ipAdd.getBytes());


            //getting data from the server(supposed to)

            InputStream inputstream = socket.getInputStream();

            byte[] address_to_sto_fr_ser = new byte[15] ;
            inputstream.read(address_to_sto_fr_ser);

            String s = new String(address_to_sto_fr_ser);
            Log.e("msg from server","msg from server "+s);



          //  stream.close();
          //  is.close();


        }catch (IOException e){

        }
    }

}

推荐答案

客户端和WiFi Direct Group Owner之间的通信基于在Group Owner上运行的Socket服务器以及连接到该服务器的客户端.

The communication between client and WiFi Direct Group Owner is based on Socket server running on the Group Owner and clients connected to that server.

WiFi Direct组一旦形成(您可以通过选中" onConnectionInfoAvailable(WifiP2pInfo wifiP2pInfo)),检查当前设备是否为组所有者,如果是,则启动套接字服务器(类似于您的代码):

Once WiFi Direct group is formed(you can know that by checking "onConnectionInfoAvailable(WifiP2pInfo wifiP2pInfo)"), check if the current device is the Group Owner and if so, start the sockets server (similar to your code):

mServerSocket = new ServerSocket(mPort);
Log.e(getClass().getSimpleName(), "Running on port: " + mServerSocket.getLocalPort());

然后,添加以下行以接受连接并存储客户端套接字的引用:

Then next, add this line to accept connections and store a reference of the client socket:

Socket mSocket = mServerSocket.accept();

现在,您有了客户端套接字的引用,可以使用它来向其发送数据/消息.接下来,另一台设备(客户端)应启动与套接字服务器的连接:

Now, you have a reference of the client socket, you can use it to send data / messages to it. Next, the other device (client) should initiate a connection to the socket server:

mSocket = new Socket();
mSocket.bind(null);
mSocket.connect(new InetSocketAddress(mAddress, mPort), 500);

要将邮件从服务器发送到客户端:

To send message from server to client:

DataOutputStream mDataOutputStream = new DataOutputStream(mSocket.getOutputStream());

发送简单消息:

mDataOutputStream.writeUTF(message);
mDataOutputStream.flush();

希望这会有所帮助.

这篇关于使用Wi-Fi Direct进行点对点数据共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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