简体   繁体   English

在android中使用Wifi连接打开套接字

[英]Opening a socket using Wifi connection in android

I have a PCan wireless device that can play the role as which they called "Micro Access Point". 我有一台PCan无线设备可以扮演他们称之为“Micro Access Point”的角色。 The concept is to make it router and other devices connect to it. 这个概念是让路由器和其他设备连接到它。 With this connection I can send/receive CAN frames using TCP or UDP. 通过此连接,我可以使用TCP或UDP发送/接收CAN帧。 The main Goal is to connect to this Wifi and open a socket and send/ receive TPC/UPD frames in android M. 主要目标是连接到这个Wifi并打开一个套接字并在android M中发送/接收TPC / UPD帧。

I wrote an android activity which search for all available Wifis and connects to it with a button which i can connect successfully. 我写了一个android活动,搜索所有可用的Wifis并用一个按钮连接到它,我可以成功连接。

After connection I tried to open a socket but it failed. 连接后我试图打开一个套接字,但它失败了。 I tried it also with different timeouts: 我也尝试了不同的超时:

button2.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        new Thread(new Runnable() {
            @Override
            public void run() {

                for (int i = 0; i <= 255; ++i) { // I wrote this for loop to check all possibilities!
                    String ip = "192.168.1." + i;
                    try {
                        Log.i(TAG, "Try ip: " + ip);
                        Socket socket = new Socket();
                        socket.connect(new InetSocketAddress(ip, 50000), 100);
                        Log.e(TAG, "Connected!");
                    } catch (IOException e) {
                        Log.e(TAG, "Exception is catched!");
                    }
                }

            }
        }).start();
    }
});

Can somebody tell me how to open a socket? 有人能告诉我如何打开插座吗? Here are the infos on the device: 以下是设备上的信息:

ip: 192.168.1.10 ip:192.168.1.10

port: 50000 港口:50000

gateway:192.168.1.199 网关:192.168.1.199

UPDATE: 更新:

I don't know if I am on the right way to overcome this problem. 我不知道我是否正在以正确的方式克服这个问题。 Very short to say: I want to connect to this wireless device and send/receive tcp and UPD frames. 很简单:我想连接到这个无线设备并发送/接收tcp和UPD帧。 Unfortunately I have a very limited knowledge on networking. 不幸的是,我对网络知识非常有限。

It seems that the wireless router only accepts the static IPs. 似乎无线路由器只接受静态IP。 Using a static IP and gateway solved the problem. 使用静态IP和网关解决了这个问题。

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

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