【问题标题】:Java network client-server (file sneding), stuck in client receive loopJava网络客户端-服务器(文件sneding),卡在客户端接收循环中
【发布时间】:2014-07-21 05:15:16
【问题描述】:

我尝试制作一个简单的客户端-服务器(带有一些 gui)服务器发送数据,客户端接收它(文件的保存也可以),但客户端似乎卡在接收循环中。 我用“.start()”启动了两个线程。 我已经用粗体标记了问题的位置。

你们中的一些人知道为什么客户端中的程序不继续... - 最好的祝福 ghali

服务器:

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();
        }

    }

}

【问题讨论】:

    标签: java file networking send


    【解决方案1】:

    这很简单。 read() 阻塞直到

    • 数据流中可用,或
    • 流已关闭

    服务器向流发送字节,但从不关闭它,因此从该流读取的客户端无法知道是否会有更多数据到来,因此它会阻塞。

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

    【讨论】:

    • 根据代码,服务器应该正在关闭流,但客户端只是没有检测到它
    • 我在服务器端没有看到对os.close()socket.close() 的任何调用。
    • 关闭ServerSocket不也关闭它的Sockets吗?
    • javadoc 没有说它有,你的实验表明它没有。
    • 那么,听起来这可能就是你的答案。试试看,让我们知道
    猜你喜欢
    • 2012-04-26
    • 1970-01-01
    • 2011-02-10
    • 2013-07-11
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    相关资源
    最近更新 更多