Out: Oct 5
Due: Oct 26 – Start of Class
(No class/lab on Oct 12th – I will be away)
CENG 320 – Lab 3
- Using the JDK API and the Unix man pages as references, create a chart comparing the following C functions and their arguments with the Java equivalents. In some cases Java methods will do the work of more than one function (4 marks)
- getaddr_info
- socket
- bind
- listen
- connect
- accept
- recv
- send
- Failure proofing the connection. (10 marks)
In C construct single threaded non-forking server listening on your assigned port with a queue capacity of 5. On accepting a connection the server simply goes to sleep for 1 second then sends the current date and time to the client. Construct a separate client that keeps forking off children at the rate of 1 per half second. - Initially test your client by having it create 7 children. The server should be able to handle this without any problems. Show the result of testing (3 marks)
- Increase the number of children created by your client to 30. You should now start running into a problem of being rejected by the server. Measure the number of successes and rejections. You can have the child return zero or an error code and have the client’s parent process do the count. (2)
- Fix it by having the client’s child process detect the problem and, after of a quarter second to 1 second, retrying 3 or 4 times before quitting with a message that you are timing out. Again measure the number of successes and rejections. [A rejection is a failure to connect at all.] If this works properly the number of rejections should go down but not to zero. (3 marks)
- Write up a status report along with any problems you had during development and how you solved them. (2 marks)
- Building a simple synchronous chat in Java made up of 2 programs based on Sender and Receiver (5 marks)
Using your assigned port 400xx alter the programs Sender.java and Receiver.java into a synchronized 2-way conversation between 2 machines. You can test this on munro to apollo or on pairs of machines in the lab that are in the same block of desks or from your desktop/laptop to Linux. The machines in J212 are organized into 2 blocks that are physically connected. If your benches are touching you should be inside the same subnet. (4 marks) Demonstrate a working program in your scheduled lab of Oct 25/26 (or earlier – this is a fairly straightforward exercise) where one of the programs is running under Windows and the other is running on your personal Linux/OS X image. (1 mark)
Your write up should describe the setup you used, a status report, and a screen shot showing an exchange of at least 2 remarks on both sides. - Creating a unified asynchronous chat program in Java. Parts a) and b) can be done in either order. (7 marks)
- Create a single program that determines if a server on port 400xx is running on the target machine. If it isn’t then it checks if the server port 400xx is available on the current machine. If either is true it attempts to become a client. Otherwise it sets itself up as the server. Essentially the main program should try to do each of the above and then hand off processing to either a method or a different class. The program should output one of the following messages: (2 marks)
- I am a server listening on port 400xx
- I am a client connected to port 400xx on machine machine name or address
- Failed to connect
- Using threads, turn the conversation into an asynchronous one where either side can send a message at any time. (5 marks)
- This question is part of Lab 4. I’m presenting it early so that if you finish Lab 3 you can get started.
Create a blackjack dealer. We are not implementing the entire game of blackjack. (10 marks) - Create java classes Card and Deck. Card contains fieldssuit, rank, and value. suitis a string or an enum and one of spades, hearts, diamonds or clubs. Rank is one of ace, 2-10, jack, queen or king. Value is the value in blackjack of a card which is the face value of 2-10 for numeric cards, 10 for a jack, queen or king and either 1 or 11 for an ace. The value of an ace is determined by the a player at time of use in the game
Create a constructor for Card, a getter and setter for value and a toStringmethod which will allow you to display the card. (2) - The constructor for Deck should contain an ArrayList (or a Stack, Queue, Vector or List) of 52 cards, initialized in order, then shuffled using the static method Collections.shuffle. It should also maintain an index to keep track of which card is the next one to deal. (1)
- Modify a new version of your Client and Server code in either question #3 or #4 so that instead of transmitting Strings, it transmits Objects using readObject and writeObject. Verify that your program(s) still work. (2)
Create an Array or a subclass of Array List , Vector in your client called handto hold cards that the client will receive in the next steps. - Modify your client and server program(s) again so that they recognize the command deal. The server responds by sending the next card from the deck which the client then displays. The client receives the Card object and adds it to the hand. If you don’t shuffle the deck, you simply get the cards in order, which is OK from the point of view of testing. If the client gets an Ace they then should decide whether or not the value is 1 or 11 right away. [In a real game they get to decide at any time.]
Test by dealing 3 cards and verifying that they are different.
(2 marks) - Create a command show on the client that shows the entire hand collected to date. No communication with the server is required. (1 mark)
- If the client issues the command stay, instead of sending the command, the client sends the whole handas a single object. The server accepts the hand, adds up the values and reports the sum on both to the client and the server.
The server and client should modify how they respond to each other. Instead of expecting strings it should test the class of the object it receives in order to determine what it should do with it using the instanceofoperator. If the class of the object sent is is a String, handle it as a command or a message or command. If it is an instanceof Card, which only happens on the Client, add it to hand. If it’s an instance of whatever you chose for hand - which only happens to the Server - send back an evaluation of the hand. (3 marks) - Bonus: Any extra functionality. For example, add a command new gamewhich causes the deck to be reinitialized, or add code which deals with the problem of running out of cards – what would the server send to the client and how would the client interpret it.