Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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套接字。服务器如何知道客户端关闭流_Java_File_Sockets_Stream - Fatal编程技术网

java套接字。服务器如何知道客户端关闭流

java套接字。服务器如何知道客户端关闭流,java,file,sockets,stream,Java,File,Sockets,Stream,我想用java编写一个传输文件套接字程序。 但我有个问题,在传输文件时如何取消 当我在客户端关闭inputstream时,服务器如何知道它关闭了outputstream 这是我的密码: 客户 服务器 BufferedInputStream bis = null; bis = new BufferedInputStream(new FileInputStream( myFile

我想用java编写一个传输文件套接字程序。 但我有个问题,在传输文件时如何取消

当我在客户端关闭inputstream时,服务器如何知道它关闭了outputstream

这是我的密码: 客户

服务器

           BufferedInputStream bis = null;

                    bis = new BufferedInputStream(new FileInputStream(
                            myFile));

                    bis.read(mybytearray, 0, mybytearray.length);
                    os = s.getOutputStream();
                    int total = 0;

                    try {

                        os.write(mybytearray, 0, mybytearray.length);

                        // os.flush();
                    }
                    catch(SocketException e){
                        os.flush();
                        tv.setText("aaaaa");
                    }


但是,当我关闭输入流

时,在传输过程中,客户端关闭<代码>输入流>代码>时,服务器端将抛出<代码> SokExtExabor >代码> .< /p>

当客户端在传输中途关闭<代码>输入流< /代码>时,服务器端将抛出一个

SocketException

您可以在try-catch块中执行此操作。 伪代码:

try{
    //your file transferring code here through socket output streams
} catch(SocketException e) {
    /* now as you are in this block you got a socket closed 
      Exception and your server now knows about it */
}

您可以在try-catch块中执行此操作。 伪代码:

try{
    //your file transferring code here through socket output streams
} catch(SocketException e) {
    /* now as you are in this block you got a socket closed 
      Exception and your server now knows about it */
}


最终要做到这一点。这可能需要几分钟的时间,具体取决于客户端套接字消失的具体情况。它将抛出一个
IOException
“连接重置”。不是
SocketException
@EJP仅供参考,
SocketException
IOException
的子类。实际上,如果你试图记录抛出的异常,它总是会准确地告诉你它是一个
SocketException
,而不是它的超类。@shinkou,“仅供参考”,抛出的异常是一个
IOException,
而不是
SocketException。
@sarnold
FIN
没有异常:,
read()
返回-1。你不能让ICMP在读取时不读。最终要做到这一点。这可能需要几分钟的时间,具体取决于客户端套接字消失的具体情况。它将抛出一个
IOException
“连接重置”。不是
SocketException
@EJP仅供参考,
SocketException
IOException
的子类。实际上,如果你试图记录抛出的异常,它总是会准确地告诉你它是一个
SocketException
,而不是它的超类。@shinkou,“仅供参考”,抛出的异常是一个
IOException,
而不是
SocketException。
@sarnold
FIN
没有异常:,
read()
返回-1。您无法在读取时获取ICMP未读。您能看到我上面的代码吗,我执行了这个try-catch块,但没有显示任何内容。我尝试了,但仍然无法工作。你能给我看看示例代码吗?请。
SocketClosed
当您关闭套接字并继续使用它时,将引发异常。当对等方关闭连接时不会。你能看到我上面的代码吗,我执行了这个try-catch块,但没有显示任何内容。我尝试了,但仍然无法工作。你能给我看看示例代码吗?请。
SocketClosed
当您关闭套接字并继续使用它时,将引发异常。当对等方关闭连接时不会。如果对您的项目有意义,我认为您应该签出ZeroMQ:我认为您需要一个循环来读取和发送,直到整个文件得到处理。如果对您的项目有意义,我认为您应该签出ZeroMQ:我认为您需要一个循环来读取和发送,直到整个文件得到处理。