尝试在 java 中通过 tcp 发送千字节消息时出错 [英] Error when attempting to send kilobyte message over tcp in java

查看:33
本文介绍了尝试在 java 中通过 tcp 发送千字节消息时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过比较客户端维护的消息发送计数器与服务器端的消息接收计数器来测试 TCP 连接中的数据包丢失.我发送的消息是 25 公斤,前 5 个字节包含来自客户端的计数,以字符串的形式填充星号以保持 5 个字节的长度.但是,在 3 或 4 条成功消息后,服务器无法找到计数.有什么我忘记考虑的吗?我的猜测是服务器无法跟上客户端,因为成功消息的数量在出错前从 3 到 4 不等.

I'm attempting to test packet loss across a TCP connection by comparing a messages sent counter maintained on the client side with a messages received counter on the server side. The messages I'm sending are 25 kilos with the first 5 bytes containing the count from the client in the form of a string padded with asterisks to maintain a 5 byte length. However after 3 or 4 successful messages the server fails to find the count. Is there something that I am forgetting to consider? My guess is that the server is having trouble keeping up with the client as the number of successful messages varies from 3 - 4 before error.

客户端代码:

try{
        clientSocket = new Socket("192.168.0.15", SERVER_PORT);   
        DataInputStream in = new DataInputStream(clientSocket.getInputStream());
        DataOutputStream out = new DataOutputStream(clientSocket.getOutputStream());
        int messageCounter = 0;
        byte [] message1 = new byte[25 * 1024];
        byte [] returnMessage1 = new byte[25 * 1024];
        byte [] count;
        long startTime1 = System.nanoTime();
        while (messageCounter < 100000) {
            System.out.println(messageCounter);
            addCountToMessage(formatCount(messageCounter).getBytes(), message1);
            out.write(message1);
            in.read(returnMessage1);     
            messageCounter ++;    
        }  
    }

服务器端代码:

try {
      byte [] data = new byte[35 * 1024];
      System.out.println("STARTING LOOP");
      System.out.println("***********************************");
      int messageCounter = 0;
      int clientMessageCount = 0;
      String clientCountString = "";
      byte[] clientCount = new byte[5];

      while (true){
          in.read(data);
          clientCount[0] = data[0];
          clientCount[1] = data[1];
          clientCount[2] = data[2];
          clientCount[3] = data[3];
          clientCount[4] = data[4];

          clientCountString = new String(clientCount);
          System.out.println("Received Count String: \"" + clientCountString + "\"");
          clientCountString = clientCountString.replace("*", "");
          clientMessageCount = Integer.parseInt(clientCountString);

          System.out.println("Client Count: " + clientMessageCount + ", Server Count: " + messageCounter);
          out.write(data);
          System.out.println("***********************************");
          messageCounter ++;
      }
   }

服务器输出:

java TCPServer

开始循环

接收计数字符串:0****"客户端数量:0,服务器数量:0

Received Count String: "0****" Client Count: 0, Server Count: 0

接收计数字符串:1****"客户端数:1,服务器数:1

Received Count String: "1****" Client Count: 1, Server Count: 1

接收计数字符串:2****"客户端数:2,服务器数:2

Received Count String: "2****" Client Count: 2, Server Count: 2

接收计数字符串:3****"客户端数:3,服务器数:3

Received Count String: "3****" Client Count: 3, Server Count: 3

接收计数字符串:""

Exception in thread "Thread-0" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:481)
at java.lang.Integer.parseInt(Integer.java:527)
at Connection.run(TCPServer.java:52)

推荐答案

我曾经处理过写作 &按照下面的代码读取字节数组,即使对于兆字节的数据,它也始终适用于我.

I used to handle writing & reading of byte array as per below code, which always works for me even for mega bytes of data.

向套接字写入字节数组和从套接字读取字节数组的示例代码

/* Write byte array */
OutputStream out = socket.getOutputStream(); 
DataOutputStream dos = new DataOutputStream(out);
int length = msgBytes.length;
dos.writeInt(length);
if (length > 0) {
    dos.write(msgBytes);
}

/*Read byte array */
DataInputStream dis = new DataInputStream(socket.getInputStream());
int length = dis.readInt();                   
if(length>0) {
    byte[] msgBytes = new byte[length];
    dis.readFully(msgBytes, 0, length); 
}

这篇关于尝试在 java 中通过 tcp 发送千字节消息时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆