1

Can I parallelly connect many independent I/O streams of server's socket and client's socket such that each pair of I/O streams could send different data at the same time ?

How could I achieve such a connection in java without increasing number of sockets between server and client?

Himanshu Singh
  • 228
  • 1
  • 3
  • 11
  • 2
    The term is 'multiplexing'. You just need to wrap up messages with something like a 'channel id', and then direct them appropriately on the other end. – sje397 Aug 29 '15 at 07:41
  • @sje397 Suppose I want to send a String from console and simultaneously send a comparatively many large file. Then would multiplexing be an appropriate approach as the files are ready to be send and while another thread is waiting for the user's String? – Himanshu Singh Aug 29 '15 at 07:46

2 Answers2

0

There's always one stream for input and one stream for output, so you can't add more streams.

However, as sje397 commented, you can use the same stream to send "different" data, you just need to come up with a way to distinguish the channels in the receiving side, so it can reconstruct the data properly. This is a protocol design issue.

Edit: In your example you could have a packet structure with a header that tells the type (or channel) of the packet, length of the data and for the file packets some additional information if needed. Let's assume that the length field is a single byte, so your maximum packet size (for String packets) would be 1 + 1 + 255 = 257 bytes.

When the server reads bytes, it will check the first byte for the type of the packet. After determining that it's a String packet, it will read the length, then read the payload. Then the process repeats itself.

For file data additional header information is most likely needed, otherwise the non-String packets will just be a bunch of bytes.

This means that your protocol will become packet based, so you must write the data one packet at a time. Assuming that a data packet has a max size of 64K, you would then be able to send data in the following way (imagine that it's a network pipe):

Client -> 257(S) -> 64K(D) -> 257(S) -> 64K(D) -> 257(S) -> Server allowing you to interleave the two different kinds of data in a single network connection.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • Suppose I want to send a String from console and simultaneously send comparatively many larger files. As the files are ready to be send and while another thread is waiting for the user's String so I should design a protocol for multiplexing all the file's data and the user's string's data? – Himanshu Singh Aug 29 '15 at 07:53
  • Thank you. It was really helpful. – Himanshu Singh Aug 29 '15 at 09:59
0

Assuming that you want fast replies for console input, my suggestion to use two socket streams - one for file data and another for user input. You can use ObjectInputStream and ObjectOutputStreams to simplify your protocol. Just create a class for your protocol make it serializeable and use it with socket streams.

Dakshinamurthy Karra
  • 5,353
  • 1
  • 17
  • 28
  • like packing of socket and considering it to be a single socket. but will increasing the number of sockets decrease the link speed of communication channel? – Himanshu Singh Aug 29 '15 at 10:02
  • From one socket to two: I doubt it very much. – Dakshinamurthy Karra Aug 29 '15 at 10:20
  • Using `ObjectStreams` rarely simplifies things, except at the very beginning (when you notice that "hey, I can just write a `String` and read it from the other end!"). Things like headers and other tricks that `ObjectStreams` do usually end up confusing the (beginner) programmer, as soon as requirements get more advanced. – Kayaman Aug 29 '15 at 10:23
  • I will also try with objectstreams. and will see how far could I get with that. Once again thank you KDM and Kayaman – Himanshu Singh Aug 29 '15 at 11:43