0

I am developing an Android app where I need to have a TCP socket open. This is done using a ServerSocket that receives a connection attempt and creates a Socket object. This object is stored in a Socket? variable.

It is expected for the other end of the socket to lose connection. When I try to send the next message I get

E/flutter (29620): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] 
Unhandled Exception: SocketException: Broken pipe (OS Error: Broken pipe, errno = 32), 
address = 0.0.0.0, port = 9000

which is expected. For this reason I put the socket.add call in a try-catch block like so:

if (socket != null) {
  try {
    socket?.add(msg);
  } on SocketException {
    if (kDebugMode) {
      print("Connection closed");
    }
    socket = null;
    /* ... */
  }
} else {
  if (kDebugMode) {
    print("Socket disconnected");
  }
}

But this doesn't print "Connection closed" and results in the error above.

But, if I change the socket?.add(msg); line into throw const SocketException("test");, "Connection closed" is printed as expected. I also tried changing the on SocketException to on Exception or catch(_) but the exception coming from the send is always uncaught.

What am I missing or doing wrong?

2

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.