This question already has answers here:
Java multiple file transfer over socket
(3 answers)
Closed 5 years ago.
TL;DR: How do I send (with a single connection) file, its size and its name. All examples in the internet send a file alone.
Server:
public class Server {
private static int PORT = 6667;
private ServerSocket serverSocket;
public void run() throws IOException {
System.out.println("Opening server");
serverSocket = new ServerSocket(PORT);
while(true) {
try(Socket incomingSocket = serverSocket.accept()) {
System.out.println("Accepted connection: " + incomingSocket);
incomingSocket.setSoTimeout(2000); // Don't let scanner block the thread.
InputStream inputStream = incomingSocket.getInputStream();
Scanner scanner = new Scanner(inputStream);
String command = "";
if(scanner.hasNextLine())
command = scanner.nextLine();
if(command.equals("update")) {
File file = new File("abc.txt");
sendFile(incomingSocket, file);
}
else {
// ...
System.out.println("Another command");
}
}
}
}
private void sendFile(Socket socket, File file) throws IOException {
byte[] bytes = new byte[(int)file.length()];
FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
bufferedInputStream.read(bytes, 0, bytes.length);
OutputStream outputStream = socket.getOutputStream();
PrintWriter writer = new PrintWriter(outputStream, true);
writer.println(file.length());
writer.println(file.getName());
System.out.println("Sending " + file.getName() + "(" + bytes.length + " bytes) to " + socket);
outputStream.write(bytes, 0, bytes.length);
outputStream.flush();
System.out.println("File sent");
}
public void stopRunning() {
try {
serverSocket.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
Client:
public class Client {
private static String HOST = "localhost";
private static int PORT = 6667;
public void run() throws IOException {
Socket socket = new Socket(HOST, PORT);
System.out.println("Connecting...");
OutputStream outputStream = socket.getOutputStream();
PrintWriter writer = new PrintWriter(outputStream, true);
writer.println("update"); // Example command which will determine what server sends back
receiveFile(socket);
socket.close();
}
private void receiveFile(Socket socket) throws IOException {
InputStream inputStream = socket.getInputStream();
int size = 16384;
String name = "example.txt";
Scanner scanner = new Scanner(inputStream);
size = Integer.parseInt(scanner.next());
name = scanner.next();
FileOutputStream fileOutputStream = new FileOutputStream(name);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
byte[] buffer = new byte[size];
int bytesRead, totalRead = 0;
while ((bytesRead = inputStream.read(buffer, 0, buffer.length)) != -1) {
totalRead += bytesRead;
bufferedOutputStream.write(buffer, 0, bytesRead);
}
bufferedOutputStream.flush();
System.out.println("File " + name + " received. " + totalRead + " bytes read");
bufferedOutputStream.close();
fileOutputStream.close();
}
I want my server to send a file to the client. It should also include the file's name and its size. Name because it's quite important and the size because I don't want to make a hardcoded buffer with a huge size.
Tried it with the code above. The client's "scanner part"
Scanner scanner = new Scanner(inputStream);
size = Integer.parseInt(scanner.next());
name = scanner.next();
works just okay, but the file is not received. inputStream.read(buffer, 0, buffer.length) never reads the remaining bytes from the stream.
If i comment out the scanner part, the bytes are read correctly(size and name information + file itself)
So, the question is, how do I send it with a single connection? Or should I make 2 separate connections, in the first one asking for size and file name and sending the file in the second one?
Scanner is good for text-based work.
One way to do what you want is using DataInputStream and DataOutputStream. Only one connection is needed:
public void send(File file, OutputStream os) throws IOException {
DataOutputStream dos = new DataOutputStream(os);
// writing name
dos.writeUTF(file.getName());
// writing length
dos.writeLong(file.length());
// writing file content
... your write loop, write to dos
dos.flush();
}
public void receive(InputStream is) throws IOException {
DataInputStream dis = new DataInputStream(is);
String fileName = dis.readUTF();
long fileSize = dis.readLong();
// reading file content
... your read loop, read from dis
}
Related
I have a Raspberry pi 4 with Raspbian installed, and I have a computer with Windows 10 installed.I wrote two functions one send a file and the other one receive the file.
when I run this function that sends a file on the raspberry pi 4:
public static void sendFile(String fileName, String ip)
{
BufferedOutputStream outputStream = null;
PrintWriter writer = null;
BufferedReader reader = null;
FileInputStream filein = null;
File file = new File(fileName);
if (!file.exists())
{
System.out.println(fileName + " does not exist");
return;
}
try
{
Socket socket = new Socket(ip, port);
outputStream = new BufferedOutputStream(socket.getOutputStream());
writer = new PrintWriter(socket.getOutputStream());
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
filein = new FileInputStream(file);
long fileSize = file.length();
writer.println(fileName); // sending file name
writer.println(fileSize); // sending file size in bytes
writer.flush();
byte[] dataBuffer = new byte[1024];
int numberOfReadBytes = 0; // the number of read bytes for each read() function call
System.out.println("Entering the loop");
for(long i = 0; i < fileSize && numberOfReadBytes > -1;)
{
numberOfReadBytes = filein.read(dataBuffer); // read read() function returns the number of bytes tha has been assigned to the array or -1 if EOF(end of file) is reached
outputStream.write(dataBuffer, 0, numberOfReadBytes); // writing the bytes in dataBuffer from index 0 to index numberOfBytes
i += numberOfReadBytes;
}
outputStream.flush();
System.out.println(fileName + " sent to " + ip);
String status = reader.readLine();
System.out.println("Status: " + status + "\t file save successfully on the other machine.");
}
catch(IOException ioe)
{
System.err.println("Status: 0\n" + ioe.getMessage());
}
finally // closing streams
{
try
{
outputStream.close();
reader.close();
writer.close();
filein.close();
}
catch (IOException ioe)
{
System.err.println("Error closing the connection.");
}
}
}
it stops at this line Socket socket = new Socket(ip, port);
and this is the other function that runs on windows 10
public static void receiveFile()
{
// 1- read the file name
// 2- read the size of the file
// 3- read the file and write it
ServerSocket server = null;
Socket socket = null;
BufferedReader reader = null;
BufferedInputStream inputStream = null;
FileOutputStream fileout = null;
PrintWriter writer = null;
try
{
server = new ServerSocket(9999);
socket = server.accept();
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
inputStream = new BufferedInputStream(socket.getInputStream());
writer = new PrintWriter(socket.getOutputStream());
String fileName = reader.readLine(); // reading file name
long fileSize = Long.parseLong(reader.readLine()); // reading file size
System.out.println(fileSize);
// reading file data and write the data
File file = new File(fileName);
fileout = new FileOutputStream(file);
for (long i = 0; i < fileSize; ++i)
{
fileout.write(inputStream.read());
System.out.println(i);
}
fileout.flush();
fileout.close();
writer.println('1');
System.out.println("Status: 1");
System.out.println(fileName+ " is saved successfully");
}
catch (IOException ioe)
{
System.err.println("Status: 0");
System.err.println(ioe.getMessage());
}
finally
{
try
{
reader.close();
inputStream.close();
}
catch(IOException ioe)
{
System.err.println("Error closing connection\n" + ioe.getMessage());
}
}
}
I think windows 10 firewall blocks connection, but I am not sure.
It turns out it was the firewall blocking connection from the raspberry pi
After editing my code with y'all suggestions, as well as condensing the code to where i can pinpoint the line of code that is causing the problem.
Server code:`
public class Server2 {
public static void main(String args[]) throws Exception {
ServerSocket servsock = null;
Socket sock = null;
int SOCKET_PORT = 12362;
InputStream is = null;
FileInputStream fis = null;
BufferedInputStream bis = null;
OutputStream os = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
String FILE_TO_SEND = "SFileToBeSent.txt";
String FILE_TO_RECEIVED = "CFileReceived.txt";
int FILE_SIZE = 6022386;
try {
System.out.println("Server created.");
servsock = new ServerSocket(SOCKET_PORT); // creates servsock with socket port
while (true) {
System.out.println("Waiting for connection...");
try {
sock = servsock.accept(); // accepts a socket connection
System.out.println("Accepted connection : " + sock);
System.out.println();
os = sock.getOutputStream(); // sets Output Stream using the sockets output stream
is = sock.getInputStream(); // get input stream from socket
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
// send file
File myFile = new File(FILE_TO_SEND); // creates a file using file to send path
byte[] bytearraySent = new byte[(int) myFile.length()]; // creates a byte array the size of myFile
try {
// reads the contents of FILE_TO_SEND into a BufferedInputStream
fis = new FileInputStream(myFile); // creates new FIS from myFile
bis = new BufferedInputStream(fis); // creates BIS with the FIS
bis.read(bytearraySent, 0, bytearraySent.length); // copies the BIS byte array into the byte array
// prints to console filename and size
System.out.println("Sending " + FILE_TO_SEND + "(" + bytearraySent.length + " bytes)");
System.out.println("byte array from file to send:" + bytearraySent);
// copies the byte array into the output stream therefore sending it through the
// socket
os.write(bytearraySent, 0, bytearraySent.length);
os.flush(); // flush/clears the output stream
} finally {
fis.close();
bis.close();
}
System.out.println();
System.out.println("sendFile complete");
System.out.println();
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
// receive file
int bytesRead;
byte[] bytearrayReceived = new byte[FILE_SIZE]; // creates byte aray using file size
fos = new FileOutputStream(FILE_TO_RECEIVED); // creates file from path
bos = new BufferedOutputStream(fos); // creates BOS from FOS
int current = 0; // equals the two integers for comparisons later
try {
System.out.println(String.format("Bytes available from received file: %d", is.available()));
System.out.println("byte array from file to receive: " + bytearrayReceived); // debug purposes
while ((bytesRead = is.read(bytearrayReceived)) != -1) {
System.out.println("amount of bytes that was read for while: " + bytesRead);
bos.write(bytearrayReceived, 0, bytesRead);
System.out.println("bos.write");
current += bytesRead;
System.out.println("current += bytesRead;");
}
System.out.println("File " + FILE_TO_RECEIVED + " downloaded (" + current + " bytes read)");
} finally {
fos.close();
bos.close();
}
System.out.println();
System.out.println("receiveFile complete");
System.out.println();
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
} // end of try
finally {
// if any streams or sockets are not null, the close them
if (os != null)
os.close();
if (is != null)
is.close();
if (sock != null)
sock.close();
} // end of finally
} // end of while
} // end of try
finally {
if (servsock != null)
servsock.close();
} // end of finally
}
}
public class Client2 {
public static void main(String args[]) throws Exception {
Socket sock = null; // used in main
String SERVER = "localhost"; // local host
int SOCKET_PORT = 12362; // you may change this
InputStream is = null;
FileInputStream fis = null;
BufferedInputStream bis = null;
OutputStream os = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
String FILE_TO_RECEIVED = "SFileReceived.txt";
String FILE_TO_SEND = "CFileToBeSent.txt";
int FILE_SIZE = 6022386;
try {
sock = new Socket(SERVER, SOCKET_PORT);
System.out.println("Connecting...");
System.out.println();
// get input and output from socket
is = sock.getInputStream(); // get input stream from socket
os = sock.getOutputStream(); // get output stream from socket
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
// receive file
int bytesRead;
byte[] bytearrayReceived = new byte[FILE_SIZE]; // creates byte aray using file size
fos = new FileOutputStream(FILE_TO_RECEIVED); // creates file from path
bos = new BufferedOutputStream(fos); // creates BOS from FOS
int current = 0; // equals the two integers for comparisons later
try {
System.out.println(String.format("Bytes available from received file: %d", is.available()));
System.out.println("byte array from file to receive: " + bytearrayReceived); // debug purposes
while ((bytesRead = is.read(bytearrayReceived)) != -1) {
System.out.println("amount of bytes that was read for while: " + bytesRead);
bos.write(bytearrayReceived, 0, bytesRead);
System.out.println("bos.write");
current += bytesRead;
System.out.println("current += bytesRead;");
}
System.out.println("File " + FILE_TO_RECEIVED + " downloaded (" + current + " bytes read)");
} finally {
fos.close();
bos.close();
}
System.out.println();
System.out.println("receiveFile() complete");
System.out.println();
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
// send file
File myFile = new File(FILE_TO_SEND); // creates a file using file to send path
byte[] bytearraySent = new byte[(int) myFile.length()]; // creates a byte array the size of myFile
try {
// reads the contents of FILE_TO_SEND into a BufferedInputStream
fis = new FileInputStream(myFile); // creates new FIS from myFile
bis = new BufferedInputStream(fis); // creates BIS with the FIS
bis.read(bytearraySent, 0, bytearraySent.length); // copies the BIS byte array into the byte array
// prints to console filename and size
System.out.println("Sending " + FILE_TO_SEND + "(" + bytearraySent.length + " bytes)");
System.out.println("byte array from file to send:" + bytearraySent);
// copies the byte array into the output stream therefore sending it through the
// socket
os.write(bytearraySent, 0, bytearraySent.length);
os.flush(); // flush/clears the output stream
} finally {
fis.close();
bis.close();
}
System.out.println();
System.out.println("sendFile() complete");
System.out.println();
// ----------------------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------------------
} // end of try
finally {
if (sock != null)
sock.close();
if (os != null)
os.close();
if (is != null)
is.close();
} // end of finally
}
}
Server output:--------------------
Server created.
Waiting for connection...
Accepted connection : Socket[addr=/127.0.0.1,port=51565,localport=12362]
Sending SFileToBeSent.txt(32 bytes)
byte array from file to send:[B#4e25154f
sendFile complete
Bytes available from received file: 0
byte array from file to receive: [B#70dea4e
Client output--------------------
Connecting...
Bytes available from received file: 32
byte array from file to receive: [B#4e25154f
amount of bytes that was read for while: 32
bos.write
current += bytesRead
I still have the issue with InputStream, and i have found out through debugging that both the server and client get stuck on the while() statement that is in the receive section. For the server, it stops immediately, whereas the client goes through the while loop once then stops when it hits the while statement.
If anyone has any suggestions or solutions, then it would be much appreciated!
First , your receiveFile method should read input and write output in a loop.
Second, please close files after done or you may have a resource leak.
public static void receiveFile() throws IOException {
byte[] mybytearray = new byte[FILE_SIZE]; // creates byte aray using file size
fos = new FileOutputStream(FILE_TO_RECEIVED); // creates file from path
bos = new BufferedOutputStream(fos); // creates BOS from FOS
int current = 0; // equals the two integers for comparisons later
try {
while ((bytesRead = is.read(mybytearray)) != -1) {
bos.write(mybytearray, 0, bytesRead );
current += bytesRead;
}
bos.flush(); // clears the buffer
System.out.println("File " + FILE_TO_RECEIVED + " downloaded (" + current + " bytes read)");
} finally {
bos.close();
}
}
I have two problems with my java server-client file communication,
I have the CLIENT sends files over to the server and the SERVER receives the files.
My 2 issues are:
1) whenever i send a file, it is 8 bytes less (i do not know why)
2) the file transfer is not complete (with 8 bytes less) unless i close the socket, which i do not want. i want my connection to be persistent, so how can i send a EOF from the client to the server.
here is my client who sends files
public void sendFiles(String file) {
try {
File myFile = new File(file);
long length = myFile.length();
byte[] buffer = new byte[8192];
System.out.println(length);
FileInputStream in = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(in);
BufferedOutputStream outF = new BufferedOutputStream(sock.getOutputStream());
out.print("%SF%" + length + "$" + myFile.getName() + "#");
out.flush();
int count;
while ((count = in.read(buffer)) > 0) {
outF.write(buffer, 0, count);
}
outF.flush();
in.close();
bis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
SERVER who receives files.
I'm passing the name and the length of the file but only using the name of the file. however, i don't know if i need to use the length of the file or not, in case of EOF or something. Please advice
Also, The code hangs in
while ((count = this.sock.getInputStream().read(buffer)) > 0) {
due to no EOF which i do not know how to implement
public void recvFile(String fileName, int length) {
try {
byte[] buffer = new byte[8192];
FileOutputStream outF = new FileOutputStream("/Users/Documents" +fileName);
BufferedOutputStream bos = new BufferedOutputStream(outF);
int count = length;
while ((count = this.sock.getInputStream().read(buffer)) > 0) {
bos.write(buffer, 0, count);
}
bos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
UPDATE: I have removed the flush() as advised that it is not needed. Also, i have tested this code in a different class and it worked but it doesn't work here with client-server chat. Could anyone tell me why?
Any help or hints would be appreciated.
Thank you.
I would suggest to you send the file size first and/or properties of the file... You can try HTTP which is wide use for this task...
Another suggestion would be for you to open another connection on other TCP port just to send the file (this is actually how FTP sends files)
I suspect the problem you have is in code you haven't shown.
In this example you can send multiple messages or files over the same stream.
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.SocketChannel;
/**
* Created by peter on 1/25/15.
*/
public class DataSocket implements Closeable {
private final Socket socket;
private final DataOutputStream out;
private final DataInputStream in;
public DataSocket(Socket socket) throws IOException {
this.socket = socket;
this.out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
this.in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
}
#Override
public void close() throws IOException {
out.flush();
socket.close();
}
// message format is length as UTF-8 encoded name, 32-bit int followed by data.
public void writeMessage(String description, byte[] bytes) throws IOException {
out.writeUTF(description);
out.writeInt(bytes.length);
out.write(bytes);
out.flush();
}
public byte[] readMessage(String[] description) throws IOException {
description[0] = in.readUTF();
int length = in.readInt();
byte[] bytes = new byte[length];
in.readFully(bytes);
return bytes;
}
public void writeFile(File file) throws IOException {
long length = file.length();
if (length > Integer.MAX_VALUE) throw new IllegalArgumentException("length=" + length);
out.writeUTF(file.toString());
out.writeInt((int) length);
byte[] buffer = new byte[(int) Math.min(length, 32 * 1024)];
try (FileInputStream fis = new FileInputStream(file)) {
for (int len; (len = fis.read(buffer)) > 0; ) {
out.write(buffer, 0, len);
}
}
out.flush();
}
public void readFile(File dir) throws IOException {
String fileName = in.readUTF();
int length = in.readInt();
byte[] buffer = new byte[(int) Math.min(length, 32 * 1024)];
try (FileOutputStream fos = new FileOutputStream(new File(dir, fileName))) {
while (length > 0) {
int len = in.read(buffer);
fos.write(buffer, 0, len);
length -= len;
}
}
}
// todo convert to a unit test
public static void main(String[] args) throws IOException {
// port 0 opens on a random free port.
ServerSocket sc = new ServerSocket(0);
DataSocket ds1 = new DataSocket(new Socket("localhost", sc.getLocalPort()));
DataSocket ds2 = new DataSocket(sc.accept());
sc.close();
// now ds1 and ds2 are connected.
File f = File.createTempFile("deleteme","");
f.deleteOnExit();
try (FileOutputStream fos = new FileOutputStream(f)) {
fos.write(new byte[10001]);
}
// send a request
ds1.writeMessage("Send me the file", new byte[0]);
String[] desc = { null };
byte[] data = ds2.readMessage(desc);
if (!desc[0].equals("Send me the file")) throw new AssertionError();
// return a response
ds2.writeFile(f);
f.delete();
if (f.exists()) throw new AssertionError();
ds1.readFile(new File(""));
if (f.length() != 10001) throw new AssertionError("length="+f.length());
ds1.close();
ds2.close();
System.out.println("Copied a "+f.length()+" file over TCP");
}
}
Client :
public class FileClient {
static int Min = 1050;
static int Max = 15000;
static int PORT = Min + (int)(Math.random() * ((Max - Min) + 1));
public static void main(String[] args) throws IOException {
Socket sock = new Socket("localhost", PORT);
if (PORT>=2000&&PORT<=2050) {
System.out.println("The rare disconnection");
sock.close();
}
System.out.println("Connection Opened");
// sendfile
File myFile = new File("input.txt");
byte[] mybytearray = new byte[(int) myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
System.out.println("Input Stream Opened");
bis.read(mybytearray, 0, mybytearray.length);
OutputStream os = sock.getOutputStream();
os.write(mybytearray, 0, mybytearray.length);
System.out.println("Data written to output file");
os.flush();
bis.close();
System.out.println("Input Stream Closed");
sock.close();
System.out.println("Connection Closed");
}
}
Server:
public class FileServer {
public static void main (String[] args ) throws IOException {
int XPORT = FileClient.PORT;
int bytesRead;
ServerSocket serverSocket = new ServerSocket(XPORT);
System.out.println("Listening for a client");
while(true) {
Socket clientSocket = null;
clientSocket = serverSocket.accept();
InputStream in = clientSocket.getInputStream();
// Writing the file to disk
// Instantiating a new output stream object
OutputStream output = new FileOutputStream("output.txt");
byte[] buffer = new byte[1024];
while ((bytesRead = in.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
// Closing the FileOutputStream handle
output.close();
}
}
}
Java Socket : I'm trying to generate random port numbers between a range, but connection is refused, but when I'm using a integer, it works. This is working for one value. I want the server to deny the connection for a few port numbers (Ex: 2000-2100)
And want the connection to be accepted and file transfer to be completed. When using integers, my file transfer works perfectly.
When you run the client you get one random number. When you run the server you get another. They're almost certainly different. There's no reason to expect this to work, and there's no reason to even try. Use a fixed port number. There are plenty available.
I want to send a file through a socket. The "server" is sitting on another computer than mine and the client in another computer as well. The file can go to and fro server and client but only in their current directory. The approach i have taken up to now is by using a file input stream and writing the file on a file output stream neverthelees this doesnt work as far as i understand.. Is there another way to send files through sockets?
Here is my code what could be wrong here?
public class Copy {
private ListDirectory dir;
private Socket socket;
public Copy(Socket socket, ListDirectory dir) {
this.dir = dir;
this.socket = socket;
}
public String getCopyPath(String file) throws Exception {
String path = dir.getCurrentPath();
path += "\\" + file;
return path;
}
public void copyFileToClient(String file, String destinationPath)
throws Exception {
byte[] receivedData = new byte[8192];
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(getCopyPath(file)));
String findDot = file;
String extension = "";
for (int i = 0; i < findDot.length(); i++) {
String dot = findDot.substring(i, i + 1);
if (dot.equals(".")) {
extension = findDot.substring(i + 1);
}
}
if (extension.equals("")) {
extension = "txt";
}
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(new File(destinationPath + "\\"
+ "THECOPY" + "." + extension)));
int len;
while ((len = bis.read(receivedData)) > 0) {
bos.write(receivedData, 0, len);
}
// in.close();
bis.close();
// output.close();
bos.close();
}
// public static void main(String args[]) throws Exception {
// Copy copy = new Copy();
// System.out.print(copy.getCopyPath("a"));
// }
}
And some client code :
...
DataOutputStream outToServer = new DataOutputStream(
clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
boolean exit = false;
else if (sentence.length() > 3 && sentence.substring(0, 3).equals("get")) {
String currPath = dir.getCurrentPath();
outToServer.writeBytes(sentence + "_" + currPath + "\n");
} else {
...
Your copyFileToClient method uses a FileInputStream and FileOutputStream directly, i.e. it does not transfer anything to/from the client at all, only from one local file to another. This is fine if you want to manage remotely files on the server, but does not help for sending data between different computers.
You have to somehow send the data through the OutputStream/InputStream of the Socket - i.e. use the FileInputStream at the sending side and the FileOutputStream at the receiving one.