Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
Can someone help me with this question please ?

Write TCP/IP client and server programs as follows:

The client should:

send the name of a file to the server, read the contents of the file back from the server, save the contents to a file.

The server should:

accept a connection from the client, read the name of a file, read the contents of the file from disk, send the contents of the file to the client, close the connection.


I have done most of the requirements in the question except the part where it asks that the client should read the contents of the file back from the server and save the contents to a file. test.txt is the specified file. I only put random contents in it, because that is not the main point, and that is why i did not include the file here. Any idea guys ?!

Thanks in Advance.

What I have tried:

ReadFileClientApp.java:


Java
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

public class ReadFileClientApp {

    public static void main(String[] args) throws Exception {

         InetAddress inet = InetAddress.getByName("localhost");
         Socket s = new Socket(inet, 2001);

         OutputStream o = s.getOutputStream();
         PrintWriter p = new PrintWriter(o);
         InputStream in = s.getInputStream();
         Scanner r = new Scanner(in);

         p.println("test.txt");
         p.flush();

        while (r.hasNextLine()){
             System.out.println(r.nextLine());
        }
         }



ReadFileServerApp.java:


C#
import java.io.FileInputStream;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.io.PrintWriter;
  import java.net.ServerSocket;
  import java.net.Socket;
  import java.util.Scanner;

  public class ReadFileServerApp {

    public static void main(String[] args) throws Exception {
         Socket s;
         ServerSocket ss = new ServerSocket(2001);
         while (true) {
         System.out.println("Server: waiting for connection ..");
         s = ss.accept();

         InputStream in = s.getInputStream();
         Scanner r = new Scanner(in);
         OutputStream o = s.getOutputStream();
         PrintWriter p = new PrintWriter(o);

         String fileName;
         fileName = r.nextLine();
         System.out.println("File requested"+ fileName);

         FileInputStream fin = new FileInputStream(fileName);
         Scanner f = new Scanner(fin);

         while (f.hasNextLine()) {
             p.println(f.nextLine());
         }


         p.close();
         }
         }

  }
Posted
Updated 8-May-16 15:35pm
Comments
Sergey Alexandrovich Kryukov 8-May-16 20:48pm    
Excuse me, where is your question? Help with what?
—SA
Ziyad Alotaibi 8-May-16 20:53pm    
I only couldn't figure out how to read the contents of the file back from the server and save the contents to a file in the client side. The same for the server side, i need to know how to read the contents of the file from DISK, send the contents of the file to the client. I am able to read file name and send it to the server. Do you get me ?
Sergey Alexandrovich Kryukov 8-May-16 21:05pm    
It's all about reading data from/to stream. What's the problem?
—SA

1 solution

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

Java
 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

Java
while (r.hasNextLine()){
     System.out.println(r.nextLine());
}


you would do (remembering Im very unpracticed at java), something like :-

Java
// We've done 
//
// p.println("test.txt");
// p.flush();
//
// To send the Filename to the server - now we (hopefully) get the file data back 
//

// Declare Filename (should be done earlier since its also sent to the server)
String filenameOutput = "test.txt";

// File Object
File fileOut = new File(filenameOutput);

// If output file doesnt exist, create it - maybe if it does exist this should be extended to 'delete it' 
if (!fileOut.Exists())
{
    fileOut.createNewFile();
}

// Stream Object
FileOutputStream fileOutStream = new FileOutputStream(fileOut);

// While the reader (data back from the server) has (lines?)
while (r.hasNextLine())
{
   // Output the data from the server to the stream -> file
   fileOutStream.write(r.nextLine());
}
// Tidy Up
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
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900