0

I'm trying to send commands from my phone to my computer using Sockets. I've tryed the answers here: Android and PC Socket connection

But after some digging i found out that you need to use a Async task so i tryed this: Using AsyncTask for android network connection

But for some reason my socket times out. Is there a way to find out why? because from the error i can't tell:

The error from Logcat:enter image description here

And this is the client code:

public class MainActivity extends AppCompatActivity {

    private Socket client;
    private PrintWriter printwriter;
    private EditText textField;
    private Button button;
    private String message;

    private static final int SERVERPORT = ####;
    private static final String SERVER_IP = "########";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textField = (EditText) findViewById(R.id.editText1); // reference to the text field
        button = (Button) findViewById(R.id.button1); // reference to the send button

    }

    public void onClick(View view) {
        message = textField.getText().toString();
        textField.setText(""); // Reset the text field to blank
        new AsyncAction().execute();
    }

    private class AsyncAction extends AsyncTask<String, Void, String> {
        protected String doInBackground(String... args) {
            try {
                System.out.println("background running");
                System.out.println(message);
                client = new Socket(SERVER_IP, SERVERPORT); // connect to server
                System.out.println(client.isConnected());
                System.out.println("test");
                printwriter = new PrintWriter(client.getOutputStream(), true);
                printwriter.write(message); // write the message to output stream

                printwriter.flush();
                printwriter.close();
                client.close(); // closing the connection

            } catch (UnknownHostException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

}
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
DjKillerMemeStar
  • 425
  • 5
  • 18
  • You do nothing in the catch blocks. Return a string with error message and e.getMessage(). Display that string with a Toast in onPostExecute(). Then the user of your app knows what happens. – greenapps Jan 17 '18 at 07:37

1 Answers1

0

I wrote a Server for this in Java and tested it in the emulator. I had two things to do :

  1. The server IP is "10.0.2.2" if you are using an Android Emulator. LocalHost is de Emulator Virtual Machine.
  2. Your application needs Permission Internet in manifest

Here is the server Code

/**
 *
 * @author Jean-Pierre
 */
public class SocketServer {

    private static final int portnumber = 4444;
    public static void main(String[] args) {
        SocketServer socketServer = new SocketServer();
        socketServer.run();
    }    

    /**
     * Reads a String from the client
     * Converts it to Uppercase
     * and sends it Back.
     */
    private void run() {
        try {
            ServerSocket serverSocket = new ServerSocket(portnumber);
            Socket clientSocket = serverSocket.accept();
            System.out.println("connected with :" + clientSocket.getInetAddress());
            PrintWriter out = 
                    new PrintWriter(clientSocket.getOutputStream(), true);
            InputStreamReader is = 
                    new InputStreamReader(clientSocket.getInputStream());
            BufferedReader in = 
                    new BufferedReader(is);
            while (true) {
                String line = in.readLine();
                if (line != null) {
                    System.out.println("recieved:" + line);
                    out.println(line.toUpperCase()); 
                }    
            }
        } catch (IOException ex) {
            Logger.getLogger(SocketServer.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}