2014-12-06 19 views
-1
import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 

import java.io.*; 
import java.net.*; 
import java.util.ArrayList; 
import java.util.Arrays; 

public class Chat_Server extends JFrame 
{ 

// total number of clients that can be logged on to the message service 
final int NO_OF_CLIENTS = 4; 
// this ArrayList will store the client threads 
ArrayList<Chat_ServerThread> chatClients = new ArrayList<Chat_ServerThread>(); 
// arrays containing valid names and passwords 
String[] names = {"Adam Smith", "Bill Allen", "Cathy Clark", "Davina Doe"}; 
String[] passwords = {"LuQezz169", "amG4tyz", "Dw1wU9wy", "Fre195Ufm"}; 
// array containing valid chat tags 
String [] chattag = {"Arken", "Ben", "DarkLark", "Free", "group"}; 
// this array indicates whether a client is currently logged on 
boolean loggedOn[] = new boolean[NO_OF_CLIENTS]; 

// GUI components 
JTextArea outputArea; 

//Other Declartations 
ServerSocket serverSocket; 
DataInputStream serverInputStream; 
DataOutputStream serverOutputStream; 


public Chat_Server() 
{ super("Chat_Server"); 
    addWindowListener 
    ( new WindowAdapter() 
     { public void windowClosing(WindowEvent e) 
      { System.exit(0); 
      } 
     } 
    ); 

    try 
    { // gets a serversocket and binds to port 6000 
     serverSocket = new ServerSocket(6000); 

    } 
    catch(IOException e) // thrown by ServerSocket 
    { System.out.println(e); 
     System.exit(1); 
    } 

    // create and add GUI components 
    Container c = getContentPane(); 
    c.setLayout(new FlowLayout()); 
    // add text output area 
    outputArea = new JTextArea(17,30); 
    outputArea.setEditable(false); 
    outputArea.setLineWrap(true); 
    outputArea.setWrapStyleWord(true); 
    c.add(outputArea); 
    c.add(new JScrollPane(outputArea)); 
    setSize(360,310); 
    setResizable(false); 
    setVisible(true); 
} 

void getClients() 
{ // add message to server output area 
    addOutput("Server is up and waiting for a connection..."); 
    int userCount = 0; 
    while(userCount < NO_OF_CLIENTS) 
    { try 
     { /* client has attempted to get a connection to server, 
       create a socket to communicate with this client */ 
     Socket client = serverSocket.accept(); 

      // get input & output streams 
     ObjectInputStream serverInputStream = new ObjectInputStream(client.getInputStream()); 
     ObjectOutputStream serverOutputStrean = new ObjectOutputStream(client.getOutputStream()); 

      // add message to server output area 
      addOutput("Server is up and waiting for a connection..."); 
      // read encrypted username from input stream & decrypt 
      EncryptedMessage uname = (EncryptedMessage)serverInputStream.readObject(); 
      uname.decrypt(); 
      // read encrypted password from input stream & decrypt 
      EncryptedMessage pword = (EncryptedMessage)serverInputStream.readObject(); 
      pword.decrypt(); 

      // add messages to server output area 
      addOutput("\nLogin Details Received\n----------------------------------------"); 
      addOutput("encrypted username : " + uname.getMessage()); 
      addOutput("encrypted password : " + pword.getMessage()); 

      boolean valid = false; 
      int pos = Arrays.binarySearch(names, uname); 
      if(pos >= 0) 
      { if(passwords[pos].equals(pword) && !loggedOn[pos]) 
       { addOutput("Login details received from client " + (userCount+1) + ", " + uname + " are valid"); 
        addOutput("Client " + uname + " is known as " + chattag[pos]); 
        valid = true; 
        loggedOn[pos] = true; 
        // send Boolean value true to client 
        output.writeObject(new Boolean(true)); 

這會返回輸出錯誤。說它不能解決我在output.writeObject(new Boolean(true))上收到錯誤;並在Chat_ServerThread chatClient = new Chat_ServerThread(...)

    // add this new thread to the array list 
        Chat_ServerThread chatClient = new Chat_ServerThread (serverInputStream, serverOutputStream, names[pos], chattag[pos]); 

這將返回在Chatserver線程內的錯誤。我也在output.writeobject上發生錯誤。我不確定爲什麼它會這樣做,希望有人能夠說明一些事情。它說,構造函數沒有被定義

    chatClients.add(chatClient); 

        // start thread - execution of the thread will begin at method run 
        chatClient.start(); 

        userCount++; 
       } 
      } 
      if(!valid) 
      { /* user is not registered therefore write a Boolean value 
        false to the output stream */ 
       output.writeObject(new Boolean(false)); 
       addOutput("Login details received from client " + (userCount+1) + ", " + uname + " are invalid"); 
      } 
     } 
+0

'output.writeObject'給出一個錯誤,因爲你沒有任何叫做'output'的東西。爲了用'Chat_ServerThread'解釋錯誤,你需要顯示那個類。 – Boann 2014-12-06 21:52:03

+1

您應該始終發佈* exact *錯誤消息。不是一個釋義,précis,摘要,解釋,... – EJP 2014-12-06 22:25:26

回答

0

看來你還沒有創建的output實例。嘗試首先使用構造函數初始化它,然後將其作爲output的實例。

對於沒有被定義的構造函數,檢查構造函數(S)爲Chat_ServerThread類,並確保你傳遞給構造函數的參數匹配那些在「S的Chat_ServerThread一個實現構造定義。

+0

Chat_ServerThread本身不是一個類。在哪裏或如何初始化輸出的構造函數? – user3629216 2014-12-06 23:00:05

+0

如果Chat_ServerThread不是一個類,那麼它是什麼?您正在創建一個類型爲Chat_ServerThread的ArrayList,因此必須有一個表示它的Class。至於輸出的構造函數,目前還不清楚什麼類的「輸出」是一個實例。你想要創建一個實例的課程是? – cschieb 2014-12-07 18:08:00

相關問題