0

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

2 Answers2

1

You cannot have

public static Socket socket at server in multi client server architecture. Server creates a client socket in "accept" method.

Think of scenario where multiple clients connecting to single server. If you have static Socket at server, it can server only socket. Then how to you handle "N" number of clients connecting to server?

  1. Remove static socket in Servidor

  2. On creation of socket after accept() method, pass the socket to leer() and escribir() methods, which require socket.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
0

If you are able to host your server within a C# service I have created an open source project that supports 1000s of connections, includes a keep-alive protocol (AKA heartbeat protocol), an authentication model which can easily be extended as well as Java, Objective-C and C# APIs. The documentation includes samples for Android, iOS and Windows Mobile. You can find it here.

Mike
  • 559
  • 5
  • 21