简体   繁体   English

ShareIt如何在PC上工作?

[英]How ShareIt works for PC?

Well,I am trying to build a software like ShareIt but it seems like I am struck in between. 好吧,我正在尝试构建诸如ShareIt之类的软件,但似乎介于两者之间。 What I had done till now- I had made a program(server and client side) where a server can send any kind of file to the client,and for this the client must have the IP address of the server. 到目前为止,我所做的是-我制作了一个程序(服务器和客户端),服务器可以在其中向客户端发送任何类型的文件,为此,客户端必须具有服务器的IP地址。 But I would like to know- How the client can automatically search the IP address of the server which is listening? 但是我想知道-客户端如何自动搜索正在监听的服务器的IP地址? I don't want to manually enter the IP address each time,instead how the client automatically detect all the IP addresses of the servers which are available and are listening? 我不想每次都手动输入IP地址,而是客户端如何自动检测所有可用并正在监听的服务器的IP地址? Thanks in advance. 提前致谢。

Once client is connected to an Access Point created, you can get Access Point IP address as below. 将客户端连接到创建的接入点后,即可获得如下的接入点IP地址。

public static String getAccessPointIpAddress(Context context) {
    WifiManager wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);
    DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
    byte[] ipAddress = convert2Bytes(dhcpInfo.serverAddress);
    try {
        String ip = InetAddress.getByAddress(ipAddress).getHostAddress();
        return ip.replace("/", "");
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    return null;
}

private static byte[] convert2Bytes(int hostAddress) {
    byte[] addressBytes = {(byte) (0xff & hostAddress),
            (byte) (0xff & (hostAddress >> 8)),
            (byte) (0xff & (hostAddress >> 16)),
            (byte) (0xff & (hostAddress >> 24))};
    return addressBytes;
}

Sender/Receiver whoever creates an Access point starts the ServerSocket on its IP address as Hostname. 发送方/接收方创建访问点的人将以其IP地址作为主机名启动ServerSocket。 But the client needs a PORT number a well, you can either hardcode it both sides or encode SSID to embed port number on it. 但是客户端很好地需要一个PORT端口号,您既可以在两侧对其进行硬编码,也可以对SSID进行编码以在其上嵌入端口号。

Also I've made an attempt to write a library called SHAREthem to help with File Sharing & transfer or in other words it simulates how SHAREit works. 另外,我还尝试编写一个名为SHAREthem的库来帮助进行文件共享和传输,或者换句话说,它可以模拟SHAREit的工作方式。 Also this blog explains implementation details Hope it helps. 此外, 博客还介绍了实现细节,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM