I can get the server to connect successfully but I can't get it to read my txt file, I tried putting it at the top of the C drive and having the write address for it, or just putting a file at the top of the java project folder and still no.
// import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; public class Server { public static void main(String[] args) { try { // creating ServerSocket object ServerSocket ss=new ServerSocket(9999); System.out.println("Server is waiting for connection...!!!"); Socket s=ss.accept(); // accepting connection from client System.out.println("Connection successful...!!!"); InputStream is=s.getInputStream(); // creating InputStream object BufferedReader br1=new BufferedReader(new InputStreamReader(is)); String fileName=br1.readLine(); BufferedReader br2=new BufferedReader(new FileReader(fileName)); OutputStream os=s.getOutputStream(); PrintWriter pw=new PrintWriter(os,true); String s1=""; // reading content from file and sending to client line by line while((s1=br2.readLine())!=null) { pw.println(s1); } pw.close(); br1.close(); br2.close(); is.close(); os.close(); } catch (Exception e) { } } }
// import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.util.Scanner; public class Client { public static void main(String[] args) { try { // creating Scanner class object to getting input from user Scanner sc=new Scanner(System.in); // creating Socket object Socket s=new Socket("localhost", 9999); System.out.print("Enter file name: "); String fileName=sc.nextLine(); // getting file name from user OutputStream os=s.getOutputStream(); PrintWriter pw=new PrintWriter(os, true); pw.println(fileName); InputStream is=s.getInputStream(); BufferedReader br=new BufferedReader(new InputStreamReader(is)); String s1=""; // reading and printing content sent by server while((s1=br.readLine())!=null) { System.out.println(s1); } sc.close(); pw.close(); is.close(); os.close(); } catch (Exception e) { } } }
Any input from anyone would be appreciated, thanks!