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_Networking_Send - Fatal编程技术网

Java网络客户端服务器(文件加密),卡在客户端接收循环中

Java网络客户端服务器(文件加密),卡在客户端接收循环中,java,file,networking,send,Java,File,Networking,Send,我尝试制作一个简单的客户机服务器(带有一些gui),服务器发送数据,客户机接收数据(保存文件也可以),但客户机似乎在接收循环中受阻。 我用“.start()”启动了两个线程。 我已经用粗体的笔调表明了问题的立场 我的朋友,你们中的一些人知道为什么客户端中的程序不能继续…-祝加利一切顺利 服务器: public class Server extends Thread implements Runnable{ public File choosenfile; public int

我尝试制作一个简单的客户机服务器(带有一些gui),服务器发送数据,客户机接收数据(保存文件也可以),但客户机似乎在接收循环中受阻。 我用“.start()”启动了两个线程。 我已经用粗体的笔调表明了问题的立场

我的朋友,你们中的一些人知道为什么客户端中的程序不能继续…-祝加利一切顺利

服务器:

public class Server extends Thread implements Runnable{

    public File choosenfile;
    public int port = 42001;
    public boolean running = true;
    public String myAdress = "localhost";
    public ServerSocket serverSocket;
    public BufferedInputStream bis;
    public boolean debug = true;

    public OutputStream os;


    public void run() {


        try {
            serverSocket = new ServerSocket(port);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        while(running){

            Socket socket = null;


            try {
                System.out.println("Listening on port: "+port);
                socket = serverSocket.accept();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            /*
             * Dies ist die stelle an der man einen Thread aufmachen kann
             */


            // 1. File-Hülle anlegen dazu muss man deren größere wissen
            if(debug) System.out.println("Server: 1.");



            int count;
            byte[] buffer = new byte[1024];

            try {

                os = socket.getOutputStream();

            } catch (IOException e1) {
                e1.printStackTrace();
            }


            BufferedInputStream in = null;

            try {

                in = new BufferedInputStream(new FileInputStream(choosenfile));

            } catch (FileNotFoundException e1) {
                e1.printStackTrace();
            }

            try {
                    while ((count = in.read(buffer)) > 0) {
                         os.write(buffer, 0, count);
                         os.flush();
                    }

            } catch (IOException e1) {
                e1.printStackTrace();
            }


             if(debug) System.out.println("Server: finish sending");

              try {
                os.flush();
                 if(debug) System.out.println("Server: close");
                serverSocket.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            running =false;
        }

    }

}
客户:

public class Client extends Thread implements Runnable{
    public Gui gui; //graphical surface
    public boolean running = true;
    public boolean debug =true;

    //Networkstuff

    Socket socket;
    DataInputStream is;
    public byte[] returnFile;

    public Client(Gui gui){
        this.gui = gui;

    }

    public void run() {


        try {
            socket = new               Socket(InetAddress.getByName(gui.inetAdress),gui.portServer);
        } catch (UnknownHostException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }


        FileOutputStream fos = null;

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

        BufferedOutputStream out = new BufferedOutputStream(fos);
        byte[] buffer = new byte[1024];

        int count;
        InputStream in = null;

        try {

            in = socket.getInputStream();
            int i =0;

        }
        catch (IOException e1) {
        e1.printStackTrace();
        }


            try {
                while((count=in.read(buffer)) >0){
                    fos.write(buffer, 0, count);

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



        **//folloing functions are not executed anymore and i dont know why**
        gui.loadPicture();



        try {
            fos.close();
            socket.close();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

    }

}

这很简单<代码>读取()阻塞直到

  • 数据流中有可用数据,或
  • 溪流关闭了
服务器向流发送字节,但从不关闭它,因此从该流读取的客户端无法知道是否还会有更多数据,因此它会阻塞


关闭服务器端的流,或者设计一个协议,允许客户端知道何时停止读取(例如,发送文件中的字节数,然后发送这些字节)。

根据代码,服务器应该关闭流,但客户端只是没有检测到itI。我看不到任何对
os.Close()的调用
或服务器端的to
socket.close()
。关闭ServerSocket不也会关闭它的套接字吗?javadoc没有说它会关闭,您的实验表明它不会。然后,听起来这可能是您的答案。试试看,让我们知道