2012-12-18 62 views
0

非常簡單我想創建一個聊天室,接受多個客戶端,所有客戶端都可以分配自己的ID。每當他們輸入任何東西時,它都會發送給所有用戶。目前,我有一個回顯客戶端服務器,客戶端輸入內容並回顯。我的第一個問題是如何讓用戶給自己一個用戶名?顯然我需要一個接受名稱的變量,你推薦我把它放在什麼類中?我怎麼會得到這個名字本身像 if (theInput.equalsIgnoreCase("username" : "「))簡單的java聊天室

然後,我需要做的是迴應客戶說什麼,所有的客戶端。我不知道如何做到這一點,所以任何意見雖然我在網上發現了一些教程和示例代碼,但是我不明白它,如果我不明白即使它工作正常,我也不習慣使用它。我的代碼: EchoClient

'// echo client 
import java.util.Scanner; 
import java.io.*; 
import java.net.*; 
public class EchoClient { 
public static void main(String[] args) throws IOException { 
    Socket echoSocket = null; 
    //PrintWriter socketOut = null; 
    try { 
     echoSocket = new Socket("127.0.0.1", 4444); // connect to self at port 4444 
     System.out.println("Connected OK"); 
     Scanner socketIn = new Scanner(echoSocket.getInputStream()); // set up input from socket 
     PrintWriter socketOut = new PrintWriter(echoSocket.getOutputStream(), true); // set up output to socket 
     Scanner kbdIn = new Scanner(System.in);  // Scanner to pick up keyboard input 
     String serverResp = ""; 
     String userInput = kbdIn.nextLine();  // get input from the user 

     while (true) { 
      socketOut.println(userInput);     // send user input to the socket 
      serverResp = socketIn.nextLine();  // get the response from the socket 
      System.out.println("echoed back: " + serverResp); // print it out 
      if (serverResp.equals("Closing connection")) { break; } //break if we're done 
      userInput = kbdIn.nextLine(); // get next user input 
     } 
     socketOut.close(); 
     kbdIn.close(); 
     socketIn.close(); 
    } 
    catch (ConnectException e) { 
     System.err.println("Could not connect to host"); 
     System.exit(1); 
    } 
    catch (IOException e) { 
     System.err.println("Couldn't get I/O for connection"); 
     System.exit(1); 
    } 
    echoSocket.close(); 
} 

}」

EchoServer的

// multiple (threaded) server 
import java.net.ServerSocket; 
import java.net.Socket; 
public class EchoServerMult { 
public static void main(String[] args) throws Exception { 
    ServerSocket serverSocket = new ServerSocket(4444); // create server socket on this machine 
    System.err.println("Started server listening on port 4444"); 
    while (true) {  // as each connection is made, pass it to a thread 
     //new EchoThread(serverSocket.accept()).start(); // this is same as next 3 lines 
     Socket x = serverSocket.accept(); // block until next connection 
     EchoThread et = new EchoThread(x); 
     et.start(); 
     System.err.println("Accepted connection from client"); 
    } 
} 

}

EchoThread

// thread to handle one echo connection 
import java.net.*; 
import java.io.*; 
import java.util.Scanner; 
public class EchoThread extends Thread { 
private Socket mySocket = null; 

public EchoThread(Socket socket) {  // constructor method 
    mySocket = socket; 
} 
public void run() { 
    try { 
     PrintWriter out = new PrintWriter(mySocket.getOutputStream(), true); 
     Scanner in = new Scanner(mySocket.getInputStream()); 
     String inputLine; 
     while (true) { 
      inputLine = in.nextLine();  
      if (inputLine.equalsIgnoreCase("Bye")) { 
       out.println("Closing connection"); 
       break;  
      } else { 
       out.println(inputLine); 
      } 
     } 
     out.close(); 
     in.close(); 
     mySocket.close(); 
    } catch (Exception e) { 
     System.err.println("Connection reset"); // bad error (client died?) 
    } 
} 

}

+1

嗨 - 1)我不確定這只是一個「有趣的實驗」,一項家庭作業......或者如果您真的想創建一個人們可以使用的聊天室。如果是後者,我強烈建議你考慮編寫一個Web應用程序:例如,使用Tomcat和.jsp。 2)問:「迴應客戶對所有客戶說的話?」答:爲每個客戶創建一些「列表」。至少需要a)用戶名和b)IP地址或套接字。 – paulsm4

回答

2

最簡單的很可能是擁有所有的客戶端連接到一個公共類(當它們是創建)使用observer pattern。該鏈接甚至給出了一些java代碼。

要讓客戶端擁有用戶名,最簡單的辦法就是讓服務器發送的第一條消息是「輸入所需的用戶名:」,然後按照原樣返回值。否則,您可以使用inputLine.substring(int)username:{username}獲取用戶名。您可以在EchoThread中存儲用戶名。避免重複的用戶名需要您在EchoServer中存儲一組用戶名。然後您可以將該用戶名與消息一起傳遞給Observable類。

當前您的程序使用順序消息傳遞,如在客戶端和服務器中發送消息(更具體地說,客戶端發送消息,然後服務器發送相同的消息)。您需要更改此設置,以便客戶可以隨時接收消息。您可以通過在客戶端創建一個只發送或接收的線程(並在客戶端執行另一個線程)來完成此操作。在客戶端:

... 
System.out.println("Connected OK"); 
PrintWriter socketOut = new PrintWriter(echoSocket.getOutputStream(), true); // set up output to socket 
new ReceiverThread(echoSocket).start(); 
while (true) 
{ 
    String userInput = kbdIn.nextLine();  // get input from the user 
    socketOut.println(userInput);     // send user input to the socket 
} 
... 

ReceiverThread run方法裏面try ... catch:(其餘的看起來與EchoThread)

Scanner in = new Scanner(mySocket.getInputStream()); 
while (true) 
{ 
    String inputLine = in.nextLine();  
    if (inputLine.equalsIgnoreCase("Bye")) 
    { 
     out.println("Closing connection"); 
     System.exit(0);  
    } 
    else 
    { 
     out.println(inputLine); 
    } 
} 

System.exit()的可能不是最好的主意,但是這僅僅是給你一些想法做什麼。