Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Help with my code for this client/server program.

  1. #1
    Junior Member
    Join Date
    Jun 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with my code for this client/server program.

    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!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with my code for this client/server program.

    Can you post code that is not double spaced? The extra blank lines makes the code hard to work with.

    One problem I see is the empty catch blocks. There should be calls to the printStackTrace method in all catch blocks to show messages when an exception happens.

    Try adding some print statements to the code that show where the code is executing and what the values of variables are as they change.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with my code for this client/server program.

    Okay, I posted without the double spaces. I will look into the other things you said right now, thanks for the quick response Norm!

    --- Update ---

    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(3333);
    	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));
    	System.out.println("BufferedReader 1");
    	String fileName=br1.readLine();
    	BufferedReader br2=new BufferedReader(new FileReader(fileName));
    	System.out.println("BufferedReader 2");
    	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", 3333);
    	System.out.print("Enter file name: ");
    	String fileName=sc.nextLine(); // getting file name from user
    	System.out.println("got filename");
    	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);
    }
    	s.close();
    	sc.close();
    	pw.close();
    	is.close();
    	os.close();
    } 
    	catch (Exception e) {
    }
    }
    }

    I added in some print statements to see where it gets completed, for some reason when I run the project once then it does the connection successful but it does not anymore after that, I am guessing because the socket is not closed. However, it does get to the print statement "got file" but still not printing any contents that I had in the file. My assignment is this: Write a client program and server program. The server uses a socket connection to allow a client to supply a filename and the server will send the file contents to the client or an error message if the file does not exist. The client will display the contents to the screen.
    Last edited by ericeclifford; July 23rd, 2019 at 08:13 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with my code for this client/server program.

    The catch blocks are still empty. That means exception messages are not printed that could tell you about a problem.

    I added in some print statements
    Can you copy what is printed and paste it here so we can see what the programs are doing?

    There needs to be print statements that print out the value of filename and that print out each record that was read from the file and prints a message when the loop exits.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with a client server program
    By skarosg3 in forum Java Theory & Questions
    Replies: 16
    Last Post: August 30th, 2014, 12:22 PM
  2. client- server program not working
    By vivek rajput in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 18th, 2014, 02:51 PM
  3. my encrypting simple client to server program unable to get the key from client
    By Paytheprice in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 3rd, 2013, 07:15 AM
  4. Client Server Program Not Working
    By ROHIT C. in forum Java Networking
    Replies: 3
    Last Post: September 3rd, 2010, 02:30 PM
  5. Client-Server program
    By Reztem in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 12th, 2010, 05:36 PM