Search code examples
androidclientdataoutputstream

My client will connect to the server, but thats it


Android client sends string to server. Server will acknowledge a connection from device, and on the correct port but that is it..What should happen is the string is printed on the server console.

For reference, I've created the exact same client, without running it inside an android app and it works fine, so that leads me to believe I'm missing something on the android side of things. Can anyone offer a suggestion as to fix this problem. Many thanks.

Client code:

public class ObjectTestActivity extends Activity {

Button submit;
TextView tv;
private String name = "Hello Android";
private DataOutputStream dos;
private DataInputStream dis;
private final int PORT = 3000;

Button send;
InetAddress host;


@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    send = (Button) findViewById(R.id.send);
    tv = (TextView) findViewById(R.id.tv);


    try{

        host = InetAddress.getLocalHost();
        Socket socket = new Socket("xx.xx.xxx.xxx", PORT);

        dos = new DataOutputStream(socket.getOutputStream());
        dis = new DataInputStream(socket.getInputStream());

    }catch(UnknownHostException e){}
     catch(IOException e){}
}


public void onClick(View view){

    try{
        dos.writeUTF(name);
        dos.flush();
        dis.close();
        dos.close();
    }catch(IOException e){}
}

Solution

  • What is onClick attached to? Try changing to:

    public class MyActivity extends Activity {
    
      Button submit;
      TextView tv;
      private String name = "Hello Android";
      private DataOutputStream dos;
      private DataInputStream dis;
      private final int PORT = 3000;
    
      Button send;
      InetAddress host;
    
      protected void onCreate(Bundle icicle) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.main);
    
            send = (Button) findViewById(R.id.send);
            tv = (TextView) findViewById(R.id.tv);
    
    
            try{
    
                host = InetAddress.getLocalHost();
                Socket socket = new Socket("xx.xx.xxx.xxx", PORT);
    
                dos = new DataOutputStream(socket.getOutputStream());
                dis = new DataInputStream(socket.getInputStream());
    
           }catch(UnknownHostException e){}
            catch(IOException e){}
    
    
            send.setOnClickListener(new View.OnClickListener() {
                 public void onClick(View v) {
                     try{
                     dos.writeUTF(name);
                     dos.flush();
                     dis.close();
                     dos.close();
                  }catch(IOException e){}
                 }
             });
         }
     }
    

    For your button onClick event.

    To put it simply: define your buttons onClick method inside of onCreate (send.onCreate(...)).

    This example came from here