Android的Wi-Fi直连:如何从集团所有者将数据发送到客户呢? [英] Android Wifi Direct: How to send data from Group Owner to the clients?

查看:184
本文介绍了Android的Wi-Fi直连:如何从集团所有者将数据发送到客户呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用Wi-Fi直连的一个问题。我设法连接2个设备,并从客户端向组所有者发送数据,因为该组的所有者IP是大家都知道的人。我也设法找出客户端的IP,并把它传递给组的所有者,但我无法从组的所有者将数据发送到客户端,即使它应该是simmetric。我使用意图 startService()发送数据和 AsynkTask 用于接收。只使用2个设备我注意到,客户端IP始终是相同的(192.168.49.10),所以我手动给它的意图。

I've got a problem using wifi direct. I managed to connect 2 devices and send data from the client to the group owner, because the group owner ip is the one that everybody knows. I managed also to find out the IP of the client and pass it to the group owner but I can't send data from the group owner to the client, even if it should be simmetric. I'm using Intent and startService() to send data and AsynkTask for receive. Using only 2 devices I noticed that the client IP is always the same (192.168.49.10), so I give it to the intent manually.

这里就是我试图创建所有者的发送者和接收者客户端的方法:

Here's the method where I'm trying to create the sender for the owner and the receiver for the client:

 @Override
    public void onConnectionInfoAvailable(final WifiP2pInfo info) {

        // InetAddress from WifiP2pInfo struct.
        InetAddress groupOwnerAddress = info.groupOwnerAddress;
        connected = true;
        ownerIP = groupOwnerAddress.getHostAddress();
        // After the group negotiation, we can determine the group owner.
        if (info.groupFormed && info.isGroupOwner) {
            Toast.makeText(MainActivity.this, "I'm the owner!!", Toast.LENGTH_SHORT).show();
            owner = true;
            // Do whatever tasks are specific to the group owner.
            // One common case is creating a server thread and accepting
            // incoming connections.
            Intent serviceIntent = new Intent(MainActivity.this, OwnerSender.class);
            serviceIntent.setAction(OwnerSender.ACTION_SEND);
            serviceIntent.putExtra(OwnerSender.EXTRAS_CLIENT_ADDRESS,"192.168.49.10");
            serviceIntent.putExtra(OwnerSender.EXTRAS_CLIENT_PORT, 8988);
            startService(serviceIntent);
            //new OwnerReceiver(this).execute(); // owner riceve dai client sulla porta 8988
        } else if (info.groupFormed) {
            // The other device acts as the client. In this case,
            // you'll want to create a client thread that connects to the group
            // owner.
            /*Intent serviceIntent = new Intent(MainActivity.this, ClientSender.class);
            serviceIntent.setAction(ClientSender.ACTION_SEND);
            serviceIntent.putExtra(ClientSender.EXTRAS_GROUP_OWNER_ADDRESS,ownerIP);
            serviceIntent.putExtra(ClientSender.EXTRAS_GROUP_OWNER_PORT, 8988);
            startService(serviceIntent);*/
            new ClientReceiver(this).execute(); // i client ricevono dall'owner sula porta 8989
            Toast.makeText(MainActivity.this, "I'm a client...", Toast.LENGTH_SHORT).show();
            Toast.makeText(MainActivity.this, "Server IP: " + groupOwnerAddress.getHostAddress(), Toast.LENGTH_SHORT).show();
        }
    }

当建立连接和所有者应启动该服务发送的数据,但永远不会启动该服务该方法开始。如何我已经说过,如果它在客户端使用,数据从客户端正确地传送到主人相同的服务启动。

This method starts when the connection is established and the owner should start the service to send the data, but the service never starts. How I already said, the same service starts if it's used on the client side and the data is transferred correctly from the client to the owner.

推荐答案

就像拉斯洛·马札儿说,你需要先发送一个空的消息服务器,使服务器可以使用客户端套接字获得进入的ip地址。也许你已经从服务器套接字的IP地址。请再次检查您的code。你应该从客户端套接字的IP地址。有没有办法,你会得到相同的IP。
那是你的问题的一部分......

Like Laszlo Magyar said, you need to sent an empty message to server first so that server can use client socket to get the incoming ip address. Perhaps you have fetched the ip from server socket. Please check your code again. You should get the ip from client socket. There is no way you will get the same IP. That is part of your problem....

其次,阐明你的逻辑,以发送数据时,发送方必须知道目标设备的IP地址。所以,你可以做的是发送一个空的消息,以便在服务器端可以获取从发送者的IP地址。没有太多的做在服务器端。只专注于客户端。

Secondly, to clarify your logic, in order to sent data, the sender MUST KNOW the ip for the targeted device. So, what you can do is sending an empty message so the server side can fetch the ip from sender. There isn't much to do on the server side. Just focus on the client side.

这篇关于Android的Wi-Fi直连:如何从集团所有者将数据发送到客户呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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