0

I'm trying to make a simple server-client connection between my phone and my computer, with my phone behaving as a client and sending a typed message to my computer's console.

While the server side is properly functioning, I can't seem to make a socket object in my phone to establish the connection. The client code was copied from my client on my eclipse, it worked perfectly on my PC.

I've searched for a solution for about half an hour but all the answers have either been outdated or too complicated for me.

connect_btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        try {
            InetAddress serverAddr = InetAddress.getByName(SERVER_IP);


            // This is where the error occurs
            socket = new Socket(serverAddr, SERVERPORT);


            Current_status.setText("Connected");

            // Sends output to the socket
            out = new DataOutputStream(socket.getOutputStream());

        }
        catch (UnknownHostException u) {
            System.out.println(u);
        }
        catch (IOException e) {
            System.out.println(e);
        }
        catch (Exception h){
            System.out.println("An error has occurred.\n");
        }
    }
});

I've left a few checkpoints in the code to try to diagnose the issue, and from what I got the problem stems from

socket = new Socket(serverAddr, SERVERPORT);

I read the documentation at https://developer.android.com/reference/java/net/Socket.html#Socket(java.lang.String,%20int) and from what I could get, the only possible error is SecurityException.

All help or clues appreciated.

H.A
  • 25
  • 7
  • "I can't seem to make a socket object in my phone to establish the connection" -- what does this mean? Do you mean that you are crashing? If so, [use Logcat to examine the stack trace associated with the crash](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this). – CommonsWare Aug 03 '19 at 21:21
  • Yes @CommonsWare, I kept crashing at first, then I decided to add the last Catch Exception, at which then I kept getting the message "An error has occurred" in my console. And since it didnt trigger any of the two exceptions and since the port number was within the port range, I deduced that the issue could be caused from SecurityException – H.A Aug 03 '19 at 21:25
  • 1
    Either get rid of that `catch` clause, or replace it with `Log.e("HA", "An error has occurred", h);`. Then, use Logcat to examine the stack trace associated with the crash. – CommonsWare Aug 03 '19 at 21:31
  • Stupid question, did you add the permission Internet to the Android Manifest? This happened to me before... – Arne Fischer Aug 03 '19 at 21:49
  • 1
    By catching the exception and printing out "An error has occurred", you have thrown away all the useful information contained in the exception. [Log](https://developer.android.com/reference/android/util/Log.html#d(java.lang.String,%20java.lang.String,%20java.lang.Throwable)) the actual exception and then edit your question with the additional information – President James K. Polk Aug 04 '19 at 03:33

0 Answers0