0

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

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

以下是客户端的流部分(为简洁起见,我删除了所有例外):

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

如果 opcode1 的语句和文件内容,则:

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

操作码 2 的 Else 语句和更多文件内容,然后:

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
4

1 回答 1

1

如果您知道您将通过流发送不同的数据类型,您可以创建自己的“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 
于 2013-04-28T14:22:59.817 回答