Over the years I’ve run into (and created) APIs around device communication (Serial, USB, TCP, Bluetooth, etc). At the bottom of these APIs are usually byte streams that can send data to the device and receive data from the device. Where they sometimes differ however is whether they give you one bidirectional stream, or two separate unidirectional streams.
I recently ran into such an API gain, specifically the Android BluetoothSocket
, which has a separate getInputStream() and getOutputStream(). This made me wonder, what are the benefits and downsides of having two separate unidirectional stream objects, versus a single bidirectional stream object?
One downside I’ve run into myself for having separate streams is that the meaning of “input” and “output” depends on the perspective, which can cause confusion. The “input” from the server side is the “output” from the client side, and vice versa. In languages where the interface for a stream is inherently bidirectional (see C# for example), this can lead to bugs when the wrong stream is accidentally used.
Rick de Water is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.