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");
}
}
'output.writeObject'給出一個錯誤,因爲你沒有任何叫做'output'的東西。爲了用'Chat_ServerThread'解釋錯誤,你需要顯示那個類。 – Boann 2014-12-06 21:52:03
您應該始終發佈* exact *錯誤消息。不是一個釋義,précis,摘要,解釋,... – EJP 2014-12-06 22:25:26