pipes with IO & Networking
Is a way of communication, connecting two ends "program to a file , program to a program ...etc".as if a person calling another between those two person there exist a pipe. For IO one end is a file, where as for networking both ends are programs
basic operation
- open():inputStream, for opening a connection. A person dailing a number, the other end pick up
- close(), for closing this connection, one person hang up
- write(byte[] bufferToWrite,int numOfByteToWrite):numOfActuallyWriten. A person talking
- read(byte[] bufferToReadIn,int numOfByteToRead):numOfBytesRead. A person listening
read() operation is a blocking one, so can use available() to check if in the input stream there exist any data to read before do a blocking read. can also synchronize between the two ends by adding a Buffer
for file:
FileInputStream --> BufferedInputStream --> DataInputStream
BufferedInputStream is used to read stream of bytes, if want to distinguish "reading int, char ...etc" use DataInput
In networking we have a socket object that is used to construct a Berkly socket between two ends, also for higher level we can use URL + URLConnection both connected togther to construct a client that can communicate with a http server.
ex:
URL url = new URL("Server URL");
URLConnection connection = url.openConnection();
connection.setRequestProperty(....)//for setting header data
PrintWriter out= new PrintWriter(connection.getOutPutStream())
out.print(name+"="+URLEncoder.encode(value)+"&")//setting request parameter
input | output | |
---|---|---|
Binary | InputStream | OutputStream |
text | Reader | Writer |
Comments
Post a Comment