Problem with server and client , working on my machine not working on another?

Up vote 1 down vote favorite share g+ share fb share tw.

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 don't 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 bytelength;//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 byte8192; 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 bytelength; 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); java server client link|improve this question asked May 25 '11 at 0:08Martinos576 89% accept rate.

– entonio May 25 '11 at 0:25 Before looking at your code, I'd look at whether there is a firewall issue. – Ted Hopp May 25 '11 at 0:25 well it could be very possible nevertheless why would either get or put work on my local system byt not on the remote? – Martinos May 25 '11 at 0:38 You should work on your variable and method names.

InFromClient and inToClient are variables I would expect on the server, but not on the client. – Pa? Lo Ebermann May 25 '11 at 1:04 In your copy method you should either use readFully instead of read or better a loop with alternating writing and reading.

– Pa? Lo Ebermann May 25 '11 at 1:05.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions