我正在做一個學校項目,我的小組已經提出了終端web messenger應用程序的主題(類似於whatsapp,絕對不那麼複雜)。我的問題是我的BufferedReader
對象在收到客戶端的下一個輸入後不會讀取下一個輸入。我第一次向服務器發送信息BufferedReader
的作品,但隨後沒有任何反應。我能做些什麼來解決這個問題?我是Threads
和Sockets
的新手,所以請回答假設我幾乎什麼都不知道。先謝謝你。 (PS。如果我很想念我的任何細節的代碼,請務必讓我知道,我會盡快得到消息添加更多)BufferedReader readLine()方法沒有多次運行,Java,Socket,線程
客戶端主:
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);
}
}
}
服務器線程:
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){
}
}
}
我會嘗試擺脫試用資源,如果這樣做/不工作,我會通知你。 – Avi