Java 无法解析serverInputStream
对于一个项目,我必须编写一个 使用加密和压缩的消息服务。我正在服务器文件中对run方法进行编码,在到达以下行时遇到错误:Java 无法解析serverInputStream,java,eclipse,encryption,tcp,compression,Java,Eclipse,Encryption,Tcp,Compression,对于一个项目,我必须编写一个 使用加密和压缩的消息服务。我正在服务器文件中对run方法进行编码,在到达以下行时遇到错误: CompressedMessage compmsg=(CompressedMessage)serverInputStream.readObject()-这里我在服务器InputStream下得到一个错误消息 无法解析serverInputStream 在客户端的每个下 无法解析fromClient 有人能解释是什么导致这些错误以及如何解决它们吗? 这是节目的一部分 p
CompressedMessage compmsg=(CompressedMessage)serverInputStream.readObject()代码>-这里我在服务器InputStream
下得到一个错误消息
无法解析serverInputStream
在客户端的每个下
无法解析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=新的ObjectOutputStream(client.getOutputStream())
和from client没有在任何地方声明,它位于提供的框架代码中,只出现在该代码段中您不能只在代码中的任何地方使用变量。使用变量时,该变量必须在作用域内
因此,如果在方法getClients()
中声明了serverInputStream
,则只能在该方法中使用它。考虑在较大范围内声明变量,比如成员变量。检查一下这个作业,一个朋友让我帮他做这个作业,因为他不知道如何让它像你一样工作
fromClient
是我们解压缩后检索的消息
// read next compressed message from client
CompressedMessage cm = (CompressedMessage) threadInputStream.readObject();
// decompress message
cm.decompress();
// retrieve decompressed message
String fromClient = cm.getMessage();
这是我在他的作业中使用的代码行。
也不要使用此处声明的serverInputStream
使用threadInputStream
private class Chat_ServerThread extends Thread
serverInputStream
在哪里声明?在哪里声明了fromClient
?serverInputStream
是在名为voidgetClients()
的程序的更高级别的方法中声明的,类似于这样的`ObjectInputStream serverInputStream=newobjectinputstream(client.getInputStream());ObjectOutputStream服务器OutputStream=新的ObjectOutputStream(client.getOutputStream());`客户端的
没有在任何地方声明,它在提供的框架代码中,只出现在该代码段中。您可以编辑您的问题以添加更多详细信息。好的,那么我该如何阅读客户端文件中的下一条压缩消息呢?我是要在run()
类中重新声明这些变量,还是使用其他代码来读取这些变量?@user3080208如果有些问题不起作用,您将不得不问另一个问题,并提供完整的详细信息。首先,找出变量作用域并适当地使用它们;//获取输入和输出流ObjectInputStream服务器InputStream=new ObjectInputStream(client.getInputStream());ObjectOutputStream serverOutputStream=新的ObjectOutputStream(client.getOutputStream());对于run方法,serverInputStream
的错误消失了。我仍然不明白客户机的做了什么,或者为什么会出错。我对Java有点生疏,以前从未在Eclipse或网络中使用过它。@user如果你不知道fromClient
做什么,我们怎么知道?我们可以解释为什么会出现错误,但不能解释代码的作用。