0

我正在創建一個客戶端 - 服務器應用程序,其中服務器或客戶端使用PrintStream發送字符串並使用BufferedReader/InputStreamReader讀取字符串。最終,我需要使用ObjectInputStream/ObjectOutputStream將對象從服務器發送到客戶端,反之亦然。如何在Java中的字符串I/O和對象I/O流之間切換?

如何從發送/接收字符串切換到發送/接收對象?我得到「無效的流頭: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語句和然後:

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 
+0

你能分享自己當前的實施? – 2013-04-21 14:00:51

+1

字符串也是對象。你不能一直髮送對象嗎? – NilsH 2013-04-21 14:01:09

+0

@LeonardBrünings完成。 – NobleUplift 2013-04-21 14:16:49

回答

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