12

Here is a code

import java.io.*;
import java.net.*;
public class Whois
{
    public static void main(String[] args)
        throws Exception
    {
        // TODO Auto-generated method stub
        int c;
        Socket s = new Socket("whois.internic.net",43);
        *InputStream in = s.getInputStream();
        *OutputStream out = s.getOutputStream();
        String str = (args.length == 0 ? "osborne.com" : args[0] ) + "\n";
        byte buf[] = str.getBytes();
        *out.write(buf);
        System.out.print("hey baby");
        while ((c=in.read()) != -1)
        {
            System.out.print((char) c);
        }
        s.close();
    }
}

I have marked the statements that I have problems understanding. I do not understand what OutputStream object out will hold when it is assigned s.getOutputStream() and what is the need of passing buf to out by out.write(buf).

I have learned Input and Output Streams using files but I do not understand getinputstream and outputstreams. I have googled it, read it here on SO as well as from many different books and from oracle documents as well. Please discuss it in detail.

I know how to read from files and how to write to them, but here I do not understand what is the need of passing buf array which holds only a string. What I mean to ask is that when in has the input stream of the socket why can't we directly just read from it? What exactly is a socket inputstream and outputstream?

I found something here on SO here is the link "https://stackoverflow.com/questions/12715321/java-networking-explain-inputstream-and-outputstream-in-socket", here an answer by DNA says

In Java, to send data via the socket, you get an OutputStream (1) from it, and write to the OutputStream (you output some data)."

This is confusing me, when outputStream is used to send data via socket what was the need of out.write(buf)? Why do we need to send "google.com" to outputStream?

Oriol Roma
  • 329
  • 1
  • 5
  • 9
Junaid Shirwani
  • 360
  • 1
  • 6
  • 20

3 Answers3

37

first thing you need to understand is what is STREAM

A stream can be defined as a sequence of data. The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination.

****Next is type of streams****

 we have byte stream and character stream.

enter image description here

classes we have in Input Stream and output stream 

enter image description here

as the name suggests in simple terms input stream is used to input the data and output stream is used to output the data

Java byte streams are used to perform input and output of 8-bit bytes. Though there are many classes related to byte streams but the most frequently used classes are , FileInputStream and FileOutputStream. also

Java Byte streams are used to perform input and output of 8-bit bytes, where as Java Character streams are used to perform input and output for 16-bit unicode. Though there are many classes related to character streams but the most frequently used classes are , FileReader and FileWriter.. Though internally FileReader uses FileInputStream and FileWriter uses FileOutputStream but here major difference is that FileReader reads two bytes at a time and FileWriter writes two bytes at a time.

For reference

  1. What is InputStream & Output Stream? Why and when do we use them?

  2. java DataOutputStream getOutputStream() getInputStream()

Example for getInputStream and getOutputStream

  1. http://zerioh.tripod.com/ressources/sockets.html

New Link http://docs.oracle.com/javase/tutorial/essential/io/buffers.html

Community
  • 1
  • 1
Divya
  • 1,469
  • 1
  • 13
  • 25
  • i know what streams are and what input and output streams are.what i dont understand is that when buf is sent to the outputstream how does inputstream reads it .input stream gets only the contents of buf which is only a string in byte form.how does it read from it then? – Junaid Shirwani Mar 21 '14 at 16:43
  • try the link i have added lastly. NewLink. – Divya Mar 21 '14 at 16:49
  • 1
    what is the purpose of writing out.write(buf) to the outputstream of the socket?thats what i asked.out hold the output stream of a socket which has inetadress google.com .what is the purpose of write to the output stream of such a socket ? thats what i asked for and i got everything except that .I need an explanation for that please – Junaid Shirwani Mar 21 '14 at 16:50
  • the difference in the examples from the links you have posted is that the inputputstream is associated with one object and the output stream is associated with another object .here both input and output streams are associated with the same object and thats what i am asking .what is the purpose of writing buf to the output stream of the same socket? please try to understand what i am asking . your answers are not helping me in the problem i am facing .No offence and respect given – Junaid Shirwani Mar 21 '14 at 16:53
  • see both the the streams have their own connections ,jsut like we open a file in input stream cannot be used for writing,we can only use that object to read similarly you cannot use or predict that if you have used a stream say output stream then it will be open for you to read or input also. – Divya Mar 21 '14 at 16:57
  • do you want to read and write at the same time? for that you can have a look. http://stackoverflow.com/questions/14494352/can-you-write-to-a-sockets-input-and-output-stream-at-the-same-time – Divya Mar 21 '14 at 17:01
  • 1
    no no no.you are not getting my point ,i am trying to learn sockets in java.I am unable to understand this piece of code despite trying really hard.Either i am being stupid or you guys are not getting my point and what i am trying to ask? – Junaid Shirwani Mar 21 '14 at 17:04
  • 1
    when i write buf to the outputstream of the socket who reads from it? – Junaid Shirwani Mar 21 '14 at 17:06
  • you don't need to take care about the background processing for that.java take cares for that and where you are getting confused where not can only be cleared when you do some efforts to make a program and then use all the methods from its classes.questioning like this is good ,and i understand these kinds of questions comes in mind while reading but i simply suggest you to make a project and use all of them and see the output you will be able to trace whats going on. – Divya Mar 21 '14 at 17:11
  • 2
    of what i understand right now after re reading what you said is that the socket is connected to a server ,it sends buf to the other ends via its outputstream ,the server than reads it and returns what it has asked for that is google.com .the returned string/object is stored in the inputstream of the socket ? am i right? – Junaid Shirwani Mar 21 '14 at 17:11
  • i tried to do that ,but i got confused what to do ,for that i have to make a server socket my self which i cant right now or have to learn it.I have been trying to get the hold of sockets for quite some time but it gets frustrating when i dont have any idea how to implement it? give me some ideas for its implementation.It would be really helpfull – Junaid Shirwani Mar 21 '14 at 17:13
  • yeah rite.hope you will do an example and come back with an answer .all the best – Divya Mar 21 '14 at 17:13
  • first step is to understand completely what are sockets and how they are created to interact with the server for that you need client and server so that you can make that.Next step is to get how you can pass data between single client and single server,after that one server and single many client.you can view tutorials on youtube for that – Divya Mar 21 '14 at 17:26
2

InputStream in and OutputStream out will hold references to two types of streams that you can either read data from or write data to. Don't expect them to hold values from the stream itself - instead, they hold the ability to work with the stream. When you create these objects, you're not sending/receiving any data - you're just getting the object that you can use to send/receive data.

out.write(buf) is sending the contents of buf over the Socket so that any readers of the socket (in your case, in) can receive that data. Whatever data is sent to out will be seen on the other side of the Socket by an InputStream.

David Koelle
  • 20,726
  • 23
  • 93
  • 130
0

Here OutputStream is used to send data to other side in socket whenever you write out.write(buf) it will send buffer data in socket.

InputStream is used to receive data from socket.

Smit Shilu
  • 345
  • 3
  • 17
  • what is the need of sending "google.com" to the other side of the socket? cant we just get the inputputStream and read from it ? why do we need to first write buf to the outputstream and then only read from it?please write detail in your answer as it dosent make anything clear .this is what i have been getting on google and unfortunately it dosent make things clear – Junaid Shirwani Mar 21 '14 at 16:36
  • you are sending domain name to other side and receving appropriate responce from the other side. osborne.com the string you are sending to other side – Smit Shilu Mar 21 '14 at 16:57
  • so youu mean to say that the server that is osborne.com is reading from the outputstream which holds data of buf? – Junaid Shirwani Mar 21 '14 at 16:59
  • I am saying that `Socket s = new Socket("whois.internic.net",43);` this will connect to whois.internic.net and the domain about what you wanted to know is 'osborne.com' so when you send this string to server in responce it will send you information about 'osborne.com'. – Smit Shilu Mar 21 '14 at 17:12