This thread is started by the class cint which gave the client interface.
In the run, a clientsocket is initialized to the server's address at port 8080.
An InputStream and an OutputStream for the socket is created. InputStreamReader and OutputStreamWriter uses instances of these to provide read/write operations of the socket.
BufferedReader reads text from the character-input stream ie. the InputStreamReader, buffering characters so as to provide for the efficient reading of characters, arrays, and lines. Each read request made of an InputStreamReader causes a corresponding read request to be made of the underlying character or byte stream. Therefore BufferedReader is wrapped around the InputStreamReader whose read( ) operations may be costly.
PrintWriter prints formatted representations of objects to a text-output stream. Since it does not contain methods for writing raw bytes, the program uses unencoded byte streams ie an OutputStreamWriter which converts characters into bytes using the default character encoding.
The use of a buffered input stream allows the application to read bytes from a stream without necessarily causing a call to the underlying system for each byte read. The data is read by blocks into a buffer; subsequent reads can access the data directly from the buffer.
Note : This code can be enhanced to display advertisements periodically in Client's GUI i.e. it will pick up gif / jpg files sent by Server & display it.
// filename: Clienttest.java
import java.io.*; import java.net.*; import java.awt.*; public class ClientTest extends Thread { public void run( ) { runclient( ); } public static void runclient( ) { try { Client client = new Client(cint.surlname,8080); BufferedReader reader = new BufferedReader(new InputStreamReader(client.in)); PrintWriter writer = new PrintWriter(new OutputStreamWriter(client.out)); BufferedInputStream bis = new BufferedInputStream(client.in,1); // (0). String init = reader.readLine( ); if(init.equals("NOTOK")) { cint.print("PULL IN OPERATION "+init); cint.print("\n\n\t\t SERVER BUSY !\n\n\t Try out after sometime."); } else{ cint.print("\nPUSH IN OPERATION "+init); // (1). fileindex int no_of_files1 = bis.read( ); cint.print("\n\n ## Total number of files: "+no_of_files1+"\n"); writer.println("OK"); writer.flush( ); cint.pb.setMaximum(no_of_files1); for(int z = 0; z < no_of_files1; z++) { int pbval = cint.pb.getValue( )+1; cint.pb.setValue(pbval); //(2). file name String filename = reader.readLine( ); writer.println("..OKn"+z); writer.flush( ); //(3). filelength //int actfilelength = (bis.read( )); //(3). filelength String lenn = reader.readLine( ); long actfilelength = Long.parseLong(lenn,10); cint.pb1.setForeground(Color.red); cint.pb1.setBackground(Color.white); cint.pb1.setMaximum((int)actfilelength); writer.println("..OKl "+z); writer.flush( ); cint.print("\n "+filename+" "+actfilelength+" "); // cint.print(" "); FileOutputStream fop = new FileOutputStream(filename); cint.pb1.setValue(-1); for (long i=0; i < actfilelength; i++) { //(4). int pb1val = cint.pb1.getValue( )+1; cint.pb1.setValue(pb1val); int j = bis.read( ); fop.write(j); } fop.close( ); cint.print(" "); } //end for loop cint.print("\n"); }// end else cint.print("\n\n\n Disconnecting from Server."); }// end of try catch(Exception e) { cint.print("IOException in client.in.readln( )"); cint.print("error.."+e); } }//end runclient }//end class class Client extends ClientTest { //function make input and output streams available to user classes private Socket client; public InputStream in; public OutputStream out; public Client(String host, int port) { try { client = new Socket(host, port);// System.out.println( ); cint.print("\n attaching to: "+client+"\n"); out = client.getOutputStream( ); in = client.getInputStream( ); } catch(IOException e) { cint.print("IOException : "+e); } } } // class end.