title: Simple Android Socket Programming
body: <p>I am learning android socket programming. The code is given below. At the first click of the button, message is being sent to the server successfully but after the second click message is not going to the server and also it is not showing any error.</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private InetAddress address;
    private Button send;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        send = (Button) this.findViewById(R.id.Send);
        send.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        new Sender().execute("xxx.xxx.xx.xxx");
    }
    private class Sender extends AsyncTask&lt;String, Void, Void&gt; {
        private Socket socket;
        private Connector connector;
        public Sender() {}
        @Override
        protected Void doInBackground(String... params) {
            try {
                address = InetAddress.getByName(params[0]);
                socket = new Socket(address, 10001);
                connector = new Connector(socket);
                connector.write("Hello server, this is client");
            } catch (UnknownHostException e) {
                Log.d("Error", e.getLocalizedMessage());
            } catch (IOException e) {
                Log.d("Error", e.getLocalizedMessage());
            }
            return null;
        }
    }
  }
  
  
  
  
  
  public class Connector {
    private Socket socket;
    private PrintWriter writer;
    public Connector(Socket soc) {
        socket = soc;
        try {
            writer = new PrintWriter(socket.getOutputStream(), true);
        } catch (IOException e) {
            Log.d("Error", e.getLocalizedMessage());
        }
    }
    public void write(String message) {
        writer.println(message);
    }
  }
  </code></pre>