Commits

Anonymous committed e83c855

The acknowledgement for both the Client and Server are ready to go. The handshaking is handled through separate method calls so just worry about the handleSession methods to take care of the encoding/decoding parts.

  • Participants
  • Parent commits 927d7d4

Comments (0)

Files changed (2)

 import java.net.*;
 import java.io.*;
+import java.util.logging.Level;
+import java.util.logging.Logger;
 
 public class Client
 {
+    private static boolean ackValue;
+	private static boolean ackSent;
+	private static final int ACKSIZE = 13;	//Size of the ACK buffer
 	public static void main( String[] args ) throws IOException
 	{
+		ackValue = false;
+		ackSent = false;
 		testArgs( args.length );
 		String server = args[ 0 ];       // Server name/IP	
-		int servPort = ( args.length == 2 ) ? Integer.parseInt(args[2]) : 7;
+		int servPort = ( args.length == 2 ) ? Integer.parseInt(args[1]) : 7;
+		
+		//set up
+		byte[] RequestData = "Client Requesting Service".getBytes();
+		
+		System.out.println("Will now attempt to connect to server at " + server + ", Port "  + servPort  );
+		
+		
+		//Connect to the server
 		Socket socket = new Socket(server, servPort);
-
-		handleSession( socket );
+		System.out.println("Connected to server");
 		
+
+		int count = 0;
+		while(count < 10){
+			try {
+				Thread.sleep(3000);
+			} catch (Exception ex) {}
+			//wait till confirmation received from server then do handle session for encoding
+			if(ackValue){
+				handleSession( socket );
+			}
+			else if(!ackSent){
+				//Begin handshake
+					handShake ( socket, RequestData );
+					ackSent = true;
+				}
+			count++;
+			if(count == 10){
+			//user idleness, depends on how it should be implemented
+				System.out.println("\nTime taken too long for server to respond, sending another handshake");
+				count = 0;
+				ackSent = false;
+			}
+		}
 		socket.close();
+		
 	}
 	static void testArgs( int numArgs ) throws IOException
 	{
 			throw new IllegalArgumentException( "Required parameter(s): ServerName/IP (Port#)" );
 	}
 	
-	static void handleSession( Socket serverConn ) throws IOException
+	static void handShake( Socket serverConn, byte[] request ) throws IOException
+	{
+		String acknowledgement = "Ready To Help";
+		//server response to receive
+		byte[] response = new byte[ACKSIZE];
+		//Expect Ready to help string response from server
+	
+		//System.out.println("Attempting Handshake");
+		InputStream in = serverConn.getInputStream();
+		OutputStream out = serverConn.getOutputStream();
+		
+		//Send confirmation data to server
+		out.write(request);
+		
+		//wait for confirmation from Server
+		int totalBytesRcvd = 0;
+		int bytesRcvd;
+		
+		in.read( response );
+		String responseString = new String(response);
+		if(responseString.equals(acknowledgement)){
+			ackValue = true;
+		}
+		/*
+		while(totalBytesRcvd < acknowledgement.length){
+			if((bytesRcvd = in.read(response, totalBytesRcvd, response.length - totalBytesRcvd)) == -1)
+				throw new SocketException("Connection closed prematurely");
+                        
+			totalBytesRcvd += bytesRcvd;
+		}*/
+		
+	}
+	
+	
+	static void handleSession( Socket serverConn  ) throws IOException
 	{
+		System.out.println("Works, Continue messaging from here");
+		serverConn.close();
+		/*
 		InputStream in = serverConn.getInputStream();
 		OutputStream out = serverConn.getOutputStream();
+		
+		
 
 		byte[] byteBuffer = "basic I/O".getBytes(); // REPLACE
 		out.write( byteBuffer );
 
 		while ( in.read( byteBuffer ) != -1 )
 			System.out.println( "Received: " + new String( byteBuffer ) ); // REPLACE
+			*/
 	}
 	
 	/*		( potential algorithm )
 public class Server
 {
 	private static final int BUFSIZE = 32;   // Size of receive buffer
+	private static final int ACKSIZE = 25;	//Size of the ACK buffer
 	private static byte[] byteBuffer;
-
+	private static boolean ackValue;
+	
 	public static void main( String[] args ) throws IOException
 	{
 		testArgs( args.length );	
 		int servPort = Integer.parseInt(args[0]);
 		ServerSocket servSock = new ServerSocket(servPort);
 		byteBuffer = new byte[ BUFSIZE ];
+		
+		ackValue = false;//initial setting for the hand
+		
+		
+		System.out.println("Server is now ready to receive client at port " + servPort);
+		//Setup of the sockets, in, and out
+		Socket clntSock = servSock.accept();
+		SocketAddress clientAddress = clntSock.getRemoteSocketAddress();
+		while( true ){
+			//Setup of the sockets, in, and out
+			//Socket clntSock = servSock.accept();
+			//SocketAddress clientAddress = clntSock.getRemoteSocketAddress();
+			System.out.println("Handling client at " + clientAddress);
 
-		while( true )
-			handleClients( servSock );
-		//servSock.close();
-	}
+			if(ackValue)
+			{
+				handleClients( servSock, clntSock );
+				break;
+				}
+			else
+			
+				handShake( servSock, clntSock );
+          }
+		servSock.close();
+     }
+	
 	static void testArgs( int numArgs ) throws IOException
 	{
 		if ( numArgs != 1 )
 			throw new IllegalArgumentException( "Only parameter: Port#" );
 	}
 	
-	static void handleClients( ServerSocket servSock ) throws IOException
+	static void handShake( ServerSocket servSock, Socket clntSock) throws IOException
 	{
-		Socket clntSock = servSock.accept();
+		System.out.println("Checking for handshake");
+		String expectedHand = "Client Requesting Service";
+		byte[] acknowledgement = "Ready To Help".getBytes(); 
+		byte[] receiveHand = new byte[ACKSIZE];
+
 		InputStream in = clntSock.getInputStream();
 		OutputStream out = clntSock.getOutputStream();
-
+		
+		while ( true )
+		{
+			in.read( receiveHand );
+			String received = new String(receiveHand);
+			String ack = new String(acknowledgement);
+			System.out.println(ack.length());
+			if(received.equals(expectedHand))
+			{
+				out.write( acknowledgement ); // echoing action // REPLACE
+				ackValue = true;
+				break;
+			}
+			else{
+				System.out.println("Client request string rejected");
+			}
+		}
+		
+		System.out.println("Received: " + new String(receiveHand));
+	}
+	
+	static void handleClients( ServerSocket servSock,  Socket clnt ) throws IOException
+	{
+		System.out.println("Continue Messaging from here");
+		servSock.close();
+		//InputStream in = clntSock.getInputStream();
+		//OutputStream out = clntSock.getOutputStream();
+		
+		//Encoding work will continue here.
+		/*
 		while ( in.read( byteBuffer ) != -1 )
 		{
 			for ( byte nn : byteBuffer )
 				System.out.print( (char) nn ); // printing to screen // REPLACE
 			out.write( byteBuffer ); // echoing action // REPLACE
 		}
-		clntSock.close();
+                clnt.close();
+				*/
+		
 	}
 	
 	/*		( potential algorithm )