Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 套接字读取数据的时间过长(BufferedReader)_Java_Sockets_Io - Fatal编程技术网

Java 套接字读取数据的时间过长(BufferedReader)

Java 套接字读取数据的时间过长(BufferedReader),java,sockets,io,Java,Sockets,Io,我正在从交换机读取ISO消息,读取时间太长。读取整个流甚至需要两分钟的时间,如果在8秒内没有得到回复,交换机将超时会话。有没有其他方法可以不使用BufferedReader从套接字获取inputstream s = new ServerSocket(8777); echo("Server socket created.Waiting for connection..."); //get the connection socket

我正在从交换机读取ISO消息,读取时间太长。读取整个流甚至需要两分钟的时间,如果在8秒内没有得到回复,交换机将超时会话。有没有其他方法可以不使用BufferedReader从套接字获取inputstream

        s = new ServerSocket(8777);

        echo("Server socket created.Waiting for connection...");
        //get the connection socket
        conn = s.accept();
        echo("Connection received from " + conn.getInetAddress().getHostName() + " : " + conn.getPort());

        //3. get Input and Output streams
        out = new PrintStream(conn.getOutputStream());
        //out.flush();
        System.out.println(new Date());
        //in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        System.out.println(new Date());
        InputStream  in  = conn.getInputStream();
        message = in.readLine();
        echo("client>" + message);
        System.out.println(new Date());
下面是您可以看到的日志,从开始读取到输出消息的时间相差大约两分钟

Server socket created.Waiting for connection...
Connection received from 11.xx.xx.xx : 51639
Fri Jul 08 11:53:48 EAT 2016
Fri Jul 08 11:53:48 EAT 2016
client>ISO8583-9300004918040387042160708122130801ISO8583-    9300004918040387049160708122230802
Fri Jul 08 11:55:51 EAT 2016

猜测:message=in.readLine();等待到endofLine\n。可能您没有发送一些,或者您没有刷新发送套接字/流。

猜测:message=in.readLine();等待到endofLine\n。可能您没有发送一些,或者您错过了刷新发送套接字/流。

您发布的输出不包含行,因此
readLine()
不合适,并且您希望消息一次一条,因此
IOUtils.toByteArray()
也不合适。试试看,呃,
read(byte[])
您发布的输出不是由行组成的,因此
readLine()
不合适,并且您希望消息一次一条,因此
IOUtils.toByteArray()
也不合适。试试看,呃,
read(byte[])

我想你应该用某种方式来检测消息的结尾

我不熟悉ISO8583,所以你必须弄清楚。可能是因为它是一个固定长度的消息协议,或者您可以检测到一个消息终止符

一个典型的例子是:

private static final int BUFFER_SIZE = 1024; // Or other reasonable value

// ...

byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = 0;

// assuming you got the InputStream as "input"
while ( (bytesRead = input.read(buffer)) > 0 ){ // -1 indicates EOF
    // read bytes are now in buffer[0..bytesRead-1]
    // analyse bytes to maybe add up multiple reads to a complete message.
}

为了简洁起见,我省略了异常处理。

我想您应该以某种方式使用并检测消息的结尾

我不熟悉ISO8583,所以你必须弄清楚。可能是因为它是一个固定长度的消息协议,或者您可以检测到一个消息终止符

一个典型的例子是:

private static final int BUFFER_SIZE = 1024; // Or other reasonable value

// ...

byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = 0;

// assuming you got the InputStream as "input"
while ( (bytesRead = input.read(buffer)) > 0 ){ // -1 indicates EOF
    // read bytes are now in buffer[0..bytesRead-1]
    // analyse bytes to maybe add up multiple reads to a complete message.
}

为了简洁起见,我省略了异常处理。

我猜交换机不会以换行结束传输-因此会出现延迟。尝试在没有BufferedReader的情况下读取字节。代码段中是否存在复制和粘贴错误?在s中声明了两个
。不应编译。仍不工作。我已经尝试过这段代码,但延迟仍然存在于=conn.getInputStream()中的InputStream中;byte[]bytes=IOUtils.toByteArray(in)//message=in.readLine();echo(“client>”+bytes.toString());System.out.println(新日期());“此方法在内部缓冲输入,因此无需使用BufferedInputStream。”只需使用大小合理的“byte[]buffer”从InputStream中直接读取即可。@Fildor该引号来自何处?@EJP抱歉,应该已经发布了源代码。我猜开关不会以换行结束传输-因此会出现延迟。尝试在没有BufferedReader的情况下读取字节。代码段中是否存在复制和粘贴错误?在
s中声明了两个
。不应编译。仍不工作。我已经尝试过这段代码,但延迟仍然存在于=conn.getInputStream()中的InputStream中;byte[]bytes=IOUtils.toByteArray(in)//message=in.readLine();echo(“client>”+bytes.toString());System.out.println(新日期());“此方法在内部缓冲输入,因此无需使用BufferedInputStream。”只需使用大小合理的“byte[]buffer”从InputStream中直接读取即可。@Fildor该引号来自何处?@EJP抱歉,应该已经发布了源代码。循环条件应为
。只有当缓冲区长度为零时,才能得到零,而这不是要循环使用的条件:它不会到达任何地方。只是一个bug。谢谢它工作了。我使用了ByteArrayOutputStream,它不再延迟了。但现在的问题是,当我第一次写入流时,即使下次写入前一条消息时刷新流,它仍然存在。”ByteArrayOutputStream bas=新的ByteArrayOutputStream(缓冲区大小);while((bytesRead=in.read(bytes))>0{baos.flush();baos.write(bytes,0,BUFFER_SIZE);out.println(“response”+baos.toString(“UTF-8”);System.out.println(baos.toString(“UTF-8”);}
bas.write(bytes,0,BUFFER_SIZE)在收到完整消息时清除缓冲区,只写入与收到的字节数相同的字节数。即
baos.write(字节,0,字节读取)如果消息完全放入缓冲区,那么它可能不会在2个或更多的读取周期中传播。如果您可以确定情况始终如此,只需在每个循环中清除ByteBuffer。循环条件应为
。只有当缓冲区长度为零时,才能得到零,而这不是要循环使用的条件:它不会到达任何地方。只是一个bug。谢谢它工作了。我使用了ByteArrayOutputStream,它不再延迟了。但现在的问题是,当我第一次写入流时,即使下次写入前一条消息时刷新流,它仍然存在。”ByteArrayOutputStream bas=新的ByteArrayOutputStream(缓冲区大小);while((bytesRead=in.read(bytes))>0{baos.flush();baos.write(bytes,0,BUFFER_SIZE);out.println(“response”+baos.toString(“UTF-8”);System.out.println(baos.toString(“UTF-8”);}
bas.write(bytes,0,BUFFER_SIZE)在收到完整消息时清除缓冲区,只写入与收到的字节数相同的字节数。即
baos.write(字节,0,字节读取)如果消息完全放入缓冲区,那么它可能不会在2个或更多的读取周期中传播。如果你能确定情况总是如此,只需在每个循环中清除ByteBuffer。