使用Wi-Fi Direct进行点对点数据共享

3
我正在开发一款使用Wi-Fi Direct的Android多人游戏。使用Wi-Fi直连,我能够在对等设备之间建立连接。同时,代码也能够将客户端设备的数据发送到服务器设备。(根据Android文档,Wi-Fi Direct使用客户端-服务器模型)
问题:
我无法使用Wi-Fi Direct从服务器设备向客户端设备共享数据。
我有以下问题:
1. 是否有其他方法可以在通过Wi-Fi Direct连接的两个Android设备之间传输双向数据?
2. 在我的在线研究中,我了解到要从服务器设备向客户端设备发送数据,服务器需要知道客户端的IP地址。如何使用客户端的IP地址将数据从服务器设备发送到客户端设备?(我能够获取客户端的IP地址)
对这些问题有任何建议将不胜感激。谢谢您提前的帮助。
代码: 服务器端
    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){

        }
    }

}

如果客户端没有连接到服务器,服务器就无法向客户端发送信息。服务器只能等待客户端连接。 - greenapps
但问题是什么?一旦客户端连接,数据可以在两个方向上发送。 - greenapps
提前感谢您。如果您真的欣赏意见,我希望您能做得更好。 - greenapps
@ greenapps 感谢回复。是的,我知道,一旦客户端连接到服务器,他们可以共享数据,但这适用于Wi-Fi直连吗? - Naresh
@ greenapps 在这个评论串中你如此粗鲁是没有理由的。 - trynacode
显示剩余12条评论
1个回答

7
客户端和WiFi Direct组所有者之间的通信是基于运行在组所有者上的Socket服务器,并连接到该服务器的客户端。
一旦WiFi Direct组被形成(您可以通过检查“onConnectionInfoAvailable(WifiP2pInfo wifiP2pInfo)”来了解),请检查当前设备是否为组所有者,如果是,则启动套接字服务器(类似于您的代码):
mServerSocket = new ServerSocket(mPort);
Log.e(getClass().getSimpleName(), "Running on port: " + mServerSocket.getLocalPort());

接下来,添加以下代码以接受连接并存储客户端套接字的引用:
Socket mSocket = mServerSocket.accept();

现在,您已经拥有客户端套接字的引用,可以使用它向其发送数据/消息。接下来,其他设备(客户端)应该启动与套接字服务器之间的连接:
mSocket = new Socket();
mSocket.bind(null);
mSocket.connect(new InetSocketAddress(mAddress, mPort), 500);

从服务器向客户端发送消息:
DataOutputStream mDataOutputStream = new DataOutputStream(mSocket.getOutputStream());

发送简单消息:
mDataOutputStream.writeUTF(message);
mDataOutputStream.flush();

希望这有所帮助。

请提供完整的代码。 - dipali

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接