【问题标题】:Java socket inpustream stuckJava套接字输入流卡住
【发布时间】:2012-12-27 11:01:46
【问题描述】:

我从套接字 inputStream 读取时遇到问题。但只在课堂上使用一种方法。这是我的代码: 服务器:

private void copyFile(String mainPath, String path) {
    outS.println("Sending file!");
    outS.println(path);
    outS.flush();

    BufferedInputStream fis = null;
    OutputStream os;

    File file = new File(mainPath);
    String str;
    byte[] buffer = new byte[4096];
    long numOfChunks = file.length() / 4096, num = 0, lng;
    try {
        fis = new BufferedInputStream(new FileInputStream(file));
        os = socket.getOutputStream();
        outS.println(numOfChunks);
        fis.read(buffer, 0, 4096);
        os.write(buffer, 0, buffer.length);
        os.flush();
        num++;
    } catch (FileNotFoundException ex) {
        System.out.println("<ERROR> Clerk, copyFile: File not found.");
        System.out.println(ex);
    } catch (IOException ex) {
        System.out.println("<ERROR> Clerk, copyFile: Could not write or read file.");
        System.out.println(ex);
    } finally {
        try {
            fis.close();
        } catch (IOException ex) {
            Logger.getLogger(Clerk.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    waitEnd();
}

客户:

private void copyFile(String destination) {
    FileOutputStream fos = null;
    long numOfChunks, num = 0;
    SBuffer sBuffer;
    byte[] buffer = new byte[4096];
    InputStream is;
    try {
        fos = new FileOutputStream(new File(destination));
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        is = socket.getInputStream();
        numOfChunks = Long.parseLong(inS.readLine());
        System.out.println(num + "/" + numOfChunks);
        int bytesRead = is.read(buffer, 0, buffer.length);
        bos.write(buffer, 0, bytesRead);
        num++;
    } catch (FileNotFoundException ex) {
        System.out.println("<ERROR> Client, copyFile: File not found.");
        System.out.println(ex);
    } catch (IOException ex) {
        System.out.println("<ERROR> Client, copyFile: Could not write or read file.");
        System.out.println(ex);
    } finally {
        try {
            fos.close();
        } catch (IOException ex) {
            Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    outS.println("end");
    outS.flush();
}

让我解释几件事。一切正常,但在线客户端int bytesRead = is.read(buffer, 0, buffer.length) 会卡住,因为流中没有任何内容(is.available = 0)。我不知道为什么。虽然我多次从服务器发送它。

而且我不能关闭流,因为套接字也会关闭。

方法 waitEnd() 等待套接字输入流中的字符串“end”。

我在网上搜索了很多教程和东西,但没有人帮助。

建立连接的代码:

服务器:

public void run() {
    try {
        ssocket = new ServerSocket(2332);
        Socket socket = ssocket.accept();
        clerk = new Clerk(socket, mainPath);
        (new Thread(clerk)).start();
    } catch (IOException ex) {
    }
    try {
        ssocket.close();
    } catch (IOException ex) {
    }
}


public Clerk(Socket socket, String path) {
    this.socket = socket;
    mainTree = new FileTree(path);
}

public Client(String address, String[] dirs) {
    try {
        socket = new Socket(address, 2332);
    } catch (UnknownHostException ex) {
    } catch (IOException ex) {
    }
}

客户和店员有相同的方法运行:

    @Override
public void run() {
    try {
        inS = new BufferedReader(iss);
        outS = new PrintWriter(socket.getOutputStream(), true);
        oos = new ObjectOutputStream(socket.getOutputStream());
        iss = new InputStreamReader(socket.getInputStream());
        ois = new ObjectInputStream(socket.getInputStream());
    } catch (IOException ex) {
    }
    msg();
    try {
        inS.close();
        iss.close();
        outS.close();
        ois.close();
        oos.close();
        socket.close();
    } catch (IOException ex) {
    }
}

【问题讨论】:

  • 能否添加建立连接的代码?
  • 我可以,但时间会长一点。但在这种情况下并没有错。

标签: java sockets


【解决方案1】:

已解决。我创建了第二个套接字仅用于文件传输。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 2016-02-11
    • 2013-07-08
    相关资源
    最近更新 更多