簡體   English   中英

TCP / IP客戶端:從服務器讀取多個輸入流的最佳方式

[英]TCP/IP Client : best way to read multiple inputstreams from server

我正在創建一個java Client程序,它向服務器發送命令,服務器發回一個確認和一個響應字符串。

響應以這種方式發回

client - > server:cmd_string

服務器 - >客戶端:ack_msg(06)

server - > client:response_msg

當我嘗試讀取輸入時,我只能讀取只讀取一個輸入流的確認消息

我的客戶端程序能夠以某種方式讀取消息(hacky方法)。 要讀取輸入,我必須使用Bufferedreader讀取ack消息。

  1. 這個緩沖的閱讀器只能讀取ack_msg而不是以下的msg
  2. 讀取響應消息需要DataInputstream代碼。
  3. 如果我跳過第1步並且只使用Datainputstream,我將無法讀取完整的消息。

Client.java

try {
            Socket clientSocket = new Socket(SERVER_ADDRESS, TCP_SERVER_PORT);// ip,port

            System.out.println(" client Socket created .. ");

            PrintWriter outToServer = new PrintWriter(clientSocket.getOutputStream(), true);

            String ToServer = command;
            outToServer.println(ToServer);

            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//          
            // while (!in.ready()) {
            // }
            System.out.println(in.read()); // Read one line and output it


            // ===
            byte[] messageByte = new byte[1000];//assuming msg size -need to know eact msg size ?
            boolean end = false;
            String dataString = "";
            int bytesRead = 0;

            try {
                DataInputStream in1 = new DataInputStream(clientSocket.getInputStream());
//
                while (!end) {
                    bytesRead = in1.read(messageByte);
                    dataString += new String(messageByte, 0, bytesRead);
                    if (dataString.length() > 0) {
                        end = true;
                    }
                }
                System.out.println("MESSAGE: " + dataString);
            } catch (Exception e) {
                e.printStackTrace();
            }
            // ===

            in.close();             

            clientSocket.close();
            System.out.print("connection closed");

        } catch (Exception e) {
            System.out.print("Whoops! It didn't work!\n");
            e.printStackTrace();
        }

產量

客戶端套接字創建..

響應:6

消息:³CO³0³Œ

連接關閉

當我跳過第1步

客戶端套接字創建..

信息: -

連接關閉

如何編寫從服務器讀取所有輸入的代碼(確認消息和響應消息,它還應該檢查數據的大小並相應地讀取)?

提到如何讀取Server Socket JAVA中的所有Inputstream

謝謝

只要發送至少1個字節,這些語句就會強制關閉流。

dataString += new String(messageByte, 0, bytesRead);
if (dataString.length() > 0) {
   end = true;
}

一旦收到6的確認,連接就會關閉。 如果忽略ack,則可以在多個分組中發送響應,在接收到第一個分組之后連接將關閉。

您必須能夠確定確認和響應何時結束。

  1. 如果你知道ack和響應中有多少個字符,你可以像你提到的問題一樣循環。 你需要一個ack循環和一個響應循環。
  2. 如果您不知道字符數,則必須查找特殊字符,例如行尾或文件結尾,以了解ack行何時結束以及服務器響應何時結束。 在每條消息的末尾查找特殊字符。
  3. 第三種可能性是等待特定的時間。 時間到期后,繼續。 這在TCP上是不可靠的,因為數據可能丟失並重新發送。 可能需要比分配響應更多的時間。 我同意以下評論。 最初,我猶豫在列表中添加3。 該選項很可能用於檢測通信信道的問題。 如果時間到期,那么客戶端將放棄錯誤條件,它將不會繼續處理,好像一切都好。

您不能在同一底層套接字上同時使用BufferedReader和DataInputStream。 您將丟失BufferedReader中的數據。 對於所有內容和套接字的使用壽命,請使用相同的流或閱讀器。 Ditto編寫器和輸出流。

InputStream input = null;
OutputStream output = null;

Socket cs = new Socket("LocalHost", 6789);
 input = cs.getInputStream();
output = cs.getOutputStream();
 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(output));

bw.write("Send your String");
bw.flush();

int bytesRead = 0;
byte[] messageByte = new byte[1000];
String dataString = "";

bytesRead = input.read(messageByte);
dataString = new String(messageByte, 0, bytesRead);

暫無
暫無

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

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