2015-09-14 118 views
0

我有這個代碼在eclipse中與服務器在android studio中的客戶端進行通信,我使用套接字,客戶端發送消息到服務器,服務器讀取消息並確定一切正常,但是當服務器向客戶端發送消息時,客戶端不會收回任何東西;這是代碼服務器 - 與套接字的客戶端通信Java

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(); 
} 

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(); 
    } 

} 

此代碼是服務器代碼,如果你需要在客戶端代碼(Android Studio中),請告訴我。

我需要幫助,請

回答

1

你不能有

public static Socket socket在多客戶端服務器架構服務器。服務器在「接受」方法中創建客戶端套接字。

想想多個客戶端連接到單個服務器的場景。如果您在服務器上有靜態套接字,則它只能服務器套接字。那麼如何處理連接到服務器的「N」個客戶端呢?

  1. 在Servidor

  2. 刪除靜態套接字上創建套接字accept()方法之後,插座傳遞給leer()escribir()方法,這需要插座。

+0

我可以使用公共Socket套接字; ?? –

+0

這也不服務於目的。每個客戶端應該有一個套接字。如果100個客戶端連接到服務器,則服務器應該有100個套接字來處理每個客戶端。 –

+0

請參閱我的答案和評論http://stackoverflow.com/questions/32542152/java-error-with-a-printwriter –

0

如果您能夠在C#服務中託管你的服務器我已經創建了一個支持的連接1000的一個開源項目,包括保活協議(AKA心跳協議),認證模型,它可以很容易地擴展以及Java,Objective-C和C#API。該文檔包括適用於Android,iOS和Windows Mobile的示例。你可以找到它here