Communication with the Android client and server using a service or IntentService

So far, I managed to start the server on one Android device (wifi tethering / hotspot) and allow the client (another android) to connect and send messages to the server. Then the server responded to this. I understand that I need a way so that the server does not listen to clients, even if the chat application does not work. Clients should be able to send messages, and the server should receive this. Should I use Service or IntentService to archive this? I can not spread from AsyncTask and Service ... how to implement this? Some sample code would be great.

This is what my server looks like:

public class Server extends AsyncTask<Integer, Void, Socket> { private ServerSocket serverSocket; private TextView textView; private String incomingMsg; private String outgoingMsg; public Server(TextView textView) { this.textView = textView; } public void closeServer() { try { serverSocket.close(); } catch (IOException e) { Log.d("Server", "Closung the server caused a problem"); e.printStackTrace(); } } @Override protected Socket doInBackground(Integer... params) { try { serverSocket = new ServerSocket(params[0]); //accept connections Socket socket = serverSocket.accept(); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); incomingMsg = in.readLine() + System.getProperty("line.separator"); //send a message outgoingMsg = "You are connected to the Server" + System.getProperty("line.separator"); out.write(outgoingMsg); out.flush(); return socket; } catch (InterruptedIOException e) { //if timeout occurs e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // finally { // if (serverSocket != null) { // try { // serverSocket.close(); // } catch (IOException e) { // e.printStackTrace(); // } // } // } return null; } protected void onPostExecute(Socket socket) { if(socket != null) { try { Log.i("Server", "Server received: " + incomingMsg); textView.setText("Server received: " + incomingMsg + "\n"); textView.append("Server sent: " + outgoingMsg + "\n"); Log.i("Server", "Server sent: " + outgoingMsg); socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { Log.d("Server", "Can't communicate with the client!"); } } } 

And this is my client:

 public class Client extends AsyncTask<Integer, Void, Socket> { private WifiManager wifi; private Context context; private String outMsg; private String inMsg; public Client(Context context, WifiManager wifiManager) { this.context = context; this.wifi = wifiManager; } @Override protected Socket doInBackground(Integer... params) { try { String gateway = intToIp(wifi.getDhcpInfo().gateway); Socket socket = new Socket(gateway, params[0]); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); String ipAdress = intToIp(wifi.getConnectionInfo().getIpAddress()); outMsg = ", Client " + ipAdress +" is connecting!" + System.getProperty("line.separator"); out.write(outMsg); out.flush(); //accept server response inMsg = in.readLine() + System.getProperty("line.separator"); return socket; } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } public String intToIp(int addr) { return ((addr & 0xFF) + "." + ((addr >>>= 8) & 0xFF) + "." + ((addr >>>= 8) & 0xFF) + "." + ((addr >>>= 8) & 0xFF)); } protected void onPostExecute(Socket socket) { if(socket != null) { Log.i("Client", "Client sent: " + outMsg); Toast.makeText(context, "\nClient sent: " + outMsg + "\n", Toast.LENGTH_LONG).show(); Log.i("Client", "Client received: " + inMsg); Toast.makeText(context, "Client received: " + inMsg + "\n", Toast.LENGTH_LONG).show(); } else { Log.d("Client", "Can't connect to server!"); Toast.makeText(context, "Can't connect to server!", Toast.LENGTH_LONG).show(); } } } 

How to make a service from a server? Should the Client be a Service?

+4
source share
1 answer

Use the service, but forget about AsyncTask. Ask your service to launch your common themes ( https://developer.android.com/reference/java/lang/Thread.html ) and configure your sockets + listeners. This service can even handle sending messages (through one of the many options that are listed in the link below).

Remember to properly flush threads in the onDestroy () service.

Also note that if you want the application to continue to receive messages from other clients, you need to make sure that the service is forced to the forefront (see http://developer.android.com/reference/android/app/Service. html # startForeground (int , android.app.Notification). However, this is not reliable, your service may still be killed.

That is why people, as a rule, use servers located on some dedicated field, unlike each individual device ...

+3
source

All Articles