Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.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_Client - Fatal编程技术网

Java 服务器和客户端出现问题,在我的机器上工作而不在其他机器上工作

Java 服务器和客户端出现问题,在我的机器上工作而不在其他机器上工作,java,client,Java,Client,大家好,我设计了一个通过套接字传输数据的服务器客户端。当我在我的机器上运行它时,一切都正常,它100%的时间都在工作。当我在另一台机器上运行服务器,在我的机器上运行客户端时,我无法获取数据。当我在我的机器上运行服务器,在他们的机器上运行客户端时,我无法放置数据,但我可以获取数据。我不知道发生了什么事,也许你能解释一下 有更多的代码可以使这项工作正常进行,但为了减少复杂性,我省略了这些代码。如果你有机会,请看看这个,告诉我为什么它可以在我的系统上工作,但不能在服务器上工作?有人知道如何调试这个吗?

大家好,我设计了一个通过套接字传输数据的服务器客户端。当我在我的机器上运行它时,一切都正常,它100%的时间都在工作。当我在另一台机器上运行服务器,在我的机器上运行客户端时,我无法获取数据。当我在我的机器上运行服务器,在他们的机器上运行客户端时,我无法放置数据,但我可以获取数据。我不知道发生了什么事,也许你能解释一下

有更多的代码可以使这项工作正常进行,但为了减少复杂性,我省略了这些代码。如果你有机会,请看看这个,告诉我为什么它可以在我的系统上工作,但不能在服务器上工作?有人知道如何调试这个吗?我的意思是,这是在服务器上运行的。既然我不能在那里(而且我的系统上所有的东西都正常工作),我如何调试服务器

服务器:

if (get.equals("get")) {
                try {

                    Copy copy = new Copy(socket, dir);//maybe dir is not needed
                    String name = input.substring(4);
                    File checkFile = new File(dir.getCurrentPath(), name);
                    DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream());

                    if (checkFile.isFile() && checkFile.exists()) {
                        outToClient.writeBytes("continue" + "\n");

                        BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
                                socket.getInputStream()));
                        boolean cont = false;
                        String x;
                        while (!cont) {

                            if ((x = inFromServer.readLine()).equals("continue")) {
                                cont = true;

                            }
                        }
                        copy.copyFile(name);
                        output = "File copied to client successfully" + "\n";
                    } else {
                        outToClient.writeBytes("File failed to be copied to client" + "\n");
                        output = "";
                    }
                } catch (Exception e) {
                    output = "Failed to Copy File to client" + "\n";
                }

            } else if (get.equals("put")) {
                //so the client sends: the put request
                //then sends the length 
                try {
                    DataInputStream inFromClient = new DataInputStream(socket.getInputStream());
                    DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream());
                    outToClient.writeBytes("continue" + "\n");
                    long lengthLong = (inFromClient.readLong());
                    int length = (int) lengthLong;

                    byte[] recieveFile = new byte[length];//FIX THE LENGTH
                    //                      InputStream is = socket.getInputStream();
                    FileOutputStream fos = new FileOutputStream("Copy " + input.substring(4));
                    BufferedOutputStream bos = new BufferedOutputStream(fos);
                    int bytesRead;
                    int current = 0;
                    bytesRead = inFromClient.read(recieveFile, 0, recieveFile.length);
                    current = bytesRead;

                    do {
                        bytesRead = inFromClient.read(recieveFile, current, (recieveFile.length - current));
                        if (bytesRead >= 0)
                            current += bytesRead;
                    } while (bytesRead > 0); // FIX THE LENGTH

                    bos.write(recieveFile, 0, current);
                    bos.flush();
                    bos.close();
                    output = "File copied to Server successfully" + " \n";
复制类:

File checkFile = new File(dir.getCurrentPath(), file);
    if (checkFile.isFile() && checkFile.exists()) {

        DataOutputStream outToClient = new DataOutputStream(socket.getOutputStream());
        //              byte[] receivedData = new byte[8192];
        File inputFile = new File(dir.getCurrentPath(), file);
        byte[] receivedData = new byte[(int) inputFile.length()];
        long length = inputFile.length();
        outToClient.writeLong(length);
        //maybe wait here for get request?
        DataInputStream dis = new DataInputStream(new FileInputStream(getCopyPath(file)));

        dis.read(receivedData, 0, receivedData.length);
        OutputStream os = socket.getOutputStream();
        outToClient.write(receivedData, 0, receivedData.length);//outputStreasm replaced by Datatoutputstream
        outToClient.flush();
客户端类:

else if (sentence.length() > 3 && sentence.substring(0, 3).equals("get")) {
                    outToServer.writeBytes(sentence + "\n");

                    String response = inFromServer.readLine();
                    if (response.equals("File failed to be copied to client")) {
                        System.out.println(response);
                    } else {
                        DataInputStream inFromClient = new DataInputStream(clientSocket.getInputStream());
                        DataOutputStream outToClient = new DataOutputStream(clientSocket.getOutputStream());
                        outToClient.writeBytes("continue" + "\n");
                        long lengthLong = (inFromClient.readLong());
                        int length = (int) lengthLong;
                        byte[] recieveFile = new byte[length];
                        FileOutputStream fos = new FileOutputStream("Copy " + sentence.substring(4));
                        BufferedOutputStream bos = new BufferedOutputStream(fos);
                        int bytesRead;
                        int current = 0;
                        bytesRead = inFromClient.read(recieveFile, 0, recieveFile.length);
                        current = bytesRead;
                        do {
                            bytesRead = inFromClient.read(recieveFile, current, (recieveFile.length - current));
                            if (bytesRead >= 0)
                                current += bytesRead;
                        } while (bytesRead > 0);
                        bos.write(recieveFile, 0, current);
                        bos.flush();
                        bos.close();

                    }

                } else if (sentence.length() > 3 && sentence.substring(0, 3).equals("put")) {

                    File checkFile = new File(dir.getCurrentPath(), sentence.substring(4));
                    if (checkFile.isFile() && checkFile.exists()) {

                        try {
                            outToServer.writeBytes(sentence + "\n");
                            boolean cont = false;
                            String x;
                            while (!cont) {
                                if ((x = inFromServer.readLine()).equals("continue")) {
                                    cont = true;

                                }
                            }
                            String name = sentence.substring(4);
                            copy.copyFile(name);

这可能是另一台计算机上的防火墙限制吗?在查看您的代码之前,我会查看是否存在防火墙问题。很可能是这样的,但是为什么要在本地系统上而不是远程系统上获取或放置工作?您应该处理变量和方法名称
INFOROMCLIENT
inToClient
是我希望在服务器上使用的变量,但在客户端上不是。在复制方法中,您应该使用
readFully
而不是
read
,或者使用交替写入和读取的循环。