Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.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 从服务器接收文件后触发IOException的原因(套接字编程)_Java_Sockets_Ioexception - Fatal编程技术网

Java 从服务器接收文件后触发IOException的原因(套接字编程)

Java 从服务器接收文件后触发IOException的原因(套接字编程),java,sockets,ioexception,Java,Sockets,Ioexception,这就是我正在研究的问题。 1) 有一台服务器和一台客户机。 2) 服务器与客户端连接。 3) 服务器发送一个文件(.mp3)。 4) 发送文件后,它会发送一些额外的字符串数据 文件发送后我收到IOException,因此我客户端没有读取其他字符串数据。WHYYYYYYYY 这是服务器代码 public class server { ServerSocket server; Socket socket; FileInputStream fis; FileOutputStream fos; Da

这就是我正在研究的问题。 1) 有一台服务器和一台客户机。 2) 服务器与客户端连接。 3) 服务器发送一个文件(.mp3)。 4) 发送文件后,它会发送一些额外的字符串数据

文件发送后我收到IOException,因此我客户端没有读取其他字符串数据。WHYYYYYYYY

这是服务器代码

 public class server {

ServerSocket server;
Socket socket;
FileInputStream fis;
FileOutputStream fos;
DataInputStream dis;
DataOutputStream dos;

public static void main(String args[])
{       
    server s=new server();
    s.connnect();
    s.init();
    s.send("f://song.mp3");     
}

public void connnect()
{
    try {
        server=new ServerSocket(8080);
        socket =server.accept();
    } catch (IOException e) {

    }       
}

public void init()
{
    try {
        dis=new DataInputStream(socket.getInputStream());
        dos=new DataOutputStream(socket.getOutputStream());
    } catch (IOException e) {
        e.printStackTrace();
    }       
}

public void send(String name)
{
    File f=new File(name);
    try {
        fis=new FileInputStream(f);
    } catch (FileNotFoundException e) {

        e.printStackTrace();
    }

    String nam=f.getName();

    try {
        dos.writeUTF(nam);
        System.out.println("header sent");

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    byte[] buffer=new byte[1024];

    int read=-1;

    try {
        while((read=fis.read(buffer))!=-1)
        {
            dos.write(buffer, 0, read);
        }

        System.out.println("file "+name+" sent");

        dos.writeUTF("this is the msg which is not received by client because of IOexception in client side");

    } 
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }               
}
}

客户端代码

 public class client {


ServerSocket server;
Socket socket;
FileInputStream fis;
FileOutputStream fos;
DataInputStream dis;
DataOutputStream dos;

public static void main(String args[])
{
    client s=new client();
    s.connect();
    s.init();
    try {           
    s.rec(s.dis.readUTF());      ///this the name of the String         
    } catch (IOException e) {

    }       
}

public void connect()
{
    try {
        socket=new Socket("127.0.0.1",8080);
        System.out.println("connected with server");
    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}

public void init()
{
    try {
        dis=new DataInputStream(socket.getInputStream());
        dos=new DataOutputStream(socket.getOutputStream());
    } catch (IOException e) {
        System.out.println("IO EXception in streams");
        e.printStackTrace();
    }       
}

public void rec(String filename)
{
    File f=new File("f://abc");
    f.mkdirs();     
    File temp=new File(f,filename);     
    try {
        fos=new FileOutputStream(temp);
    } catch (FileNotFoundException e) {

        e.printStackTrace();
    }

    byte[] buffer=new byte[1024];
    int read=-1;

    System.out.println("started reading file"+filename);

    try {           
        while((read=dis.read(buffer))!=-1)
        {
            fos.write(buffer, 0, read);
        }

        String FINAL_MSG=dis.readUTF();
        System.out.println("ended "+FINAL_MSG);

    } 
    catch (IOException e) {
        System.out.println("why IO exception?");
    }       
}

public void sendFile(String name)
{
    File f=new File("f://abc");
    f.mkdirs();

    File temp=new File(f,name);

    try {
        fos=new FileOutputStream(temp);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    byte[] buffer=new byte[1024];
    int read=-1;

    try {
        while((read=dis.read(buffer))!=-1)
        {
            fos.write(buffer, 0, read);
        }
    } 
    catch(SocketException e)
    {
        System.out.println(name+" has end"+" "+(socket==null));

    }
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}   
}

我想读取额外的字符串数据(FINAL_MSG)。这怎么可能

服务器输出

header sent     
file f://song.mp3 sent       
Final MSG is sent from server side 
客户端输出

connected with server    
started reading filesong.mp3     

java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at java.io.DataOutputStream.write(Unknown Source)
at s1.send(s1.java:87) at s1.main(s1.java:30)

为什么IO异常?

在客户端读取导致IO异常的所有数据之前,服务器程序已终止


您的服务器程序正在发送一个mp3文件,然后打印发送的消息文件,再次通过套接字写入数据,然后由于无需执行任何操作,它被终止/结束,因此服务器套接字被关闭,从而导致客户端程序出现IO异常。

当您仍在写入时,对等方已经关闭了连接。换句话说,应用程序协议错误


在这种情况下,服务器没有关闭接受的套接字,只是退出线程。

添加IOException的完整堆栈跟踪。不,我的问题是为什么它会转到catch(IOException){System.out.println(“为什么IO异常”}@flkes it is is.mp3 file它在文件末尾给出-1。您的问题很可能是读取“文件数据”的客户端代码也会读取您的最终消息并将其保存到文件中。因此,当您尝试在客户端中读取最终消息时,您现在是“流结束”。正如@Quintium所说,您正在使用整个流。您需要发送一个控件来设置长度。发送一个在另一端读取的长值,这样您就只能读取文件的部分。然后以字符串形式完成流的其余部分。在客户端获取IOException这就是我在回答中提到的,因为r消失了,你在客户端/客户端程序中遇到IO异常。TCP就是这样工作的。你的服务器程序正在结束。我已经自己运行了你的代码。如果你想验证我说的话,为什么不在客户端程序中遇到异常的地方设置一个断点。你会看到在那个时候在客户端遇到异常服务器端未运行。我在while((read=dis.read(buffer))!=-1{fos.write(buffer,0,read);}和FINAL_MSG=dis.readUTF()之间遇到异常;@SumitRathi没有证据表明服务器正在结束。我们只知道它已关闭连接。@RohitSingh:请在Scanner sc=new Scanner(System.in);String end=sc.next()中添加以下行;调用send方法后服务器程序的main方法。您会注意到,添加这些行后不会出现任何IO异常,因为现在服务器正在等待一些输入。通过这种方式,您将能够验证我在回答中提到的内容,即您的服务器程序在客户端读取所有数据之前结束。验证后,请执行以下操作:垂直-1到+1:)我如何修复它?任何建议都可以调试应用程序。同伴不应该关闭,或者你不应该继续发送。