Also the first important requirement of the client-server model is that both the client and the server must be intelligent objects i.e. both must be able to perform enough of their own processing to form a valid message and to give a valid reply to a message.
The roles of the client and the server are asymmetric. This means that both halves are coded differently. When a concurrent server is started first it typically does the following steps :
1. Opens a communication channel and wait for a client request to arrive at some well-known address.
2. A new process is spawned to handle this client request. When this process is finished, it closes its communication channel with the client, and terminates.
SERVERTEST is the sibling thread which is started along with the pullthread in class InetClient.
Class servertest uses the ServerSocket class.
A server socket on port 8080 was created in the class InetClient. Port number 8080 is specific to avoid little endian big - endian problem.
It is on this socket the server accepts connections from the clients.
Initializations :
The method server.accept( ) accepts a connection. This method blocks until the connection is made. It internally creates a socket of type 'client' Socket class and returns it. This 'client' Socket caters only to the client connected on it. Now to service multiple clients that many such clientSockets must be created. Therefore after every successful client connection a new instance of the class Servertest is made. This looks after potential client connections.
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 i.e. 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.
Also a buffered output stream is created. This stream simply lets you write characters to the input stream of the socket without causing a write every time. The data is first written into a bufferof size [ ]. Data is written to the actual stream only when the buffer is full, or when the stream is flushed.
Service :
The global variable 'claim' is accessed.
If set then either a pull is in operation or its pulltime with pullthread waiting to be notified from the last of the servertest thread after the file transfer operation. The serverthread therefore sends appropriate message to the client which then gracefully disconnects itself.
Else the variable 'nconnections' is incremented to reflect the number of clients connected and the file transfer operation is initiated.
The file transfer operation between the server and a client is simple to implement. Initially the .gif files from the \advt directory are sent.
Then it is the turn of the .htm files listed in the globalfiledbase[ ].
The number of files to be transferred is sent to the client.
This allows client to know beforehand about the number of files it has to accept.
For that many times :
The name of the file and the file length is sent across to the client.
Next the file is sent byte by byte.
Afterwards the nconnections variable is decremented.
The last server thread invokes the notify method to wakeup the waiting pull thread.
When the process terminates, the communication channel with the client is also closed.
// filename ServerTest.java
import java.util.*; import java.io.*; import java.net.*; public class ServerTest extends Thread { String clientRequest; BufferedReader reader; PrintWriter writer; BufferedOutputStream bos; FileInputStream fip; //static InetClient v = new InetClient( ); ////////// static String adarray[ ]; //final static int SERVER_PORT = 8080; // OUR SERVER'S OWN PORT //static ServerTest svrt; //static ServerSocket server; ServerTest( ) { //InetClient.print("\nIn servertest constructor...*************************"); } // **** for atomic actions ****** static synchronized void increader( ) { InetClient.nconnections++; } static synchronized void decreader( ) { InetClient.nconnections--; } static synchronized void wakeup( ) { if((InetClient.nconnections == 0) && (InetClient.claim == true)) InetClient.pt.p.bb( ); } static synchronized boolean chkclaim( ) { if(InetClient.claim == true) return true; else return false; } private void push(int fileindex, String dir, String filename[ ]) { } public void run( ) { InputStream in; OutputStream out; Socket socket; ServerTest svrt1; try { InetClient.print("\n Java test server v0.02 on line !"); InetClient.print("\n Server waiting for connection at : " + InetClient.server); InetClient.print("\n before accept"); socket = InetClient.server.accept( ); InetClient.print("\n after accept"); svrt1 = new ServerTest( ); svrt1.start( ); in = socket.getInputStream( ); out = socket.getOutputStream( ); reader = new BufferedReader(new InputStreamReader(in)); writer = new PrintWriter(new OutputStreamWriter(out)); bos = new BufferedOutputStream(out,1); // end new // (0). boolean status1 = chkclaim( ); if(status1==true) { writer.println("NOTOK"); writer.flush( ); } else { writer.println("ALLOK"); writer.flush( ); increader( ); InetClient.print("\n READERS: "+InetClient.nconnections); InetClient.print("\n Client loggedon at : " + socket); InetClient.print(" "); // (1). adarray = advtpush.select( ); int ladarray = adarray.length; try { Thread.sleep(300); } catch(Exception s) { } bos.write(InetClient.fileindex + ladarray); bos.flush( ); InetClient.print("\n## "+(InetClient.fileindex+ladarray)+" files to be transferred "); InetClient.print(reader.readLine( )); InetClient.print("\nPushing Advertisements: "); for(int filecount = 0; filecount < ladarray; filecount++) { //(2). file name InetClient.print("\n "+adarray[filecount]+" "); writer.println(adarray[filecount]); //send client the filename. writer.flush( ); System.out.println(reader.readLine( )+" "); File fp = new File(System.getProperty("user.dir")+"\\advt\\"+adarray[filecount]); System.out.println("file is fp:"+fp); //(3). filelength // InetClient.print(" "+(fp.length( ))); writer.println(Long.toString(fp.length( ),10)); //send client the filelength. writer.flush( ); System.out.println(reader.readLine( )+" "); fip = new FileInputStream(fp); InetClient.svrt.sleep(2000); // InetClient.print(" reading file.. "); int i; while((i = fip.read( ))!=-1) { //(4). bos.write(i); bos.flush( ); } //while ends fip.close( ); // InetClient.print("done."); } //end for loop InetClient.print("\nPushing Files: "); for(int filecount = 0; filecount < InetClient.fileindex; filecount++) { //(2). file name InetClient.print("\n "+InetClient.globalfiledbase[filecount]+" "); writer.println(InetClient.globalfiledbase[filecount]); //send client the filename. writer.flush( ); System.out.println(reader.readLine( )+" "); File fp = new File(InetClient.globalfiledbase[filecount]); //(3). filelength // InetClient.print(" "+(fp.length( ))); // bos.write((int)fp.length( )); // bos.flush( ); //(3). filelength // InetClient.print(" "+(fp.length( ))); writer.println(Long.toString(fp.length( ),10)); //send client the filelength. writer.flush( ); System.out.println(reader.readLine( )+" "); fip = new FileInputStream(fp); InetClient.svrt.sleep(2000); // InetClient.print(" reading file.. "); int i; while((i = fip.read( ))!=-1) { //(4). bos.write(i); bos.flush( ); } //while ends fip.close( ); } //end for loop // InetClient.print("\nMUST NOW DECREMENT"); decreader( ); InetClient.print("\nREADERS: "+InetClient.nconnections); wakeup( ); } // end else. } catch(Exception e) { InetClient.print("\nIn catch before finally "+e); decreader( ); InetClient.print(" "); InetClient.print("\nREADERS: "+InetClient.nconnections+" "+e); wakeup( ); } }//end main }//end class****