开发者

problem with server and client , working on my machine not working on another

开发者 https://www.devze.com 2023-03-08 08:23 出处:网络
hello guys i have designed a server client which transfers data through sockets. Everything is ok when i run it on my machine it works 100% of the time. When i run the server on another machine and th

hello guys i have designed a server client which transfers data through sockets. Everything is ok when i run it on my machine it works 100% of the time. When i run the server on another machine and the client on mine, i am unable to get data. when i run the server on my machine and the client on theirs i am unable to put data , but i can get. i dont know what is going on, maybe you can shed some light.

There is more code that makes this work correctly but i omit that out to reduce the complications . Please if you get a chanse look at this and tell me why it works on my system but not on the server? and does anyone know how to debug this? i mean this is run on the server how can i debug a server since i cannot be there (and everything 开发者_如何学运维works correctly on my system?)

Server :

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";

The copy class:

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

The client class:

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号