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

时间:2016-03-10 19:59:52

标签: java sockets ioexception

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

我发送文件后收到IOException,所以我的客户端没有读取额外的字符串数据.WYYYYYYYY?

这是服务器代码

 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异常?

2 个答案:

答案 0 :(得分:2)

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

您的服务器程序正在发送一个mp3文件,然后打印发送的消息文件,再次通过套接字写入数据,然后由于没有任何事情要做,它终止/结束,因此服务器套接字关闭,这反过来导致IO异常在客户端程序中。

答案 1 :(得分:0)

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

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