I hate the way you havnt commented your code, and you've jumbled up your input and output scanners & streams etc of your code for your client and server - makes it real hard to follow !
Are you in effect asking about this code
p.println("test.txt");
p.flush();
while (r.hasNextLine()){
System.out.println(r.nextLine());
}
?
In which I take it that you've sent 'test.txt to the server as the filename for it to read and return the data from ?
if thats the case, then, surely instead of
while (r.hasNextLine()){
System.out.println(r.nextLine());
}
you would do (remembering Im very unpracticed at java), something like :-
String filenameOutput = "test.txt";
File fileOut = new File(filenameOutput);
if (!fileOut.Exists())
{
fileOut.createNewFile();
}
FileOutputStream fileOutStream = new FileOutputStream(fileOut);
while (r.hasNextLine())
{
fileOutStream.write(r.nextLine());
}
fileOut.close();
I would suggest you put a try/catch around the code Ive shown using appropriate exceptions, for your safety, and dispose of anything I havnt (in c# I would be advocating 'using...' blocks)
There's also a lot of 'assumption' in your technique whereby you're assuming your text file is line orientated - for more that a trivial test case, I'd likely use bytes, and, have a dedicated file header sent back to the client containing perhaps the file size & crc, so the client can ensure it has the information to verify the (bytes) have arrived back safely