Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible for a server side socket to do both read and write operations on files?
    primarykey
    data
    text
    <p>Is it possible to do both operations on files in java sockets? If so how? I tried on method in which the client sends a token such as "R" for read and "W" for write and the server examines it and carries out the requested operation. But this does not seem to work. </p> <p>A relevant piece of <strong>client side</strong> code is as follows:</p> <pre><code>public static void readFile(String input, BufferedReader stdin, PrintWriter out, BufferedReader in) throws IOException { System.out.println("Enter the filename that you want to read"); stdin = new BufferedReader(new InputStreamReader(System.in)); String input = stdin.readLine(); out.println("R" + input); System.out.println("File from server \n\n" + in.readLine()); } public static void writeFile(String input, BufferedReader stdin, PrintWriter out, BufferedReader in, Socket s) throws IOException { System.out.println("Enter the filename that you want to write to"); stdin = new BufferedReader(new InputStreamReader(System.in)); input = stdin.readLine(); out.println("W" + " " + input); System.out.println("Enter the text you want to enter in the file"); out.println(stdin.readLine()); System.out.println("File has been updated. The updated file from the server is: " + in.readLine()); } </code></pre> <p>In the <strong>server side</strong>, I've written a code as follows:</p> <pre><code>BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter out = new PrintWriter(socket.getOutputStream(),true); String[] recv = in.readLine().split(" "); String file_name = recv[1]; </code></pre> <p>This is the place where I check whether I'm supposed to read or write to the file:</p> <pre><code>if(recv[0] == "R") { while((file_name = recv[1]) != null) { System.out.println("Filename got from client: " + file_name); fileread = new BufferedReader(new FileReader(file_name)); while((line = fileread.readLine()) != null) { out.println(line); } System.out.println("The contents of the file " + file_name + " has been sent to the client"); } } if(recv[0] == "W") { System.out.println("Filename got from client: " + file_name); File file = new File(file_name); filewrite = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(filewrite); line = in.readLine(); System.out.println(line); bw.write(line); bw.close(); out.println(line); System.out.println("The contents of the file " + file_name + " has been sent to the client"); } } </code></pre> <p>I do not get an explicit error, but when I run the program, the client takes the file name, the server recognizes it (I know this as I make the server print the name of the file), and then the server does nothing; it stops (without any errors or exceptions). </p> <p>Can anyone tell me where I went wrong? Or is there a better method to carry out the task?</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload