对于一个项目,我必须编写一个多线程的客户端服务器实现
消息服务使用加密和压缩。我正在对服务器文件中的run方法进行编码,遇到以下几行时会遇到错误:CompressedMessage compmsg = (CompressedMessage)serverInputStream.readObject();
-在这里我在serverInputStream
下收到一条错误消息
serverInputStream无法解析
并在每个fromClient
下显示消息
fromClient无法解析
谁能解释导致这些错误的原因以及如何解决这些错误?
这是程序的一部分
public void run()
{ try
{ //send greeting to this client
threadOutputStream.writeObject(getCompressedMessage("Welcome to the chat server " + clientName));
// inform this client of other clients online
threadOutputStream.writeObject(getCompressedMessage("online" + getChatClients()));
// output to server window
addOutput(clientName + " known as " + chatName + " has joined");
// inform other clients that this client has joined
sendMessage("join" + chatName);
boolean quit = false, broadcast = false;
// this loop will continue until the client quits the chat service
while(!quit)
{ // read next compressed message from client
CompressedMessage compmsg = (CompressedMessage)serverInputStream.readObject();
// decompress message
compmsg.decompress();
// retrieve decompressed message
compmsg.getMessage();
// find position of separating character
int foundPos = fromClient.indexOf('#');
// list of recipients for message
String sendTo = fromClient.substring(0,foundPos);
// message to be sent to recipients
String message = fromClient.substring(foundPos+1);
// if the message is "quit" then this client wishes to leave the chat service
if(message.equals("quit"))
{ // add message to server output area
addOutput(clientName + " has " + message);
// inform other clients that this client has quit
sendMessage("quit" + chatName);
//send "goodbye" message to this client
threadOutputStream.writeObject("Goodbye");
// remove this client from the list of clients
remove(chatName);
}
else
{ // add message to server output area
addOutput(clientName + ">> " + message);
// split string to separate recipients names
String[] recipients = sendTo.split(",\\s*");
// sort this array to use binarySearch
Arrays.sort(recipients);
// identify if this message is to be sent to all other clients
foundPos = Arrays.binarySearch(recipients, chattag[chattag.length-1]);
if(foundPos >= 0)
// send this message to all other clients
sendMessage(chatName + ">> " + message);
else
// send this message to all clients in recipients array
sendMessage(chatName + ">> " + message, recipients);
}
} // end while
// close input stream
threadInputStream.close();
// close output stream
threadOutputStream.close();
}
catch(IOException e) // thrown by method readObject, writeObject, close
{ System.out.println(e);
System.exit(1);
}
catch(ClassNotFoundException e) // thrown by method readObject
{ System.out.println(e);
System.exit(1);
}
}
serverInputStream
在名为void getClients()
的程序上方的方法中声明,例如ObjectInputStream serverInputStream = new ObjectInputStream(client.getInputStream()); ObjectOutputStream serverOutputStream = new ObjectOutputStream(client.getOutputStream());
,并且在任何地方都没有声明来自客户端,它在所提供的框架代码中,仅出现在该代码段中 最佳答案
您不能只在代码中的任何位置使用变量。使用该变量时,它必须在范围内。
因此,如果在方法serverInputStream
中声明了getClients()
,则只能在该方法中使用它。考虑在更大的范围内声明变量,例如成员变量。通过Java Classes and Objects tutorial.