i have this code to communicate a server in eclipse with a client in android studio, i am using sockets, the client send messages to the server, the server reads that messages and ok all fine, but when the server send messages to the client, the client dont recive anything; this is the code
public class Servidor {
ServerSocket servidor=null;
String funcion;
public static Socket socket;
BufferedReader lector=null;
RegUser registrar=null;
PrintWriter escritor=null;
Gson gson = new Gson();
public Servidor(){
}
public static void main(String[] args) {
Servidor server=new Servidor();
server.iniciarHilo();
}
public void iniciarHilo(){
Thread principal=new Thread(new Runnable(){
public void run(){
try{
servidor=new ServerSocket(8080);
while(true){
socket=servidor.accept();
leer();
escribir();
}
}catch(Exception ex){
ex.printStackTrace();
}
}
});
principal.start();
System.out.println("Servidor iniciado......");
}
public void leer(){
Thread leer_hilo=new Thread(new Runnable(){
public void run(){
try{
lector=new BufferedReader(new InputStreamReader(socket.getInputStream()));
while(true){
JsonParser parser = new JsonParser();
String mensaje= lector.readLine();
JsonElement elemento = parser.parse(mensaje);
String mensaje_in=elemento.getAsJsonObject().get("tipo").getAsString();
if (lector==null){
System.out.println("Conexion Interrumpida....");
}
if (mensaje_in.equals("registrar")){
RegUser registrar=new RegUser();
System.out.println("Solicitud de Registro");
registrar.newUser(elemento);
}
else if (mensaje_in.equals("ingresar")){
System.out.println("Solicitud de Ingreso");
}
}
}catch(Exception ex){
ex.printStackTrace();
}
}
});
leer_hilo.start();
}
public void escribir(){
Thread escribir_hilo=new Thread(new Runnable(){
public void run(){
try{
escritor= new PrintWriter(socket.getOutputStream(),true);
escritor.println(funcion);
System.out.println(funcion);
}catch(Exception ex){
ex.printStackTrace();
}
}
});
escribir_hilo.start();
}
and this
public class RegUser extends Servidor{
Gson gson = new Gson();
ListaEnlazada listaUsuarios;
Comparar comparar=new Comparar();
public RegUser(){
}
public void newUser(JsonElement elemento){
Servidor respuesta=new Servidor();
//respuesta=new Servidor();
String user=elemento.getAsJsonObject().get("nombre").getAsString();
if(comparar.UserComp(user)){
listaUsuarios=new ListaEnlazada();
listaUsuarios.add(elemento);
System.out.println(listaUsuarios.get(0));
JsonObject o = new JsonObject();
o.addProperty("tipo", String.valueOf("registro"));
o.addProperty("estado", String.valueOf("completo"));
String enviar_mensaje = gson.toJson(o);
funcion=enviar_mensaje.toString();
escribir();
}
else
{
JsonObject comp = new JsonObject();
comp.addProperty("tipo","registro");
comp.addProperty("estado","existe");
String comparar=gson.toJson(comp);
funcion=comparar.toString();
escribir();
}
}
This code is of the Server code, if you need the client code(Android Studio) please tell me.
I need help please