title: Android Socket (Client - Server)
body: <p>Hello dear programmers I need to your help.</p>
  
  <p>I made a very very very simple java socket (client - server) project, what worked well.
  But when I used that client code to make it for android, it is not working, but I am making only client code for android, the server code is in Java again(may be I should make the server code in android too?)</p>
  
  <p><strong><em>Server(in Java)</em></strong></p>
  
  <pre><code>public class DailyAdviceServer{  
    public void go() {
      try {
          ServerSocket serverSock = new ServerSocket(16242, 50);
          while (true)
          {
              Socket sock = serverSock.accept();
  
              PrintWriter writer = new PrintWriter(sock.getOutputStream());
              String advice = getAdvice();
              writer.println(advice);
              writer.close();
              System.out.println("smth");
          }
      } catch (IOException ex)
      {
          ex.printStackTrace();
      }
  }
  
  
  
  public static void main(String[] args)
  {
      DailyAdviceServer server = new DailyAdviceServer();
      server.go();
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong><em>Client(in Android)</em></strong></p>
  
  <pre><code>public class MainActivity extends Activity  {
  
  
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          try {
              TextView tv = (TextView) findViewById(R.id.tv);     
              Socket s = new Socket("127.0.0.1", 4242);
              InputStreamReader streamReader = new InputStreamReader(s.getInputStream()); 
              BufferedReader reader = new BufferedReader(streamReader);
  
              String advice = reader.readLine();
  
              tv.setText("Today you should: " + advice);
              reader.close();
          }
          catch (IOException ex)
          {
              ex.printStackTrace();
          }
  
  
  
  }
  </code></pre>