java套接字连接重置

时间:2017-02-20 20:54:27

标签: java sockets

我正在尝试制作简单的服务器客户端应用程序,让客户端将文件发送到服务器,然后服务器发送收到文件的确认消息

这是服务器类。

public class Server {

public static void main(String[] args) {
    try {
        ServerSocket server=new ServerSocket(9090);
        while(true){
            Socket clientSocket=server.accept();
            ClientThread client=new ClientThread(clientSocket);
            client.start();
        }
    } catch (IOException e) {
        System.out.println(e.toString());
    }

}

}

ClientThread类

public class ClientThread extends Thread {

private Socket socket;// socket to connect through
private BufferedInputStream SocketInput;// stream to read from socket
private BufferedOutputStream FileOutput;// stream to write file through it
private PrintWriter SocketOutput; // print writer to write through socket

public ClientThread(Socket socket){// constructor
    this.socket=socket;
}

public void run(){
    try {
        //get socket input stream
        SocketInput=new BufferedInputStream(socket.getInputStream());

        //get socket output stream
        SocketOutput=new PrintWriter(socket.getOutputStream(),true);

        // get the file from client
        int NBytes=0;
        byte[] data=new byte[1024];

        FileOutput=new BufferedOutputStream(new   FileOutputStream("C:/Users/mohamed/Desktop/Download/band.jpg"));      
        while((NBytes=SocketInput.read(data))!=-1){
            FileOutput.write(data,0,NBytes);
            FileOutput.flush();
        }

        // send ack. that file has been received
        SocketOutput.println("File has been sent sucessfully");

        //close all streams
        FileOutput.close();
        SocketInput.close();
        SocketOutput.close();
        socket.close();

    }
    catch (IOException e) {
        System.out.println(e.toString());
    }
}
}

客户端类

public class Client {

public static void main(String[] args) {

    // file will be sent
    File f=new File("D:/images/band.jpg");

    try {

        // get address of local host
        InetAddress serverAddress=InetAddress.getLocalHost();

        //create socket
        Socket socket=new Socket(serverAddress,9090);

        // create streams
        BufferedInputStream input=new BufferedInputStream(new FileInputStream(f));
        BufferedOutputStream output=new BufferedOutputStream(socket.getOutputStream());
        BufferedReader p=new BufferedReader(new InputStreamReader(socket.getInputStream()));

        // send the file
        byte[] data=new byte[1024];
        int Nbytes=0;

        while((Nbytes=input.read(data))!=-1){
            output.write(data,0,Nbytes);
            output.flush();
        }

        // read the ack. and print it
        System.out.println(p.readLine());

        //close streams & socket
        p.close();
        input.close();
        output.close();
        socket.close();

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

当我运行服务器和客户端我得到什么时,我得到什么,如果我停止客户端服务器应用程序键入控制台异常连接重置,但如果我删除确认的一部分文件被收到,没有任何进展错了,有什么问题?

2 个答案:

答案 0 :(得分:0)

你陷入僵局。客户端正在读取套接字到流的末尾,而你还没有关闭套接字,所以没有流的结束,所以当你去读取ACK线时,两个对等端都在读。

您不需要ACK线。只需删除它。不要发送它,也不要读它。

答案 1 :(得分:-1)

因为如果你有像这样的连接方代码

    while((Nbytes=input.read(data))!=-1){
        output.write(data,0,Nbytes);
        output.flush();
    }

    // read the ack. and print it
    System.out.println(p.readLine());

您将在最后一行上阻止并等待输入。但是因为套接字仍然打开你的阅读旁边代码

   while((NBytes=SocketInput.read(data))!=-1){ <-- HERE U ARE BLOCKED
        FileOutput.write(data,0,NBytes);
        FileOutput.flush();
    }

将阻止while循环条件。在连接有效且输入未关闭之前,#read将不会返回,因此您将被阻止。

尝试在传输所有内容后关闭输出流

    while((Nbytes=input.read(data))!=-1){ 
        output.write(data,0,Nbytes);
        output.flush();
    }
    output.close(); // this should unblock blocked thread on the other side
    // read the ack. and print it
    System.out.println(p.readLine());

这应该有效地告诉对方没有其他数据将被传输(阻塞读取方法将返回-1)