klauscj2
03/05/14, 01:50:18
Hola amigos estoy desarrollando una aplicacion basica para hacer un chat entre un programa desarrollado en java y un app en android que se comunicaran mediante socket's
el problema esq no puedo conectarme desde el cliente, es decir mi app llega solamente hasta antes de establecer:
servidor = new Socket(IP, PORT);
esta linea es la que no ejecuta es como si se saltara la instruccion, mi app esta desarrollada en froyo y no entiendo el problema
aqui el codigo
package com.klauscj2.led2;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Led2 extends Activity {
private EditText txtIp, txtPuerto, txtMensaje, txtArea;
private Button bConectar, bEnviar;
Socket servidor;
private boolean bandera = false;
DataInputStream dis = null;
DataOutputStream dos = null;
String msg = "";
int conta = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_led2);
txtIp = (EditText) findViewById(R.id.editText1);
txtPuerto = (EditText) findViewById(R.id.editText2);
txtMensaje = (EditText) findViewById(R.id.editText4);
txtArea = (EditText) findViewById(R.id.editText3);
bConectar = (Button) findViewById(R.id.button1);
bEnviar = (Button) findViewById(R.id.button2);
bEnviar.setEnabled(false);
txtMensaje.setEnabled(false);
}
public void clicBoton(View view) {
// Toast.makeText(Led2.this, "Hola",Toast.LENGTH_SHORT).show();
boolean flag = Conectar();
conta = 0;
if (flag) {
iniciarHandler();
Toast.makeText(Led2.this,
"Conectado a " + servidor.getRemoteSocketAddress(),
Toast.LENGTH_SHORT).show();
bEnviar.setEnabled(true);
txtMensaje.setEnabled(true);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(txtMensaje.getWindowTo ken(), 0);
} else {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(txtMensaje.getWindowTo ken(), 0);
Toast.makeText(Led2.this, "No se ha podido conectar al servidor",
Toast.LENGTH_SHORT).show();
}
}
public void clicEnviar(View view) {
if (!txtMensaje.getText().toString().trim().equals("")) {
txtArea.append("Yo: " + txtMensaje.getText() + "\n");
txtMensaje.setText("");
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(txtMensaje.getWindowTo ken(), 0);
}
}
public boolean Conectar() {
boolean retornar = false;
String IP = txtIp.getText().toString();
if (IP.matches("[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}")) {
if (!txtPuerto.getText().toString().trim().equalsIgno reCase("")) {
if (txtPuerto.getText().toString().matches("[0-9]+")) {
try {
int PORT = Integer.valueOf(txtPuerto.getText()
.toString());
servidor = new Socket(IP, PORT);
Toast.makeText(Led2.this, "Entra hasta servidor", Toast.LENGTH_SHORT).show();
if (servidor.isConnected() == true) {
bandera = true;
dis = new DataInputStream(servidor.getInputStream());
dos = new DataOutputStream(
servidor.getOutputStream());
retornar = true;
} else {
retornar = false;
}
} catch (Exception e) {
Log.e("Error connect()", "" + e);
retornar = false;
}
} else {
Toast.makeText(Led2.this,
"Error: Puerto Invalido\nIngrese un numero entero",
Toast.LENGTH_SHORT).show();
retornar = false;
}
} else {
Toast.makeText(Led2.this,
"Error: Puerto Invalido\nIngrese un numero entero",
Toast.LENGTH_SHORT).show();
retornar = false;
}
} else {
Toast.makeText(Led2.this, "Error: IP invalida", Toast.LENGTH_SHORT)
.show();
retornar = false;
}
return retornar;
}
public void iniciarHandler() {
final Handler myHandler = new Handler();
(new Thread(new Runnable() {
boolean bandera = true;
public void run() {
while (bandera) {
String recibe;
try {
if (dis != null) {
recibe = dis.readUTF();
if (recibe != null) {
Led2.this.setMsg(recibe);
}
} else {
bandera = false;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myHandler.post(new Runnable() {
public void run() {
analizar();
}
});
}
}
})).start();
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public void analizar() {
txtArea.append("Servidor: " + getMsg() + "\n");
}
}
adjunto todo el proyecto
el problema esq no puedo conectarme desde el cliente, es decir mi app llega solamente hasta antes de establecer:
servidor = new Socket(IP, PORT);
esta linea es la que no ejecuta es como si se saltara la instruccion, mi app esta desarrollada en froyo y no entiendo el problema
aqui el codigo
package com.klauscj2.led2;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Led2 extends Activity {
private EditText txtIp, txtPuerto, txtMensaje, txtArea;
private Button bConectar, bEnviar;
Socket servidor;
private boolean bandera = false;
DataInputStream dis = null;
DataOutputStream dos = null;
String msg = "";
int conta = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_led2);
txtIp = (EditText) findViewById(R.id.editText1);
txtPuerto = (EditText) findViewById(R.id.editText2);
txtMensaje = (EditText) findViewById(R.id.editText4);
txtArea = (EditText) findViewById(R.id.editText3);
bConectar = (Button) findViewById(R.id.button1);
bEnviar = (Button) findViewById(R.id.button2);
bEnviar.setEnabled(false);
txtMensaje.setEnabled(false);
}
public void clicBoton(View view) {
// Toast.makeText(Led2.this, "Hola",Toast.LENGTH_SHORT).show();
boolean flag = Conectar();
conta = 0;
if (flag) {
iniciarHandler();
Toast.makeText(Led2.this,
"Conectado a " + servidor.getRemoteSocketAddress(),
Toast.LENGTH_SHORT).show();
bEnviar.setEnabled(true);
txtMensaje.setEnabled(true);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(txtMensaje.getWindowTo ken(), 0);
} else {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(txtMensaje.getWindowTo ken(), 0);
Toast.makeText(Led2.this, "No se ha podido conectar al servidor",
Toast.LENGTH_SHORT).show();
}
}
public void clicEnviar(View view) {
if (!txtMensaje.getText().toString().trim().equals("")) {
txtArea.append("Yo: " + txtMensaje.getText() + "\n");
txtMensaje.setText("");
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(txtMensaje.getWindowTo ken(), 0);
}
}
public boolean Conectar() {
boolean retornar = false;
String IP = txtIp.getText().toString();
if (IP.matches("[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}")) {
if (!txtPuerto.getText().toString().trim().equalsIgno reCase("")) {
if (txtPuerto.getText().toString().matches("[0-9]+")) {
try {
int PORT = Integer.valueOf(txtPuerto.getText()
.toString());
servidor = new Socket(IP, PORT);
Toast.makeText(Led2.this, "Entra hasta servidor", Toast.LENGTH_SHORT).show();
if (servidor.isConnected() == true) {
bandera = true;
dis = new DataInputStream(servidor.getInputStream());
dos = new DataOutputStream(
servidor.getOutputStream());
retornar = true;
} else {
retornar = false;
}
} catch (Exception e) {
Log.e("Error connect()", "" + e);
retornar = false;
}
} else {
Toast.makeText(Led2.this,
"Error: Puerto Invalido\nIngrese un numero entero",
Toast.LENGTH_SHORT).show();
retornar = false;
}
} else {
Toast.makeText(Led2.this,
"Error: Puerto Invalido\nIngrese un numero entero",
Toast.LENGTH_SHORT).show();
retornar = false;
}
} else {
Toast.makeText(Led2.this, "Error: IP invalida", Toast.LENGTH_SHORT)
.show();
retornar = false;
}
return retornar;
}
public void iniciarHandler() {
final Handler myHandler = new Handler();
(new Thread(new Runnable() {
boolean bandera = true;
public void run() {
while (bandera) {
String recibe;
try {
if (dis != null) {
recibe = dis.readUTF();
if (recibe != null) {
Led2.this.setMsg(recibe);
}
} else {
bandera = false;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myHandler.post(new Runnable() {
public void run() {
analizar();
}
});
}
}
})).start();
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public void analizar() {
txtArea.append("Servidor: " + getMsg() + "\n");
}
}
adjunto todo el proyecto