0

My app connects to a hotspot and establishes a socket connection. On reading from the socket, SocketException is thrown with the following message "Software caused connection abort".

Socket socket = null;
            try {
                socket = new Socket(SERVER_IP, SERVER_PORT);
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
                        1024);
                byte[] buffer = new byte[1024];

                int bytesRead;
                InputStream inputStream = socket.getInputStream();
                /*
                 * notice: inputStream.read() will block if no data return
                 */
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    byteArrayOutputStream.write(buffer, 0, bytesRead);
                    response += byteArrayOutputStream.toString("UTF-8");
                }
            } catch (UnknownHostException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                response = "UnknownHostException: " + e.toString();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                response = "IOException: " + e.toString();
            } finally {
                if (socket != null) {
                    try {
                        socket.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
tbfp
  • 147
  • 14
  • Possible duplicate of [Official reasons for "Software caused connection abort: socket write error"](https://stackoverflow.com/questions/2126607/official-reasons-for-software-caused-connection-abort-socket-write-error) – denvercoder9 May 15 '18 at 12:39
  • `SocketException is thrown ` Why arent you catching it and inspect stack trace and message? – greenapps May 15 '18 at 13:04

1 Answers1

0

This usually means that there was a network error, such as a TCP timeout. I would start by placing a sniffer (wireshark) on the connection to see if you can see any problems. If there is a TCP error, you should be able to see it. Also, you can check your router logs, if this is applicable. If wireless is involved anywhere, that is another source for these kind of errors

Otherwise, you can Download TCP Test Tool for windows and try to test with your IP is it working or not.

I hope this will help you.

Nitin Karande
  • 1,280
  • 14
  • 33