2016-11-12 13 views
0

私は学校プロジェクトをやっています。私のグループは、端末のメッセンジャーアプリを提案しています(whatsappと似ています。私の問題は、私のBufferedReaderオブジェクトは、受信した後にクライアントから次の入力を読み込まないということです。初めて私がサーバーに情報を送るときは、BufferedReaderが動作しますが、それ以降は何も起こりません。これを修正するにはどうすればよいですか?私はThreadsSocketsに新しいので、私はほとんど何も知らないと仮定して答えてください。前もって感謝します。BufferedReader readLine()メソッドが複数回実行されていない、Java、Socket、Threads

package COE528.MajorProject; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.PrintWriter; 
import java.net.Socket; 

public class ClientUIManager { 
private Client user = null; 
private Socket userSocket = null; 
private BufferedReader serverScannerInput, scanner = null; 
private PrintWriter output = null; 


public static ClientUIManager instance; 

private ClientUIManager(){ 
    try{ 
     userSocket = new Socket("localhost", 4000); 
     serverScannerInput = new BufferedReader(new InputStreamReader(userSocket.getInputStream())); 
     output = new PrintWriter(userSocket.getOutputStream(), true); 
     scanner = new BufferedReader(new InputStreamReader(System.in)); 
    }catch(IOException ex){ 
     ex.printStackTrace(); 
    } 
} 

public static ClientUIManager getInstance(){ 
    if(instance == null) 
     instance = new ClientUIManager(); 
    return instance; 
} 

public static void main(String[] args) { 
    instance = ClientUIManager.getInstance(); 
    try{ 
    String userInput; 
    String[] breakUp; 
    String serverInput; 
    System.out.println("Type \"login\" to login or \"create\" to create a new account "); 
    while(!((userInput = instance.scanner.readLine()).equals("/exit"))){ 
     System.out.println("userInput: " + userInput); //GET RID OF LATER 
     breakUp = userInput.split(" "); 
     //establish connection and respond acordingly 
     if(userInput.equals("login")){ 
      System.out.println("Please enter login information: \"Username Password\" "); 
      userInput = instance.scanner.readLine(); 
      instance.login(userInput); 
     } 
     if(userInput.equals("create")){ 
      System.out.println("Please create new account information: \"Login Password\" "); 
      userInput = instance.scanner.readLine(); 
      instance.createClient(userInput); 
     } 
     if(instance.userLoggedIn()){ 
      //private messaging 
      if(userInput.charAt(0)== '@'){ 
       if(breakUp.length > 1) 
        instance.privateMessage(userInput); 
       else 
        System.out.println("Not enough agruments please enter @Username message"); 
      } 
      if(breakUp[0].equals("/add")){ 
       if(breakUp.length ==2) 
        instance.addFriend(breakUp[1]); 
       else 
        System.out.println("Not enough agruments please enter a new command"); 
      } 
      if(breakUp[0].equals("/unfriend")){ 
       instance.removeFriend(breakUp[1]); 
      } 
     } 
    } 
    instance.output.println("/exit"); 
    System.out.println("Closing program"); 
}catch(IOException ex){ 
     ex.printStackTrace(); 
     System.exit(1); 
    } 
} 

public boolean userLoggedIn(){ 
    //OVERVIEW: checks if user has logged in or not. 
    if(user == null) 
     return false; 
    else 
     return true; 
} 

public void privateMessage(String previousInput){ 
    //OVERVIEW: sends private message to another user (name inside string). Checks checkPersonOnline first: true sends message/false responds with message telling recipient is offline 
    String checkUsername; 
    String[] checkUsernameArray; 
    checkUsernameArray = previousInput.split(" "); 
    checkUsername = checkUsernameArray[0]; 
    checkUsername = checkUsername.replace("@", ""); 
    if(checkPersonOnline(checkUsername)) 
     output.println(previousInput); 
    else 
     System.out.println(checkUsername + " is offline, message not sent"); 
} 

public boolean checkPersonOnline(String username){ 
    boolean check = false; 
    output.println("/check "+ username); //send command to check with the username 
    try{ 
     if(serverScannerInput.readLine().equals("true")) 
      check = true; 
    }catch(IOException ex){ 
     ex.printStackTrace(); 
    } 
    return check; 
} 
public boolean checkPersonExists(String username){ 
    boolean check = false; 
    String serverInput; 
    try(BufferedReader serverScannerInput = new BufferedReader(new InputStreamReader(userSocket.getInputStream())); 
     PrintWriter outputToServer = new PrintWriter(userSocket.getOutputStream(), true); 
     ){ 
     outputToServer.println("/exists "+username); 
     serverInput = serverScannerInput.readLine(); 
     System.out.println("Server responded "+serverInput); 
     if(serverInput.equals("true")) 
      check = true; 
     else 
      check = false; 
    }catch(IOException ex){ 
     ex.printStackTrace(); 
    } 
    return check; 
} 
public void addFriend(String username){ 
    //OVERVIEW: takes a username, checks if person exists, checks if person is in client's friendsList, adds username to persons friendsList 
    //Check if person exists 
    if(checkPersonExists(username)){ 
     //check if person is in friends list. If false: add person to friendsList. If true: display's: "This person is in your friendsList already" 
     if(!user.checkFriends(username)){ 
      output.println("/add" + username); 
     }else{ 
      System.out.println("This person is already in your friends list"); 
     } 
    }else{ 
     System.out.println(username +" does not exist, please enter new command"); 
    }   
} 

public void removeFriend(String username){ 
    //Checks if username is in client's friends List, sends remove command to server 
    if(user.checkFriends(username)) 
     output.println("/remove "+username); 
    else 
     System.out.println(username+ " is not in your friends list, please enter new command"); 
} 

public void createClient(String previousInput){ 
    System.out.println("createClient method started");   
    try(   
     BufferedReader serverScannerInput = new BufferedReader(new InputStreamReader(userSocket.getInputStream())); 
     PrintWriter outputToServer = new PrintWriter(userSocket.getOutputStream(), true); 
     ){ 
     String userInput = previousInput; 
     String[] breakUp = userInput.split(" "); 
     String serverInput; 
     while(breakUp.length != 2){ 
      System.out.println("Missing argument please re-enter login"); 
      userInput = scanner.readLine(); 
      breakUp = userInput.split(" "); 
      if(userInput.equals("exit")){ //Re-check to see if you need this 
       System.out.println("closing program"); 
       System.exit(1); 
      } 
     } 
     //Checks if the user exists or not 
     if(!this.checkPersonExists(userInput)){ 
      //Sends commands to create user 
      System.out.println("/create sent"); 
      outputToServer.println("/create " +userInput); 
      output.flush(); 
     }else{ 
      System.out.println("The name you have entered has already been created please choose another command"); 
     } 
     System.out.println("new user created. Please login"); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 
} 

public void login(String previousInput) { 
    try{ 
    String userInput = previousInput; 
    String[] breakUp = userInput.split(" "); 
    String serverInput; 
    System.out.println("please enter login: \"username password\" "); 
    while(breakUp.length != 2){ 
     System.out.println("Missing argument please re-enter login"); 
     userInput = scanner.readLine(); 
     breakUp = userInput.split(" "); 
     if(userInput.equals("exit")){ //Re-check to see if you need this 
      System.out.println("closing program"); 
      System.exit(1); 
     } 
    } 
    //Checks if user inputed correct login information(/login command): send user login together and is suppose to recieve "true" or "false" 
    output.println("/exists "+ userInput); 
    serverInput = serverScannerInput.readLine(); 
    while(serverInput.equals(false)){ 
     //place code for bad login 
     System.out.println("Invalid login"); 
     userInput = scanner.readLine(); 
     login(userInput); 
     serverInput = serverScannerInput.readLine(); 
    } 
    if(serverInput.equals("true")){ 
     //creates user 
     output.println("/login "+ userInput); 
     user = new Client(breakUp[0], breakUp[1]); 
     //user.changeStatus(); 
     output.println("/getFriendsList "+ breakUp[0]); //sends getFriendsList plus username 
     serverInput = serverScannerInput.readLine(); 
     breakUp = serverInput.split(" "); 
     for(int x = 0; x< breakUp.length; x++){ 
      instance.addFriend(breakUp[x]); 
     } 
    } 
}catch(IOException ex){ 
     ex.printStackTrace(); 
     System.exit(1); 
    } 
} 
} 

サーバーのスレッド:

クライアントメイン(。私は詳細に任意のコードをしないのです場合はPS、私に知らせてくださいと私はすぐに私はメッセージを取得するように、よりを追加しますしてください)。それを自分自身を実行することなく、

package COE528.MajorProject; 

import java.net.Socket; 
import java.io.*; 
import java.net.ServerSocket; 


public class ServerThread extends Thread{ 
private final Socket client; 
private final ServerProtocol protocol; 

public ServerThread (Socket clientThread){ 
    client = clientThread; 
    protocol = new ServerProtocol(client); 
} 

@Override 
public void run(){ 
    System.out.println("Thread created"); //GET RID OF LATER 
    String inputLine=null; 
    String[] breakUp; 
    try(
      BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream())); 
      PrintWriter output = new PrintWriter(client.getOutputStream(), true); 
      ){ 
     System.out.println("trying BufferedReader " + input.toString()); 
     inputLine = input.readLine(); 
     while(!inputLine.equals("/exit")){ 
      System.out.println("server recieved :" + inputLine); 
      breakUp = inputLine.split(" "); 
      if(breakUp[0].equals("/login")){ 
       protocol.login(breakUp[0]); 
      } 
      if(breakUp[0].equals("/create")){ 
       System.out.println("create started"); 
       StringBuilder foo = new StringBuilder(); 
       foo.append(breakUp[1]); 
       foo.append(breakUp[2]); 
       protocol.createClient(foo.toString()); 
      } 
      if(breakUp[0].equals("/exists")){ 
       protocol.checkForClientExists(breakUp[1]); 
      } 
      inputLine = input.readLine(); 
      System.out.println("reading next input: " + inputLine); 
     } 
     //input.close(); 
     //output.close(); 

     //closing of thread 
    }catch(IOException ex){ 

    } 
} 
} 

答えて

0

、私はそれがためにあなたのBufferedReader S上のtry-と資源ループの使用である推測するつもりです。 BufferedReaderを閉じると、基本ストリームも閉じます。つまり、ソケット(したがって接続)が閉じられます。

入力ストリーム用に新しいBufferedReaderを開き、すべてのメソッド呼び出しで出力ストリーム用に新しいPrintWriterを開くのではなく、おそらくソケットの横にあるグローバルである必要がありますか?どちらかというと、この場合はtry-with-resourcesを使用しないでください。

+0

私はtry-with-resourcesを取り除こうとしますが、これが動作しない場合は通知します。 – Avi

関連する問題