0

So a very simple Server code:

while(true) { Socket sock = serverSock.accept(); PrintWriter writer = new PrintWriter(sock.getOutputStream()); String advice = "advice here"; writer.println(advice); writer.close(); } 

And a simple Client that will read data from this Socket:

Socket s = new Socket(“127.0.0.1”, 4242); InputStreamReader streamReader = new InputStreamReader(s.getInputStream()); BufferedReader reader = new BufferedReader(streamReader); String advice = reader.readLine(); 

What I am trying is very high-level actually and quite simple. How does sock.getOutputStream is connected to s.getInputStream()?

How can the data that is sent over clients outputstream can be read from servers inputstream? I can not make the connection in my head and I can not visualize it.

My question is how the inputstream and outputstream object are connected? How can writer.println(advice); end up in reader.readLine()? How is the OutputStream connected to InputStream?

Any help is greatly appreciated.

2
  • Do you mean physically? Or what operations the operating system performs to connect the socket? Can you be a bit more specific on what kind of answer you are searching for? Commented Jul 7, 2014 at 9:48
  • No, I mean what does getOutputStream return? When I print to it, how does it end up in the inputstream of the other Socket? How are those to objects connected in Java? Also I would like to know what happens physically as well as this will probably answer my question too. Thanks btw. Commented Jul 7, 2014 at 10:18

3 Answers 3

1

Sockets uses TCP. If you are unfamiliar, it is a protocol which specifies the mechanics of transmitting data over the internet. The important part of the protocol to this question is the connection.

When 2 devices wish to communicate, a Socket is created on each device, for the port being used to send/receive. This Socket provides a line of communication, on which the server can listen. The Sender sends "Packets" of data across that line of communication, where they are received by the Receiver.

The packets carry a "payload" of data, once of which has data signifying it is the last packet. This allows the Receiver to interpret the full message and respond accordingly.

There are a lot of mechanisms involved in ensure all the data gets there and in the right order, but that is a little outside the scope of this question. Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

1

The version of the Socket constructor in the client you call creates a connected socket directed at the specified endpoint. Since it is not shown, we presume the serverSock in the server was created and initialized to listen on that endpoint. When the client successfully connects, the initialized socket is returned, and correspondingly, the server's socket accept() returns a socket connected to the client.

Connected sockets permit bidirectional communication between the connected endpoints. What is written to one socket is delivered to and can be (eventually) read by the corresponding peer socket to which it is connected. The getOutputStream() and getInputStream() methods return stream objects that permit stream I/O operations that will pass the data through the corresponding socket from which it was created.


Below, I provide answers to the specific questions listed in one of your comments to my post.

Q: What happens (technically) if I write to outputstream but do not read from the inputstream?

A: The data is held inside a buffer until it is read. There is a limit to how much data the buffer can hold. When the limit is reached, the writer on the other socket will either block or be notified it has to wait for the peer socket to read out what has already been written.

Q: How long will it live?

A: Unread data will remain in the buffer until it is read out or until the connection is forcibly torn down with a reset. A connection reset is distinguished from a connection close in that the close indicates no more data will be sent, and the receiver gets this notification as a successful read of 0 bytes of data, while a reset indicates the connection is no longer valid, and any buffered data will be dropped and both read and write operations will fail.

Q: How much time do I have until I can read again?

A: You may read at any time, and it will succeed so long as the connection is still valid.

Q: Can I write twice and then read twice?

A: A connected socket is a byte stream. There is no true relation between "the number of writes" on one end to "the number of reads" on the other end, except that 0 writes correspond will mean 0 successful reads.

As a simple example, a single write of 4 bytes may correspond to 4 reads of 1 byte. A large single write may get segmented in such a way that the receiver will be forced to issue multiple reads to successfully receive the entire message.

Similarly, two writes, each of 4 bytes, may correspond to a single read of 8 bytes. Multiple small writes may get delivered to the receiver in a way that all of them can be retrieved in a single read.

5 Comments

Thanks but this is not what I am asking. I am asking about the outputstream and the inputstreams. How are they connected?
How is data written to a outputstream of one socket, can be read from the inputstream object that the other socket has?
Connected sockets permit bidirectional communication between the connected endpoints. (That is their function.)
What happens (technically) if I write to outputstream but do not read from the inputstream? How long will it live? How much time do I have until I can read again? Can I write twice and then read twice?
@KorayTugay Now these are answerable questions. I suggest you edit them into your original post so they can be properly answered, or start a new post.
0

The sockets connect using a principle called Three Way Handshake.

Once the connection is created, data can be sent back and forth between clients via the various streams.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.