我应该关闭tcp连接吗?

时间:2018-01-23 18:47:20

标签: java performance sockets network-programming tcpclient

我有一个Java打印应用程序,它使用原始TCP套接字将票证打印到网络打印机。此应用程序是REST侦听器,当它收到命令时,它使用一个连接打印到单个以太网热敏打印机。

这是我的单例服务使用的代码:

public class TcpPrinterWrapper implements PrinterWrapper {

    private static Logger log = LoggerFactory.getLogger(TcpPrinterWrapper.class);

    private PrinterOpzioni opzioni;
    private TcpWrapper wrapper;


    public TcpPrinterWrapper(PrinterOpzioni opzioni) {
        super();
        this.opzioni = opzioni;
        wrapper=new TcpWrapper();
    }



    @Override
    protected void finalize() throws Throwable {
        if(wrapper!=null) try{wrapper.close();}catch (Exception e) {}
        super.finalize();
    }


    private void openPort(){
        try {
            if(wrapper==null)
                wrapper=new TcpWrapper();
            wrapper.openPort(opzioni, opzioni.getTcpTimeout());
        } catch (Exception e) {
            log.error("Errore apertura porta",e);
            throw new HardwareException("Porta stampante non valida o non disponibile");
        }
    }

    @Override
    public synchronized void print(byte[] content, boolean close) throws Exception {
        try{
            if(wrapper==null || !wrapper.isOpened()){
                openPort();
            }
            wrapper.write(content);
            if(close)
                close();
        }catch(Exception e){
            try{
                wrapper.close();
            }catch(Exception e1){}
            throw e;
        }
    }

    @Override
    public void close(){
        try {
            wrapper.close();
            wrapper=null;
        } catch (Exception e) {}
    }

}
  

我的问题是:它应该在每次打印结束时关闭它的套接字   工作?或者,TCP套接字是否可以在等待作业的情况下保持打开状态?

我不知道REST请求频率,但我确信它们可以等待很多个小时,但也可以在一分钟内调用很多。这就是为什么我无法回答,可以等待这么长时间的套接字保持打开?另一方面,我可以在几分钟内打开/关闭/打开/关闭许多插座吗?我应该使用断开计时器复杂化吗?

2 个答案:

答案 0 :(得分:1)

You're not using raw Sockets (it is not possible using existing Java's network standard package; you'll need to use native calls with the help of JNI for utilising raw Sockets).

What you're using in your code seems to be a PrintServer having TCPSocket extending from the Java's Standard Package's TCP Socket. And, there's too much of code which has not been shared.

My question is: should it close its socket at the end of each print job? Or can a TCP socket stay opened for many hours waiting for jobs?

Ideally, each application should have process timeout, or release the resource after a certain period of time. It is the general principle of security (and best practices) to close sessions in case of request-waiting situations. By process, I mean the underlying resource if not being used should close the connection or/and give a timeout (depending on the application's use-case).

So, yes, if the class TcpWrapper extends the class Socket, you should set the connect timeout as well as the read timeout. The most important thing is: "Once the operation is done, you should close the Socket at both the client and the Server end".

You can utilise Socket pools to manage multiple connections for your print application as mentioned in the post Manage Client Socket Pool.

答案 1 :(得分:1)

这还取决于打印机。它会打印出收到的每一个角色吗?当它收到一条完整的线路时,它会累积线条吗?还是等待整个页面?还是为了完整的工作?在这种情况下,根据打印机协议,您可能必须关闭打印机的插槽才能意识到这是打印作业的结束。

另一方面:在许多企业环境中,防火墙会切断在某些(可配置)时段内处于非活动状态的套接字。在这种情况下,最好在作业结束时关闭套接字。

最后,最终确定无用。查一查。