Commits

Nzen committed e491775

Updated client/server

Refactored client & server files & marked areas to replace with new code.
Each still needs a PhysicalLayer (which needs decoding functionality) and
handshaking protocols.

I also removed the regex from whichSpecialInput(), replacing it with
str.equals(). This means the commands must be sent separate from messages,
but that was my intention from the start anyway. Further, the regex would
have considered the whole message a command rather than picking it out.
Another option involves adding the sequences to decode and flagging a
command field.

Comments (0)

Files changed (4)

-import java.net.*;  // for Socket
-import java.io.*;   // for IOException and Input/OutputStream
-
-public class Client {
-
-  public static void main(String[] args) throws IOException {
-
-    if ((args.length < 2) || (args.length > 3))  // Test for correct # of args
-      throw new IllegalArgumentException("Parameter(s): <Server> <Word> [<Port>]");
-
-    String server = args[0];       // Server name or IP address
-    // Convert input String to bytes using the default character encoding
-    byte[] byteBuffer = args[1].getBytes();
-
-    int servPort = (args.length == 3) ? Integer.parseInt(args[2]) : 7;
-
-    // Create socket that is connected to server on specified port
-    Socket socket = new Socket(server, servPort);
-    System.out.println("Connected to server...sending echo string");
-
-    InputStream in = socket.getInputStream();
-    OutputStream out = socket.getOutputStream();
-
-    out.write(byteBuffer);  // Send the encoded string to the server
-
-    // Receive the same string back from the server
-    int totalBytesRcvd = 0;  // Total bytes received so far
-    int bytesRcvd;           // Bytes received in last read
-    while (totalBytesRcvd < byteBuffer.length) {
-      if ((bytesRcvd = in.read(byteBuffer, totalBytesRcvd,  
-                        byteBuffer.length - totalBytesRcvd)) == -1)
-        throw new SocketException("Connection close prematurely");
-      totalBytesRcvd += bytesRcvd;
-    }
-
-    System.out.println("Received: " + new String(byteBuffer));
-
-    socket.close();  // Close the socket and its streams
-  }
+import java.net.*;
+import java.io.*;
+
+public class Client
+{
+	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[2]) : 7;
+		Socket socket = new Socket(server, servPort);
+
+		handleSession( socket );
+		
+		socket.close();
+	}
+	static void testArgs( int numArgs ) throws IOException
+	{
+		if ( numArgs < 1 || numArgs > 2 )
+			throw new IllegalArgumentException( "Required parameter(s): ServerName/IP (Port#)" );
+	}
+	
+	static void handleSession( Socket serverConn ) throws IOException
+	{
+		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
+	}
 }

PhysicalLayer.java

 		}
 	}
 	
+	// message to bits to encoding switch, return new bits
 	String encode( String message )
 	{
 		toBitBuff( message );
 		return codedBuff.toString( );
 	}
 	
+	// string to bytes, bytes to 0/1s in bitBuff
 	void toBitBuff( String original )
 	{
-		/*
-		cast string to int array
-		each int to bit string
-		insert in bitBuff
-		*/
 		int limit = original.length();
 		int tempVal;
-		bitBuff = new StringBuilder( limit * 32 ); //? bits per char
+		bitBuff = new StringBuilder( limit * 8 ); //? bits per char
 		byte[] bytes = original.getBytes();
 
 		for ( byte b : bytes )
 			tempVal = b;
 			for ( int i = 0; i < 8; i++ )
 			{
-				bitBuff.append( ( tempVal & 128 ) == 0 ? 0 : 1 );
+				bitBuff.append( ( tempVal & 128 ) == 0 ? '0' : '1' );
 			tempVal <<= 1;
 			}
 		}
 		System.out.printf( "original bits: %s\n", bitBuff.toString() );
 	}
 	
+	// bitBuff examined in 4 char increments & encoding put in codedBuff
 	void applyEncoding( )	// warning: big, branching FSM
 	{
 		int limit = bitBuff.length( ) * 5 / 4 - 5; // for the additional bits,
-		codedBuff = new StringBuilder( );	// and the offsets below; however --AD HOC-- offset above
+		codedBuff = new StringBuilder( limit );	// and the offsets below; however --AD HOC-- offset above
 		for ( int ind = 0; ind < limit; ind += 4 ) // in nibble chunks
 		{
 			if ( bitBuff.charAt( ind ) == 0 )
 		}
 	}
 
-	/*
-		DECODING
+	/*	DECODING	*\
 	analyze for commands (react)
-	decode to "bit" string
+	decode to int string
 	convert to int, cast to char
 	display
 	*/
-	// utilizing java's regex; inclined to use bitStr.equals later
+	// assuming command is the whole message, rather than appended
 	Comm whichSpecialInput( String bitString )
 	{
-		Pattern commFind = Pattern.compile( "11111" );
-		Matcher spy = commFind.matcher( bitString );
-		if ( spy.matches( ) )
+		if ( bitString.equals( "11111" ) )
 			return Comm.idle;
 		else
 		{
-			commFind = Pattern.compile( "11000" );
-			spy = commFind.matcher( bitString );
-			if ( spy.matches( ) )
+			if ( bitString.equals( "11000" ) )
 				return Comm.startAsk;
 			else
 			{
-				commFind = Pattern.compile( "10001" );
-				spy = commFind.matcher( bitString );
-				if ( spy.matches( ) )
+				if ( bitString.equals( "10001" ) )
 					return Comm.startAck;
 				else
 				{
-					commFind = Pattern.compile( "10011" );
-					spy = commFind.matcher( bitString );
-					if ( spy.matches( ) )
+					if ( bitString.equals( "10011" ) )
 						return Comm.end;
 					else
 						return Comm.NULL; // This wasn't a command, move to decode
 		
 	}
 	
-	// assuming that client determined that this isn't special
+	// assuming that client got Comm.Null, decode normally
 	String decode( String fourB5B )
 	{
-		// ternary 0:1, multiply powers of 2 to recover int, cast char
+		// ternary 0:1, multiply powers of 2 to recover ints, cast as char
 		return "decoding not yet implemented\n";
-	}
 	/*
 	( "11110" ); // 0000
 	( "01001" ); // 0001
 	( "11011" ); // 1101
 	( "11100" ); // 1110
 	( "11101" ); // 1111
-	 * */
+	*/
+	}
 }
-import java.net.*;  // for Socket, ServerSocket, and InetAddress
-import java.io.*;   // for IOException and Input/OutputStream
-
-public class Server {
-
-  private static final int BUFSIZE = 32;   // Size of receive buffer
-
-  public static void main(String[] args) throws IOException {
-
-    if (args.length != 1)  // Test for correct # of args
-      throw new IllegalArgumentException("Parameter(s): <Port>");
-
-    int servPort = Integer.parseInt(args[0]);
-
-    // Create a server socket to accept client connection requests
-    ServerSocket servSock = new ServerSocket(servPort);
-
-    int recvMsgSize;   // Size of received message
-    byte[] byteBuffer = new byte[BUFSIZE];  // Receive buffer
-
-    for (;;) { // Run forever, accepting and servicing connections
-      Socket clntSock = servSock.accept();     // Get client connection
-
-      System.out.println("Handling client at " +
-        clntSock.getInetAddress().getHostAddress() + " on port " +
-             clntSock.getPort());
-
-      InputStream in = clntSock.getInputStream();
-      OutputStream out = clntSock.getOutputStream();
-
-      // Receive until client closes connection, indicated by -1 return
-      while ((recvMsgSize = in.read(byteBuffer)) != -1)
-	  {
-		for ( byte nn : byteBuffer )
-			System.out.print( (char) nn );
-        out.write(byteBuffer, 0, recvMsgSize);
+import java.net.*;
+import java.io.*;
+
+public class Server
+{
+	private static final int BUFSIZE = 32;   // Size of receive buffer
+	private static byte[] byteBuffer;
+
+	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 ];
+
+		while( true )
+			handleClients( servSock );
+		//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
+	{
+		Socket clntSock = servSock.accept();
+		InputStream in = clntSock.getInputStream();
+		OutputStream out = clntSock.getOutputStream();
+
+		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();
 	}
-
-      clntSock.close();  // Close the socket.  We are done with this client!
-    }
-    //servSock.close();
-  }
 }

TestPhysical.java

 public class TestPhysical
 {
 
-	public static void main(String[] args)
+	public static void main( String[] args )
 	{
 		String message = "hi";
 		String output;
 		output = link.encode( message );
 		System.out.printf( "Encoded bits: %s\n", output );
 		System.out.print( message );
+
+		System.out.println( "\tTesting input" );
 		pretestInput( link );
 		message = link.decode( output );
 	}
 	
 	public static void pretestInput( PhysicalLayer link )
 	{
-		System.out.println( "\tTesting input" );
 		System.out.println( "Should print ack, end, idle, null, ask" );
 		String[ ] inputs = { "10001", "10011", "11111", "11010", "11000" };
 		for ( String received : inputs )