简体   繁体   中英

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". 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. The main Goal is to connect to this Wifi and open a socket and send/ receive TPC/UPD frames in android M.

I wrote an android activity which search for all available Wifis and connects to it with a button which i can connect successfully.

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

port: 50000

gateway: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. Unfortunately I have a very limited knowledge on networking.

It seems that the wireless router only accepts the static IPs. Using a static IP and gateway solved the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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