Commits

Nzen  committed faca36f

Working, impatient server & numberformaterr

They put out the right handshaking notices but server moves on. Client is
gettting "fail" from somewhere even after I took it out of 4b5bd( ) or
whatever. Uploading this so you aren't afeared that we will have nothing.
This is so close.

  • Participants
  • Parent commits 5ee4090
  • Branches np

Comments (0)

Files changed (2)

     private static boolean connected;
 	private static final int BUFSIZE = 32;	//Size of the ACK buffer
 	private static PhysicalLayer ethernet;
+	private static DataInputStream in;
+	private static DataOutputStream out;
 	
 	public static void main( String[] args ) throws IOException
 	{
 		testArgs( args.length );
 		String server = args[ 0 ];       // Server name/IP	
 		int servPort = ( args.length == 2 ) ? Integer.parseInt(args[1]) : 7;
-		
 		System.out.println("Connecting to server at " + server + ", Port "  + servPort  );
-		
 		Socket serverConn = new Socket(server, servPort);
+		
+		InputStream inPath = serverConn.getInputStream();
+		in = new DataInputStream( inPath );
+		OutputStream outPath = serverConn.getOutputStream();
+		out = new DataOutputStream( outPath );
+		
 		handleSession( serverConn );
 		serverConn.close();
 		
 	}
+	
 	static void testArgs( int numArgs ) throws IOException
 	{
 		if ( numArgs < 1 || numArgs > 2 )
 	 */
 	static void handShake( Socket serverConn ) throws IOException
 	{
-		byte[] response = new byte[BUFSIZE];
-
-		InputStream in = serverConn.getInputStream();
-		OutputStream out = serverConn.getOutputStream();
-		
-		in.read( response );		
+		String response = in.readUTF( );		
 		// listen for idle server
-		if ( ethernet.whichSpecialInput( ethernet.decode( new String( response ) ) ) != Comm.idle )
+		if ( ethernet.whichSpecialInput( response ) != Comm.idle )
 		{
-			System.out.println( "Server busy or connection lost" );
+			System.out.println( "Server busy or connection lost " );
 			return; // & sleep
 		}
 		else
 		{
 			System.out.println( "Found idle server, requesting connection" );
 			// ask to connect
-			out.write( ethernet.specialOut( Comm.startAsk ).getBytes( ) );
-			in.read( response );
-			if ( ethernet.whichSpecialInput( ethernet.decode( new String( response ) ) ) != Comm.startAck )
+			out.writeUTF( ethernet.specialOut( Comm.startAsk ) );
+			response = in.readUTF( );
+			if ( ethernet.whichSpecialInput( response ) != Comm.startAck )
 			{
 				System.out.println( "Connection lost" );
 				return; // connection lost; sleep
 				connected = true;
 			}
 		}
+		/*
+	    String message = inDataStream.readUTF ();
+	  
+	    outDataStream.writeUTF ( message );
+		String fiB4B=fiveB4B(message);
+			String Decode=decode(fiB4B);
+		 
+		 	String Encode= encode(message);
+	    outDataStream.writeUTF ( Encode );
+
+	      while ( true ) {
+	          message = inDataStream.readUTF ();
+	          msgDisplay.appendText ( message );
+		 */
 	}
 	
 	static boolean communicate( Socket serverConn ) throws IOException
 	{
 		String reply;
+		String message;
 		boolean unfinished = true;
-		byte[ ] byteBuffer = new byte[ BUFSIZE ];
-		InputStream in = serverConn.getInputStream();
-		OutputStream out = serverConn.getOutputStream();
 		while( unfinished )
 		{
-			byteBuffer = ethernet.encode( userMessage( ) ).getBytes( );
-			out.write( byteBuffer );
-			in.read( byteBuffer );
-			reply = ethernet.decodeCompletely( new String( byteBuffer ) );
+			message = ethernet.encode( userMessage( ) );
+			out.writeUTF( message );
+			reply = ethernet.decodeCompletely( in.readUTF( ) );
 			// print response
 			System.out.print( "Server:\t" + reply );
 			unfinished = messageIncomplete( );
 		}
-		out.write( ethernet.specialOut( Comm.end ).getBytes( ) );
+		out.writeUTF( ethernet.specialOut( Comm.end ) );
 		return true; // finished
 	}
 	
 		{
 			System.out.print( " -- " );
 			message = uInput.nextLine( );
-			if ( message.length( ) >= BUFSIZE / 2 )
+			if ( message.length( ) >= BUFSIZE / 2 ) // this is unneeded now.
 				System.out.println( "Message too long. Please twitterfy to 15 chars." );
 			else
 				return message;
 	private static byte[] byteBuffer;
 	private static boolean connected;
 	private static PhysicalLayer ethernet;
+	private static DataInputStream in;
+	private static DataOutputStream out;
 	
 	public static void main( String[] args ) throws IOException
 	{
 		Socket clntSock = servSock.accept();
 		SocketAddress clientAddress = clntSock.getRemoteSocketAddress();
 		System.out.println("Sensed client at " + clientAddress);
+
+		InputStream inPath = clntSock.getInputStream();
+		in = new DataInputStream( inPath );
+		OutputStream outPath = clntSock.getOutputStream();
+		out = new DataOutputStream( outPath );
 		handleSession( clntSock );
 		servSock.close();
      }
 			tries--;
         }
 	}
-	
+	/*
+      String message = inDataStream.readUTF ();
+    
+      outDataStream.writeUTF ( message );
+	String fiB4B=fiveB4B(message);
+		String Decode=decode(fiB4B);
+	 
+	 	String Encode= encode(message);
+      outDataStream.writeUTF ( Encode );
+
+        while ( true ) {
+            message = inDataStream.readUTF ();
+            msgDisplay.appendText ( message );
+	 */
 	static boolean handShake( Socket clntSock ) throws IOException
 	{
-		String nn;
-		byte[] response = new byte[BUFSIZE];
-		InputStream in = clntSock.getInputStream();
-		OutputStream out = clntSock.getOutputStream();
-		if ( false )//badState( ) )
+		String response;
+		if ( badState( ) )
 		{
-			System.out.println( "Server busy" );
-			byteBuffer = ethernet.specialOut( Comm.quiet ).getBytes( );
-			out.write( byteBuffer );
+			response = ethernet.specialOut( Comm.quiet );
+			System.out.println( "Server busy " + response );
+			out.writeUTF( response );
 			return false; // no success
 		}
 		else
 		{
-			System.out.println( "Server idle" + ethernet.specialOut( Comm.idle ) );
-			nn = ethernet.specialOut( Comm.idle );
-			byteBuffer = nn.getBytes( );
-			out.write( byteBuffer );
-			in.read( response );
-			if ( ethernet.whichSpecialInput( new String( response ) ) != Comm.startAsk )
+			response = ethernet.specialOut( Comm.idle );
+			System.out.println( "Server idle " + response );
+			out.writeUTF( response );
+			response = in.readUTF( );
+			if ( ethernet.whichSpecialInput( response ) != Comm.startAsk )
 			{
 				System.out.println( "Ambiguous client request, closing" ); // extremely unlikely
 				return false; // no success
 			}
 			else if ( badState(  ) )
 			{
-				System.out.println( "Line quiet: connection lost" );
+				response = ethernet.specialOut( Comm.quiet );
+				System.out.println( "Line quiet: connection lost " + response );
+				out.writeUTF( response );
 				return false; // no success
 			}
 			else
 			{
-				System.out.println( "Start request acknowledged" );
+				response = ethernet.specialOut( Comm.startAck );
+				System.out.println( "Start request acknowledged " + response );
+				out.writeUTF( response );
 				return true; // woo!
 			}
 		}
 	
 	static boolean badState( )
 	{
-		Random gen = new Random( 42 );
+		Random gen = new Random( );
 		return gen.nextBoolean( );
 	}
 	static void communicate( Socket clntConn ) throws IOException // FIX, THIS IS CLIENT CODE 4 25, 2:39am
 	{
 		String reply;
 		Comm flag;
-		byteBuffer = new byte[ BUFSIZE ];
-		InputStream in = clntConn.getInputStream();
-		OutputStream out = clntConn.getOutputStream();
 		while( true )
 		{
-			in.read( byteBuffer );
-			flag = ethernet.whichSpecialInput( ethernet.decode( new String( byteBuffer ) ) );
+			reply = in.readUTF();
+			flag = ethernet.whichSpecialInput( reply );
 			if ( flag == Comm.end )
 			{
-				System.out.println( "Communication complete, closing socket" );
+				System.out.println( reply + " Communication complete, closing socket" );
 				return;
 			}
 			else if ( flag == Comm.NULL ) // ie text
 			{
-				reply = ethernet.decodeCompletely( new String( byteBuffer ) );
+				reply = ethernet.decodeCompletely( reply );
 				System.out.print( "Client:\t" );
 				reply.toUpperCase( );
-				byteBuffer = ethernet.encode( reply ).getBytes( );
-				out.write( byteBuffer );
+				reply = ethernet.encode( reply );
+				out.writeUTF( reply );
 			}
 			else
 				System.out.println( "Ambiguous client request\t" ); // unreachable?