title: Java Sockets - transmission in real time
body: <p>i got this code form Android-er blogspot, big thanks for him to make me almost understand basic socket connections in java. So i got this client app on my android device, and computer with server running, but how could i make a loop in a client code, to make it send data from EditText in real time? (whenever it changes) Please if someone could clear it out for a complete newbie?</p>
  
  <p>-----This is client code (Android-er Copyrights):</p>
  
  <pre><code>package com.exercise.AndroidClient;
  
  import java.io.DataInputStream;
  import java.io.DataOutputStream;
  import java.io.IOException;
  import java.net.Socket;
  import java.net.UnknownHostException;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  
  public class AndroidClient extends Activity {
  
  EditText textOut;
  TextView textIn;
  
   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) { 
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
  
   textOut = (EditText)findViewById(R.id.textout);
   Button buttonSend = (Button)findViewById(R.id.send);
   textIn = (TextView)findViewById(R.id.textin);
   buttonSend.setOnClickListener(buttonSendOnClickListener);
   }
  
   Button.OnClickListener buttonSendOnClickListener
   = new Button.OnClickListener(){
  
  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   Socket socket = null;
   DataOutputStream dataOutputStream = null;
   DataInputStream dataInputStream = null;
  
   try {
    socket = new Socket("192.168.1.101", 8888);
    dataOutputStream = new DataOutputStream(socket.getOutputStream());
    dataInputStream = new DataInputStream(socket.getInputStream());
    dataOutputStream.writeUTF(textOut.getText().toString());
    textIn.setText(dataInputStream.readUTF());
   } catch (UnknownHostException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   finally{
    if (socket != null){
     try {
      socket.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
  
    if (dataOutputStream != null){
     try {
      dataOutputStream.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
  
    if (dataInputStream != null){
     try {
      dataInputStream.close();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
   }
  }};
  }
  </code></pre>
  
  <p>-----This is server code (Android-er Copyrights):</p>
  
  <pre><code>import java.io.DataInputStream;
  import java.io.DataOutputStream;
  import java.io.IOException;
  import java.net.ServerSocket;
  import java.net.Socket;
  
  
  public class MyServer {
  
   public static void main(String[] args){
    ServerSocket serverSocket = null;
    Socket socket = null;
    DataInputStream dataInputStream = null;
    DataOutputStream dataOutputStream = null;
  
    try {
     serverSocket = new ServerSocket(8888);
     System.out.println("Listening :8888");
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
  
    while(true){
     try {
      socket = serverSocket.accept();
      dataInputStream = new DataInputStream(socket.getInputStream());
      dataOutputStream = new DataOutputStream(socket.getOutputStream());
      System.out.println("ip: " + socket.getInetAddress());
      System.out.println("message: " + dataInputStream.readUTF());
      dataOutputStream.writeUTF("Hello!");
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
     finally{
      if( socket!= null){
       try {
        socket.close();
       } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
      }
  
      if( dataInputStream!= null){
       try {
        dataInputStream.close();
       } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
      }
  
      if( dataOutputStream!= null){
       try {
        dataOutputStream.close();
       } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }
      }
     }
    }
   }
  }
  </code></pre>