簡體   English   中英

套接字的輸入字節流包裝在兩種不同的流類型中嗎?

[英]Socket's input byte stream wrapped in two different stream types?

我正在嘗試實現這樣的協議:

Client side:
 1) client sends     command (String)
 2) client sends     object
 3) client receives  object or data   [depends on command]
Server side:
 1) server reads     command (String)
 2) server receives  object           [type depends on command]
 3) server sends     object or data   [depends on command]

在客戶端,我正在執行以下操作(程序塊在線,標記為“ !!!”):

/** Retrieves required wrapper streams */
private void getSocketStreams() {
    try {
        inputStream         = new DataInputStream(
                                    connection.getInputStream());

        /* !!! here is a problem: can I do next line's stuff? */
        inputObjectStream   = new ObjectInputStream(
                                    connection.getInputStream());

        outputWriter        = new BufferedWriter(
                                new OutputStreamWriter(
                                    connection.getOutputStream()));
        outputObjectStream  = new ObjectOutputStream(
                                    connection.getOutputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

/** "put" command processor */
private int processCmdPut(OrderInfo orderInfo) {

    /* Send command to peer */
    try {
        outputWriter.write("put");
        outputWriter.newLine();
        outputWriter.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }

    /* Send inserted object to peer */
    sendObject(orderInfo);

    /* Get from peer inserted info id */
    int id = -1;
    try {
        id = inputStream.readInt();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return id;
}


/**
 * Sends object to peer.
 * @param obj object to send.
 */
public void sendObject(Object obj){
    try {
        outputObjectStream.writeObject(obj);
        outputObjectStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

服務器端會執行鏡像客戶端的操作。

關於用“ !!!”標記的行的問題:是否可以用兩個不同的高杠桿流包裝套接字的字節流,並輪流(一個接一個地)對其進行讀/寫操作? 我錯了嗎? 有我的錯誤或誤解嗎?

緩沖往往會使使用不同的裝飾器鏈充其量變得困難。 您真的不想在同一流上混合文本和二進制數據。 我建議以與二進制數據相同的格式編寫文本。

這是服務器端代碼,以防萬一:

/** Retrieves required wrapper streams */
private void getSocketStreams() {
    try {
        inputReader         = new BufferedReader(
                                new InputStreamReader(
                                    clientSocket.getInputStream()));
        inputObjectStream   = new ObjectInputStream(
                                    clientSocket.getInputStream());
        outputStream        = new DataOutputStream(
                                    clientSocket.getOutputStream());
        outputObjectStream  = new ObjectOutputStream(
                                    clientSocket.getOutputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

/**
 * Process distinct command from client.
 * @param cmd command to process
 * @return connection state flag.
 */
private boolean processCmd(String cmd) {
    if ("put".equals(cmd)) {
        System.out.println(cmd);
        processCmdPut();
    } else if ("bye".equals(cmd)) {
        System.out.println(cmd);
        return false;
    }
    return true;
}

/** "put" command processor */
private void processCmdPut() {

    /* Reciever from a peer an object to put into data source */
    OrderInfo orderInfo = (OrderInfo) receiveObject();

    /* Put recieved object into a data source */
    int id = ordersService.put(orderInfo);

    /* Send to peer inserted data id */
    try {
        outputStream.writeInt(id);
        outputStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

/**
 * Recieves an object from peer.
 * @return recieved object, or <tt>null</tt> on error.
 */
public Object receiveObject() {
    Object res = null;
    try {
        res = inputObjectStream.readObject();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return res;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM