2011-05-26 83 views
0

我在使用Java的客户机/服务器时遇到问题。这是一个学校项目。Java服务器套接字问题

我打开我的Java服务器的插槽内:

BufferedReader reader = 
    new BufferedReader(new InputStreamReader(client.getInputStream())); 
PrintWriter writer = 
    new PrintWriter(new OutputStreamWriter(client.getOutputStream()), true); 

,我打开与客户端的插口(4客户,每个客户端1个线程):

BufferedReader reader = 
    new BufferedReader(new InputStreamReader(socket.getInputStream())); 
PrintWriter writer = 
    new PrintWriter(new OutputStreamWriter(socket.getOutputStream())); 

我得到同步错误。让我解释。

服务器正在向文本"J1GO"(表示"Player 1 go")发送给所有客户端,直到玩家1结束任务。然后变量发生变化,并发送"P2GO",但由于一个模糊的原因,只有客户端1(播放器1)获取新文本,并且需要30到40秒,直到播放器2,3和4获取文本。就好像是有瓶颈或什么的。 (然后,当它的玩家2的转向,这将需要更长的时间!)


编辑:

服务器端:

if (ligne.contains("Attente")) 
{ 
    writer.println("J"+Serveur.PlayerTurn+ "GO"); // (J1-2-3-4GO) 

    writer.flush(); 

客户端:

if (Deplacement) 
{ 
    sleep(); 
    writer.println("Attente"); 
    writer.flush(); 
    String ligne = reader.readLine(); 
    System.out.println(ligne); 
    int Coup = 99; 
    if (!Question) 
    { 
     if (!ligne.contains(IamPlayer +"GO")) //J1-2-3-4GO 
     { 

      sleep(); 
      writer.println("Attente"); 
      writer.flush(); 


     } 
     else // go on... 

我将解释我如何在服务器之间进行交换。当游戏开始时,服务器将"J1GO"发送给所有客户端,Player1客户端读取它并开始游戏。其他客户端阅读,但他们不开始游戏,他们发送"Waiting...",当服务器得到"Waiting..",他发送"J1GO",所以当它是Player2转弯,服务器将发送J2GO,所以Player2将开始游戏和Player1将返回到等待模式


完整的服务器端代码:

import java.net.*; 
import java.io.*; 



public class Serveur 
{ 
    public static int NombreJoueurs = 1; 
    public static boolean Debut = false; 
    public static Joueur J1 = new Joueur(); 
    public static Joueur J2 = new Joueur(); 
    public static Joueur J3 = new Joueur(); 
    public static Joueur J4 = new Joueur(); 
    public static ConnectionBD BD = new ConnectionBD(); 
    public static boolean Pret = false; 
    public static String TourJoueur = "1"; 
    public static String NumeroQuestion = null; 
    public static boolean PretAJouer() 
    { 
     if (Pret) 
      Pret=false; 
     else 
      Pret = true; 
     return Pret; 
    } 
    public static void AjouterJoueur() 
    { 
     NombreJoueurs = NombreJoueurs+1; 
    } 
    public void servir(int port) 
    { 
     try 
     { 

      ServerSocket serveur = new ServerSocket(port); 
      System.out.println("Serveur echo en ligne"); 

      System.out.println(Serveur.BD.VerifierReponse("V04B")); 

      boolean enService = true; 
      while (enService) 
      { 
       Socket client = serveur.accept(); 
       System.out.println("Client connecte"); 


       // pour chaque nouvelle connexion on crée un thread qui prend en 
       // charge les échanges avec le client 
       Connexion connexion = new Connexion(client); 
       new Thread(connexion).start(); 
      } 
     } 
     catch (Exception e) 
     { 
      System.err.println(e); 
     } 
    } 



    public static void main(String[] args) 
    { 
     new Serveur().servir(1337); 
    } 
} 

class Connexion implements Runnable 
{ 


    Socket client; 

    Connexion(Socket client) 
    { 
     this.client = client; 
    } 

    // thread qui s'occupe des échanges avec un client 
    public void run() 
    { 
     try 
     { 
      BufferedReader reader = 
       new BufferedReader(
       new InputStreamReader(client.getInputStream())); 

      PrintWriter writer = 
       new PrintWriter(
       new OutputStreamWriter(client.getOutputStream()), true); 

      boolean fini = false; 
      while (! fini) 
      { 
       String ligne = null; 
       ligne = reader.readLine(); 
       //System.out.println(ligne); 
       //System.out.println("RECEPTION CLIENT:"+ligne); 
       //création de joueur, le client doit envoyer NewJoueur: Vincent,Matte 
       //le numéro est attribuer par un variable incrémentable sur le serveur 
       if (ligne.startsWith("NewJoueur")) 
       { 
        String temp = Nettoyer(ligne); 
        String Nom = temp.substring(0,temp.indexOf(",")); 
        String Pseudo = temp.substring(temp.indexOf(",")+1, temp.length()); 
        if (Serveur.NombreJoueurs == 1) 
        { 
         Serveur.J1.SetNom(Nom); 
         Serveur.J1.SetPseudo(Pseudo); 
         writer.println("J1"); 
         writer.flush(); 
        }     else if (Serveur.NombreJoueurs == 2) 
        { 
         Serveur.J2.SetNom(Nom); 
         Serveur.J2.SetPseudo(Pseudo); 
         writer.println("J2"); 
         writer.flush(); 

        }     else if (Serveur.NombreJoueurs == 3) 
        { 
         Serveur.J3.SetNom(Nom); 
         Serveur.J3.SetPseudo(Pseudo); 
         writer.println("J3"); 
         writer.flush(); 

        }     else if (Serveur.NombreJoueurs == 4) 
        { 
         Serveur.J4.SetNom(Nom); 
         Serveur.J4.SetPseudo(Pseudo); 
         writer.println("J4"); 
         writer.flush(); 
        } 
        if (Serveur.NombreJoueurs > 4) 
        { 
         writer.println("ERREUR: Trop de joueurs"); 
         writer.flush(); 
        }     else Serveur.AjouterJoueur(); 
       } 
       if (ligne.startsWith("Setup?")) 
       { 
        if (Serveur.NombreJoueurs <= 4) 
        { 
         writer.println("not ready"); 
         writer.flush(); 
        } 
        else 
        { 
         writer.println("Setup: " + Serveur.J1.GetPseudo() + "," + Serveur.J2.GetPseudo() + "," + Serveur.J3.GetPseudo() + "," + Serveur.J4.GetPseudo()); 
         writer.flush(); 
        } 
       } 
       if (ligne.contains("Attente")) 
       { 
        writer.println("J"+Serveur.TourJoueur + "Deplacement"); 
        writer.flush(); 
        Pr("J'envoie:"+ "J"+Serveur.TourJoueur + "Deplacement"); 
        if (ligne.contains("AttenteQuestion")) //On recoit J2AttenteQuestion: V 
        { 
         if (ligne.contains("J"+Serveur.TourJoueur)) 
         { 
          String Categorie; 
          String Question; 
          Categorie = ligne.substring(ligne.indexOf(":")+1, ligne.length()); 

          Question = Serveur.BD.ObtenirQuestion(Categorie);//Envoie V pour vert, J pour Jaune .. etc.. 
          //Exemple de reponse... J2: V03: Quel homme a marcher sur la terre? |A[Vincent matte] B[.... 

          writer.println("DonneQuestions : " + Question); 
          writer.flush(); 
         } 
         else 
         { 
          try 
          { 
           Thread.sleep(2000); // do nothing for 1000 miliseconds (1 second) 
          } 
          catch (InterruptedException e) 
          { 
           e.printStackTrace(); 
          } 
          //Pr(ligne); 
          writer.println("not ready"); 
          writer.flush(); 
         } 
        } 
       } 
       if (ligne.startsWith("Reponse")) //Recoit ReponseJ1:V03A 
       { 
        String Reponse = ligne.substring(ligne.indexOf(":") +1, ligne.length()); // V03A 
        String Question = ligne.substring(ligne.indexOf(":"), (ligne.length()-1)); //V03 
        String CategorieString = ligne.substring(ligne.indexOf(":"), (ligne.indexOf(":")+1)); 
        char Categorie = CategorieString.charAt(0); 
        boolean BonneReponse = false; 
        System.out.println("Contenu de la reponse (de la methode)" + Serveur.BD.VerifierReponse(Reponse)); 
        if (Serveur.BD.VerifierReponse(Reponse).contains("Y")) 
         BonneReponse = true; 
        else 
         System.out.println("Le joueur a mal repondu"); 

        //boolean BonneReponse = Serveur.BD.VerifierReponse(Reponse); 
        if (BonneReponse) 
        { 
         System.out.println("Le joueur a bien repondu"); 
         //Serveur.BD.SetQuestionRepondue("Y", Question); 
         //Serveur.BD.AjouterScore(Categorie, Question); 
         //String Score = Serveur.BD.GetScore// (envoyer pseudo en string et retourne un score 
         // writer.println(Serveur.TourJoueur + ": " + "bravo" + "("+ Score +")"); 
         //mettre tour joueur = J +1; 
         if (Serveur.TourJoueur.equals("4")) 
         { 
          Serveur.TourJoueur = "1"; 
         } 
         else 
         { 
          int temp = Integer.parseInt(Serveur.TourJoueur); 
          temp++; 
          Serveur.TourJoueur = Integer.toString(temp); 
          System.out.println("Le joueur + " + Serveur.TourJoueur + " peut maintenant joué"); 
         } 
        } 
        else 
        { 
         writer.println(Serveur.TourJoueur + ": " + "fail"); 
         writer.flush(); 
         if (Serveur.TourJoueur.equals("4")) 
         { 
          Serveur.TourJoueur = "1"; 
         } 
         else 
         { 
          int temp = Integer.parseInt(Serveur.TourJoueur); 
          temp++; 
          Serveur.TourJoueur = Integer.toString(temp); 
          System.out.println("Le joueur + " + Serveur.TourJoueur + " peut maintenant joué"); 
         } 

        } 
       } 
      } 

      reader.close(); 
      writer.close(); 
      client.close(); 
     } 
     catch (IOException ioe) 
     { 
      /* 
      * Ici on choisit de quitter l'application, mais il y aurait peut-être 
      * moyen de traiter l'exception. 
      */ 
      System.err.println("Erreur d'entre-sortie"); 
      System.exit(1); 
     } 
    } 

    public void Pr(String pr) 
    { 
     System.out.println(pr); 
    } 
    public String Nettoyer(String Crap) 
    { 
     //Enleve n'importe quoi devant.. ex Lol: blabla, devient Blabla 
     int index = Crap.indexOf(":"); 
     index++; 
     String Propre = Crap.substring(index, Crap.length()); 
     return Propre; 
    } 
} 
+0

什么是睡眠()呼叫,他们睡了多久? – EJP 2011-05-27 00:48:06

+0

我的老师叫我睡觉电话,所以我不发送文本快,这是一个500毫秒等待 'Thread.sleep(500);' – Vincent90 2011-05-27 00:51:36

+0

我添加了更多信息 – Vincent90 2011-05-27 00:55:50

回答

-1

我想你需要一个writer.flush()您发送消息后。

+0

我总是写一个刷新后发送。就像客户端读取作者,但它读取旧文本 – Vincent90 2011-05-27 00:28:00

+0

(但它会很高兴看到更多的代码 - 实际发送/接收消息的代码 - 您可能有其他问题)。 – jcov 2011-05-27 00:28:05

+0

我添加了一些代码 – Vincent90 2011-05-27 00:33:04