在Java中如何在字符串I/O和对象I/O流之间切换?
我正在创建一个客户机-服务器应用程序,其中服务器或客户机使用PrintStream发送字符串,并使用BufferedReader/InputStreamReader读取字符串。最后,我需要使用ObjectInputStream/ObjectOutputStream将对象从服务器发送到客户端,反之亦然 如何从发送/接收字符串切换到发送/接收对象?我得到“无效流头:7372000E” 以下是客户端的流部分(为了简洁起见,我删去了所有例外): 如果opcode1的语句和文件内容,则:在Java中如何在字符串I/O和对象I/O流之间切换?,java,bufferedreader,objectinputstream,objectoutputstream,printstream,Java,Bufferedreader,Objectinputstream,Objectoutputstream,Printstream,我正在创建一个客户机-服务器应用程序,其中服务器或客户机使用PrintStream发送字符串,并使用BufferedReader/InputStreamReader读取字符串。最后,我需要使用ObjectInputStream/ObjectOutputStream将对象从服务器发送到客户端,反之亦然 如何从发送/接收字符串切换到发送/接收对象?我得到“无效流头:7372000E” 以下是客户端的流部分(为了简洁起见,我删去了所有例外): 如果opcode1的语句和文件内容,则: ObjectIn
ObjectInputStream ois;
ObjectOutputStream oos;
UUID u = null;
ois = new ObjectInputStream(new FileInputStream(f));
u = (UUID) ois.readObject();
oos = new ObjectOutputStream(sock.getOutputStream());
oos.writeObject(u); // MESSAGE 5
Else语句和opcode2的更多文件内容,然后:
ObjectOutputStream oos;
ObjectInputStream ois;
UUID u;
ois = new ObjectInputStream(sock.getInputStream());
u = (UUID) ois.readObject(); // MESSAGE 5
System.out.println("UUID " + u.toString() + " received.");
oos = new ObjectOutputStream(new FileOutputStream(f));
oos.writeObject(u);
System.out.println("UUID " + u.toString() + " written to file.");
服务器执行以下操作:
PrintStream output = new PrintStream(sock.getOutputStream());
BufferedReader input = new BufferedReader(new InputStreamReader(sock.getInputStream()));
output.println("Please enter your username: "); // MESSAGE 1
username = input.readLine(); // MESSAGE 2
output.println("Welcome back!"); // MESSAGE 3
output.println("opcode1") OR output.println("opcode2") // MESSAGE 4
操作码1部分:
ObjectInputStream ois = new ObjectInputStream(sock.getInputStream());
UUID local = (UUID) ois.readObject(); // MESSAGE 5
if (user.getUUID().equals(local))
output.println("Your UUID is valid."); // MESSAGE 6
操作码2部分:
ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
oos.writeObject(u.getUUID()); // MESSAGE 5
output.println("You now have a UUID."); // MESSAGE 6
如果您知道要通过流发送不同的数据类型,您可以创建自己的“YourMessage”类,该类将记录存储的数据类型
private ObjectOutputStream sOutput;
private ObjectInputStream sInput;
class YourMessage {
static final int STRINGTYPE = 0; OBJECTTYPE = 1; // etc
int type;
String str;
// Any other message types
// Constructor
public YourMessage(int type, String str){
this.type = type;
this.str = str;
// etc
}
// Create Getters here
String getMessage(){
return str;
}
}
然后通过objectoutput流发送消息,如下所示:
sOutput.writeObject(new YourMessage(YourMessage.STRINGTYPE, message));
然后,在服务器上接收到该信息时
ym = (YourMessage) sInput.readObject();
switch (ym.getType()) {
case YourMessage.STRINGTYPE:
// do something with -
ym.getMessage();
break;
case // other types of messages
能否共享当前的实现?字符串也是对象。你不能一直发送物品吗?@LeonardBrünings完成了。只是。。。不要这样做。流可以有一大堆内部状态,如果在一个较低级别的流上打开两个较高级别的流,则几乎无法保持内部状态同步。有什么原因不能直接通过OOS发送字符串命令吗?在您的情况下,罪魁祸首可能是
BufferedReader
。它完全按照can上的说明执行,在内部缓冲数据。也就是说,它从底层读取器读取整个字符块,然后稍后将缓冲数据拆分为多行。这意味着在您尝试“切换”到ObjectInputStream
时,BufferedReader
已经吞并了一些序列化数据。(包括错误所涉及的预期标题。)
ym = (YourMessage) sInput.readObject();
switch (ym.getType()) {
case YourMessage.STRINGTYPE:
// do something with -
ym.getMessage();
break;
case // other types of messages