0

I am trying to send a string from real android device(client) to pc(server) where server is running on Netbeans 8.0 and device is connected to pc over wifi(using pc wifi hotspot by connectify). Server code:

try {

            clientSocket = serverSocket.accept(); // accept the client connection
            inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
            bufferedReader = new BufferedReader(inputStreamReader); // get the client message
            message = bufferedReader.readLine();

            System.out.println(message);
            inputStreamReader.close();
            clientSocket.close();

        } catch (IOException ex) {
            System.out.println("Problem in message reading");
        }

and the client code:

try{
            client = new Socket("192.168.207.1", 4444); 
            printwriter = new PrintWriter(client.getOutputStream(), true);
            printwriter.write("SENTTTTTTTTT");
            printwriter.flush();
            printwriter.close();
            client.close(); 

    } catch (Exception e) {Toast.makeText(getApplicationContext(), "Exception-->"+e.getMessage(), Toast.LENGTH_SHORT).show();}

I have tested (Using PING app) that the IP is reachable. But when I ran app on device it gives Exception-->null. But when I tested server & client both on pc as an JAVA SE app it works fine. But it is not working on android device.

Dale Wilson
  • 9,166
  • 3
  • 34
  • 52
Subhadeep
  • 3
  • 5
  • I have been using this to send a message from Android: http://stackoverflow.com/questions/20131316/android-tcp-ip-socket-wont-send-data-to-computer/20131985#20131985 – cYrixmorten Oct 06 '14 at 17:14

1 Answers1

0

There will be an exception if a network operation is executed on the main thread in Android

Make sure you are not doing networking operations on the main thread in Android.

That is do all the same in a separate thread.

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158