проблема с сервером и клиентом, работающая на моей машине, не работающая на другой - PullRequest
1 голос
/ 25 мая 2011

Привет, ребята. Я разработал серверный клиент, который передает данные через сокеты. Все нормально, когда я запускаю его на своей машине, он работает 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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...