在两个WiFi设备的数据传输 [英] Data transfer between two Wifi Devices

查看:263
本文介绍了在两个WiFi设备的数据传输的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索在谷歌。我尝试了很多。在Android 2.2及SDK 8我如何使用SSID在Android的一个列表?通过使用SSID应该以编程方式获取特定的WiFi功能的设备属性。与帮助下,应转移的android 2 WiFi功能的设备之间的DATAS。谁能帮我在这嘿?

I have searched in Google. I tried a lot. In Android 2.2 and sdk 8 how can i use SSID in a List in Android ? By using SSID Should get specific wifi enabled device properties by programmatically. With that help, should transfer the datas between two Wifi enabled devices in android. Can anyone help me in this plz?

推荐答案

要在两个Android设备之间发送的数据以有意义的方式,你会使用一个TCP连接。要做到这一点,你需要的IP地址,并在其上​​其他设备监听的端口。

To send data in a meaningful manner between two Android devices you would use a TCP connection. To do that you need the ip address and the port on which the other device is listening.

例子是从<一个取href="http://www.helloandroid.com/tutorials/simple-connection-example-part-ii-tcp-communication">here.

有关服务器端(监听端),你需要一个服务器socket:

For the server side (listening side) you need a server socket:

try {
        Boolean end = false;
        ServerSocket ss = new ServerSocket(12345);
        while(!end){
                //Server is waiting for client here, if needed
                Socket s = ss.accept();
                BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
                PrintWriter output = new PrintWriter(s.getOutputStream(),true); //Autoflush
                String st = input.readLine();
                Log.d("Tcp Example", "From client: "+st);
                output.println("Good bye and thanks for all the fish :)");
                s.close();
                if ( STOPPING conditions){ end = true; }
        }
ss.close();


} catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
} catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
}

有关客户端,你需要一个套接字连接到服务器套接字。

For the client side you need a socket that connects to the server socket. Please replace "localhost" with the remote Android devices ip-address or hostname:

try {
        Socket s = new Socket("localhost",12345);

        //outgoing stream redirect to socket
        OutputStream out = s.getOutputStream();

        PrintWriter output = new PrintWriter(out);
        output.println("Hello Android!");
        BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));

        //read line(s)
        String st = input.readLine();
        //. . .
        //Close connection
        s.close();


} catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
} catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
}

这篇关于在两个WiFi设备的数据传输的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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