3.2 combined input and output streams
Stream-related classes in Java can be combined to provide powerful functions. We can package the outputstream of a socket instance in a bufferedoutputstream instance. In this way, we can temporarily cache the bytes and then send them all to the underlying communication channel, to improve program performance. We can also wrap this bufferedoutputstream instance in a dataoutputstream instance to implement the function of sending basic data types. The following code implements this combination:
Socket socket = new Socket(server, port);DataOutputStream out = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));
The following is an example of combining I/O streams.
The complete code is as follows:
Server
Package COM. suifeng. tcpip. chapter3; import Java. io. bufferedinputstream; import Java. io. bufferedoutputstream; import Java. io. datainputstream; import Java. io. dataoutputstream; import Java. io. ioexception; import java.net. serversocket; import java.net. socket; import java.net. socketaddress; public class tcpechoserver {public static void main (string [] ARGs) throws ioexception {If (ARGs. length! = 1) {Throw new illegalargumentexception ("parameter: <port>");} // obtain the server port int SERVERPORT = integer. parseint (ARGs [0]); // create the socketserver instance serversocket Server = new serversocket (SERVERPORT) for client connection; system. out. println ("server has started !!!! \ Nwaiting data from client. "); While (true) {system. out. println ("waiting for the client to send data"); Socket socket = server. accept (); socketaddress address = socket. getremotesocketaddress (); system. out. println ("Handling Client at" + address); // combined input stream datainputstream Dis = new datainputstream (New bufferedinputstream (socket. getinputstream (); // read a double value, double value = dis. readdouble (); system. out. println ("server encoded ed double value =" + value); // read an int value int value2 = dis. readint (); system. out. println ("server received ed int value =" + value2); // read a long value long value3 = dis. readlong (); system. out. println ("server received long value =" + value3); system. out. println ("the server is sending data ..... "); dataoutputstream out = new dataoutputstream (New bufferedoutputstream (socket. getoutputstream (); out. writelong (value3); out. writedouble (value); out. writeint (value2); system. out. println ("the server ends sending data"); out. flush (); socket. close ();}}}
Client
Package COM. suifeng. tcpip. chapter3; import Java. io. bufferedinputstream; import Java. io. bufferedoutputstream; import Java. io. datainputstream; import Java. io. dataoutputstream; import Java. io. ioexception; import Java. io. inputstream; import java.net. socket; import java.net. unknownhostexception; public class tcpechoclient {/*** @ Param ARGs * @ throws ioexception * @ throws unknownhostexception */public static void Main (string [] ARGs) throws unknownhostexception, ioexception {If (ARGs. length <1 | args. length> 2) {Throw new illegalargumentexception ("parameter (s): <Server> [<port>]") ;}// sername or IP addressstring Server = ARGs [0]; // obtain the port number int SERVERPORT = (ARGs. length = 2 )? Integer. parseint (ARGs [1]): 7; // use the specified server and port to create socketsocket socket = new socket (server, SERVERPORT); system. out. println ("connected to server ..... sending echo string "); system. out. println ("the client is sending data ..... "); // combined output stream dataoutputstream out = new dataoutputstream (New bufferedoutputstream (socket. getoutputstream (); system. out. println ("client send double value = 3.14"); // sends a double value out first. writedouble (3.14); system. out. println ("client send int value = 50"); // resend an int value out. writeint (50); system. out. println ("client send long value = 1233333333333333l"); // Finally, send a long value out. writelong (12333333333333333333l); out. flush (); system. out. println ("End of data sending"); system. out. println ("the client is receiving data ..... "); datainputstream Dis = new datainputstream (New bufferedinputstream (socket. getinputstream (); system. out. println ("client received long value =" + dis. readlong (); system. out. println ("client passed ed double value =" + dis. readdouble (); system. out. println ("client received ed int value =" + dis. readint (); system. out. println ("the client is receiving data to end"); socket. close ();}}
Start the server and listen to port 39393.
Start the client and want to send data on port 39393
View the server again