Sockets, streams and services in android, how to make them work together?

I ran into a problem with streams and sockets, which I cannot understand, if someone can help me, I would really appreciate it.

There are facts :

I have a NetworkService class of service, inside this class I have a Socket attribute. I would like it to be in a state of connectivity for the entire service life cycle.

To connect the socket, I do this in the stream, so if the server needs a timeout, it does not block my UI thread.

The problem is that in the stream where I connect my socket, everything is fine, it is connected, and I can talk to my server as soon as this stream is completed, and I try to reuse the socket in another stream, I have an error Socket is not connected message.

Questions: - Is the connector automatically disconnected at the end of the stream? - In any case, can we pass the value of the called thread to the called user?

Many thanks,

Here is my code

  public class NetworkService extends Service { private Socket mSocket = new Socket(); private void _connectSocket(String addr, int port) { Runnable connect = new connectSocket(this.mSocket, addr, port); new Thread(connect).start(); } private void _authentification() { Runnable auth = new authentification(); new Thread(auth).start(); } private INetwork.Stub mBinder = new INetwork.Stub() { @Override public int doConnect(String addr, int port) throws RemoteException { _connectSocket(addr, port); _authentification(); return 0; } }; class connectSocket implements Runnable { String addrSocket; int portSocket; int TIMEOUT=5000; public connectSocket(String addr, int port) { addrSocket = addr; portSocket = port; } @Override public void run() { SocketAddress socketAddress = new InetSocketAddress(addrSocket, portSocket); try { mSocket.connect(socketAddress, TIMEOUT); PrintWriter out = new PrintWriter(mSocket.getOutputStream(), true); out.println("test42"); Log.i("connectSocket()", "Connection Succesful"); } catch (IOException e) { Log.e("connectSocket()", e.getMessage()); e.printStackTrace(); } } } class authentification implements Runnable { private String constructFirstConnectQuery() { String query = "toto"; return query; } @Override public void run() { BufferedReader in; PrintWriter out; String line = ""; try { in = new BufferedReader(new InputStreamReader(mSocket.getInputStream())); out = new PrintWriter(mSocket.getOutputStream(), true); out.println(constructFirstConnectQuery()); while (mSocket.isConnected()) { line = in.readLine(); Log.e("LINE", "[Current]- " + line); } } catch (IOException e) {e.printStackTrace();} } } 
+4
source share
1 answer

Define the output stream as a member variable, attach it to your stream and only close this stream when you're done ...

You are currently opening (and implicitly closing) the output stream in the stream. As the thread freezes, it closes this output stream, which in turn can kill a socket / connection.

If you define a stream outside the stream, you can attach it to the stream and close it later, for example, when the service is completed.

+2
source

Source: https://habr.com/ru/post/1308776/


All Articles