java - 在 Java 中如何在字符串 I/O 和对象 I/O 流之间切换?

标签 java bufferedreader objectinputstream objectoutputstream printstream

我正在创建一个客户端-服务器应用程序,其中服务器或客户端使用 PrintStream 发送字符串并使用 BufferedReader/InputStreamReader 读取字符串。最终,我需要使用 ObjectInputStream/ObjectOutputStream 将对象从服务器发送到客户端,反之亦然。

如何从发送/接收字符串切换到发送/接收对象?我收到“无效的流 header :7372000E”。

以下是客户端的流部分(为了简洁起见,我删除了所有异常(exception)情况):

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedReader fromServer;
PrintStream clientToServer;
try {
    fromServer = new BufferedReader(new InputStreamReader(sock.getInputStream()));
    clientToServer = new PrintStream(sock.getOutputStream());
} catch (IOException e) {
    System.out.println(e.getMessage());
    e.printStackTrace();
    return;
}

String username;
String opcode;
System.out.print(fromServer.readLine()); // MESSAGE 1
username = in.readLine();
clientToServer.println(username); // MESSAGE 2
System.out.println(fromServer.readLine()); // MESSAGE 3
if (!username.matches("[a-zA-Z]\\w+")) {
    return;
}
opcode = fromServer.readLine(); // MESSAGE 4

如果操作码 1 的语句和文件内容,则:

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;
    }

}

然后通过对象输出流发送消息,如下所示:

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 

关于java - 在 Java 中如何在字符串 I/O 和对象 I/O 流之间切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16131877/

相关文章:

java - JTabbedPane - 如何使用鼠标滚轮滚动(而不是选择)选项卡(SCROLL_TAB_LAYOUT)

java - boxlayout中有 'top to bottom'和 'right to left'吗?

java - 新手 keytool 命令——如何更新已添加到 keystore 的证书?

java - 将 BufferedReader 应用到 Java 类的构造函数中时遇到问题

java - 从 txt 文件中删除第一行时遇到问题

java - 未找到 Google GSON 依赖项

java - ObjectInputStream 给出 StreamCorruptedException

java - 对通过 ObjectInputStream 接收的数据进行排序

java - 通过 TCP/IP 接收对象

java - 关于java中计算耗时的问题