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 2 of 2

Thread: Help understanding Input/OutputStreams and BufferedReader/Writers by dissecting code

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help understanding Input/OutputStreams and BufferedReader/Writers by dissecting code

    Here is the code that I wrote. First for the client:
    import java.io.*;
    import java.net.*;
     
    public class ThenClient {
    	public static void main(String[] args) throws IOException {
    		int port = Integer.parseInt(args[0]);
    		String ipAddress = args[1];
    		InetAddress ia = InetAddress.getByName(ipAddress);
     
    		Socket client = new Socket(ia, port);
     
    		InputStream is = client.getInputStream();
    		OutputStream os = client.getOutputStream();
     
    		InputStreamReader isr = new InputStreamReader(is);
    		OutputStreamWriter osw = new OutputStreamWriter(os);
     
    		BufferedReader br = new BufferedReader(isr);
    		BufferedWriter bw = new BufferedWriter(osw);
     
    		bw.write("Paul, you should make the internet in Morrison faster. Kthx!\n");
    		bw.flush();
     
    		System.out.println(br.readLine());
     
    		br.close();
    		bw.close();
    		osw.close();
    		isr.close();
    		os.close();
    		is.close();
    		client.close();
    	}
    }

    and then for the server:

    import java.io.*;
    import java.net.*;
     
    public class ThenServer {
    	public static void main(String[] args) throws IOException {
    		int port = Integer.parseInt(args[0]);
    		String message = args[1];
     
    		ServerSocket server = new ServerSocket(port);
    		Socket client = server.accept();
     
    		InputStream is = client.getInputStream();
    		OutputStream os = client.getOutputStream();
     
    		InputStreamReader isr = new InputStreamReader(is);
    		OutputStreamWriter osw = new OutputStreamWriter(os);
     
    		BufferedReader br = new BufferedReader(isr);
    		BufferedWriter bw = new BufferedWriter(osw);
     
    		System.out.println(br.readLine());
     
    		bw.write(message + "\n");
    		bw.flush();
     
    		bw.close();
    		br.close();
    		osw.close();
    		isr.close();
    		os.close();
    		is.close();
    		client.close();
    		server.close();
    	}
    }

    I have read the APIs for all the classes I used but I don't understand what each line is doing exactly (apart from the closing ones). Our teacher gave us really guided directions to how to make this program which is why I was able to come up with the code but I have no experience with any network stuff so this is all really confusing to me. What the program is supposed to do is the server starts up with 2 command line arguments, a port and a message and then the client starts up with 2 command line arguments, a port and an ip address and then the client sends a static message to the server and the server responds back with the message entered in the command line. My program works, just wondering how it actually WORKS.

    Thank you!


  2. #2
    Member
    Join Date
    Dec 2011
    Posts
    50
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default Re: Help understanding Input/OutputStreams and BufferedReader/Writers by dissecting c

    What you say shows that you understand almost of the program. The server listens on a specific port. The client connects to the server using IP address that points to the server, and the port which is listened by the server. The client sends a message and the server responds back. That's all simple!

Similar Threads

  1. Your input with written code (Sorting and searching within array)
    By GalBenH in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 11th, 2012, 09:19 AM
  2. Not really Understanding JPanel..
    By Emperor_Xyn in forum Java Theory & Questions
    Replies: 7
    Last Post: January 1st, 2012, 05:48 AM
  3. Help me Understanding Please...
    By Jabetha in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 17th, 2011, 01:55 PM
  4. Problem understanding Java code modification
    By eagle09 in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 26th, 2011, 04:13 PM
  5. Help understanding this code
    By Zepx in forum Java Theory & Questions
    Replies: 2
    Last Post: October 20th, 2009, 10:18 AM