简体   繁体   English

套接字未在Java 7中接收输入

[英]Socket not Receiving Input in Java 7

I have run into an interesting issue trying to upgrade one of my applications from the Java 6 to Java 7. It is a simple Java socket program. 尝试将我的应用程序之一从Java 6升级到Java 7时遇到了一个有趣的问题。这是一个简单的Java套接字程序。 It sends a command to a COM socket and receives a response. 它向COM套接字发送命令并接收响应。 It works perfectly in a Java 6 environment, but when I try to run the same code in a Java 7 environment, the socket appears to receive nothing in the InputStream. 它可以在Java 6环境中完美运行,但是当我尝试在Java 7环境中运行相同的代码时,套接字在InputStream中似乎什么也没收到。

I can confirm that the COM socket it's connecting to does receive the command and sends the response. 我可以确认所连接的COM套接字确实收到了命令并发送了响应。 This is run on the exact same machine in both cases with the firewall disabled, and it's the exact same code ran both times. 在这两种情况下,都在禁用防火墙的情况下在完全相同的计算机上运行,​​并且两次都运行了完全相同的代码。

Has something changed in Java 7, do I have some deeper flaw, or is this simply a Java bug? Java 7中有什么更改吗?我是否有更深层的漏洞?或者这仅仅是Java错误?

Here is a slightly stripped version of the code. 这是代码的一部分。

public static void main(String[] arguments) throws Exception {
  InetAddress server = InetAddress.getByName(serverAddress);
  Socket sock = SSLSocketFactory.getDefault().createSocket(server.getHostAddress(), port);
  InputStream in = sock.getInputStream();
  OutputStream out = sock.getOutputStream();
  out.write(command.getBytes()); //Is valid command
  String token = "";
  responseReader: while (true) {
    try {
      Thread.sleep(1);
    }
    catch (InterruptedException exception) {}
    byte[] d = new byte[in.available()];
    int avail = in.read(d);
    for (int i = 0; i < avail; i++) {
      if (d[i] == fieldSeperator) {
        token = "";
      }
      else if (d[i] == commandSeperator) {
        break responseReader;
      }
      else {
        token += (char) d[i];
      }
    }
  }
}

I've tried as much as I can think of, most of the time knowing it shouldn't matter. 我已经尽我所能尝试了很多,大部分时间都知道这无关紧要。 Using different methods of reading the stream, casting to SSLSocket and making different calls, adding some sleeps. 使用不同的方法读取流,转换为SSLSocket并进行不同的调用,添加一些睡眠。

The code is wrong. 代码错误。 You shouldn't use available() like that. 你不应该这样使用available()。 If there is no data available you will allocate a zero length buffer and execute a zero length read, which will retun zero without blocking. 如果没有可用数据,则将分配一个零长度的缓冲区并执行零长度的读取,这将在不阻塞的情况下将其调整为零。 Use a constant like 8192 for the buffer size, and allocate the buffer outside the loop. 使用像8192这样的常量作为缓冲区大小,并在循环外分配缓冲区。 And get rid of the sleep() too. 并摆脱sleep()。

There are few if any correct uses of available(), and this isn't one of them. 几乎没有可用的available()正确用法,这也不是其中之一。

And note that available() always returns zero for an SSLSocket, and has always done so right back to Java 1.3 and the separate JSSE download. 并注意,available()对于SSLSocket始终返回零,并且一直如此,直到Java 1.3和单独的JSSE下载。 So I am unable to accept that the same code worked in Java 6. 因此,我无法接受相同的代码在Java 6中也能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM