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

时间:2013-04-21 13:54:04

标签: java bufferedreader objectinputstream objectoutputstream printstream

我正在创建一个客户端 - 服务器应用程序,其中服务器或客户端使用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

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

opcode1部分:

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

opcode2部分:

ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());
oos.writeObject(u.getUUID()); // MESSAGE 5
output.println("You now have a UUID."); // MESSAGE 6

1 个答案:

答案 0 :(得分: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