0

I did a TCP client on Java and works fine, but when I import the class to my Android project, it doesn't work.

Android code:

// ...
@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 Button button = (Button) findViewById(R.id.SEND);
 ssid = (EditText) findViewById(R.id.textSsid);
 pass = (EditText) findViewById(R.id.textPass);
 debugText = (EditText) findViewById(R.id.debugText);
 cliente = new ClienteTCP();
 debugText.setText("Version 1-prealpha");
 button.setOnClickListener(new View.OnClickListener() {
 public void onClick(View v) {
 // Do something in response to button click
 demo(v);
 }
 });
}
public void demo(View view) {
 cliente.configure(ssid.getText(), pass.getText());
 CharSequence cs = cliente.sendMessage("Hola Mundo");
 if ( cs != null)
 debugText.setText(cs);
 else
 debugText.setText("ERROR OCURRED");
}
// ...

I always get the "ERROR OCURRED" message, I try to use another IPs, but all of them works fine on Java and not on android.

The class code:

private CharSequence SSID, pass;
private String HOST = "52.28.45.92"; // My external server i tried with localhot server too ...
// of course i have a server aplication running hahaha
private int PORT = 5000;
private Socket s;
private DataOutputStream oms;
// ...
public CharSequence sendMessage(String ms) {
 DataInputStream ims;
 CharSequence data = null;
 try {
 s = new Socket(HOST,PORT);
 oms = new DataOutputStream(s.getOutputStream());
 ims = new DataInputStream(s.getInputStream());
 oms.writeUTF(ms+getFull());
 data = ims.readUTF();
 oms.close();
 ims.close();
 s.close();
 } catch (IOException e) {
 e.printStackTrace();
 }
 return data;
}

I think it can be due to my emulator, any ideas? is the code wrong?

EDIT: Server code:

# ...
# Wrote on Python:
while 1:
 data = conn.recv(BUFFER_SIZE)
 if not data: break
 print "received data:", data
 conn.send(data+'\r\n')
conn.close()
asked Apr 23, 2015 at 19:56
11
  • Two things: 1. You should run public void demo(View view) on different thread than Main. 2 Can you post log error? Commented Apr 23, 2015 at 20:01
  • There is no error, just cs is always null Commented Apr 23, 2015 at 20:04
  • The DataInputStream readUTF() method is not the same as readLine(), here are more details the data that your Python program sends can't be read using the readUTF() method. Commented Apr 23, 2015 at 20:05
  • And why it's read on my Java project: stackoverflow.com/questions/29829036/… what's the diference? Commented Apr 23, 2015 at 20:08
  • You return data==null and then you say you have ERROR_OCCURRED. But you will have had a catch: catch (IOException e). So you are already printing the stack trace but you could also log e.getMessage() to see the reason of the exception. Or return e.getMessage() there to see it directly. Commented Apr 23, 2015 at 20:08

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.