3

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.

mohsen_og
  • 774
  • 1
  • 9
  • 31
  • Is this a socket question or a "how to connect to a wifi network" question? Either way, there seems to be a lot of irrelevant information. If you're not on a network, how to make a socket doesn't matter. And if you ARE on a network, whether it's a wifi network doesn't matter when making a socket. – xaxxon Oct 20 '16 at 07:18
  • @xaxxon I am C++ guy with no networking experience. So maybe the question is ambiguous. The Question is how can I send/receive data on this device? Am I on the right road? – mohsen_og Oct 20 '16 at 07:35
  • Probably? But you've really put two questions in here. One is "how to I connect to a wifi network" and another is "how do I make a socket". Please limit the post to just one question (you can make another post if needed) – xaxxon Oct 20 '16 at 07:38
  • I don't know PCan wireless device do but TCP/IP connection is establish by Three-Way Handshake procedure. What you need to check 1. your android device and PCan wireless device are on same network. 2. PCan wireless device is act as tcp/ip server 3.PCan wireless device at which port its listening for incoming connection requests. NOTE: PCan wireless device can be accessed over internet then first step is not needed only your device then should have internet access. –  Oct 20 '16 at 08:36
  • if you want to connect your device some specific WiFi network programmatically then you should check WifiManager and these methods especially 1. addNetwork 2. startScan 3. getScanResults –  Oct 20 '16 at 08:46
  • If your credentials are right and you are on the same network, use `socket.getInputStream()` to monitor the incoming traffic, and use `socket.getOutputStream()` to write data into the socket (eg. send data to the other client). I recommend to increase your timeout period as 100ms seems a bit too short. Sockets are used for TCP, if you want UDP protocol use DatagramSockets. – Endre Börcsök Oct 20 '16 at 09:06
  • @YasirAli To answer your questions: 1. I don't know what you mean they are on the same network. I connect to pcan and it plays the AP role. and there is no internet. 2. Yes, the device is acting as server and 3. port 50000 for listening (also I can define it as well) and handshake is off. Stupid question: i connects to a wifi. should i always open a socket for sending data with TCP protocol? – mohsen_og Oct 20 '16 at 09:17
  • @EndreBörcsök Handshake by AP is off. I connect to the AP (wireless device that acts as server) programmatically. Try to open a socket which is unsuccessfull. answer yours: 1. What do you mean on the same network? they should be isnt it? 2. I cannot connect a socket so how can i use `socket.getInputStream()` – mohsen_og Oct 20 '16 at 09:23
  • Same network mean they should have same network mass –  Oct 20 '16 at 10:04
  • @YasirAli I believe they have the same network. – mohsen_og Oct 20 '16 at 10:12

1 Answers1

1

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

mohsen_og
  • 774
  • 1
  • 9
  • 31