1. Trang chủ
  2. » Giáo án - Bài giảng

Networking exercises JAVA

8 264 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

1 EXERCISES 1. Create a server that asks for a password, then opens a file and sends the file over the network connection. Create a client that connects to this server, gives the appropriate password, then captures and saves the file. Test the pair of programs on your machine using the localhost (the local loopback IP address 127.0.0.1 produced by calling InetAddress.getByName(null)). 2. Modify the server in Exercise 2 so that it uses multithreading to handle multiple clients. 3. Build on ShowHTML.java to produce an applet that is a password-protected gateway to a particular portion of your Web site. 4. (More challenging) Create a client/server pair of programs that use datagrams to transmit a file from one machine to the other. (See the description at the end of the datagram section of this chapter.) 5. (More challenging) Take the VLookup.java program and modify it so that when you click on the resulting name it automatically takes that name and copies it to the clipboard (so you can simply paste it into your email). You’ll need to look back at the IO stream chapter to remember how to use the Java 1.1 clipboard. 3. Distinguish between connection-oriented and connectionless network services. 4. How does a client determine the host name of the client computer? 5. Under what circumstances would a SocketException be thrown? 6. How can a client get a line of text from a server? 7. Describe how a client connects to a server. 8. Describe how a server sends data to a client. 9. Describe how to prepare a server to receive a stream-based connection request from a single client. 10. Describe how to prepare a server to receive connection requests from multiple clients if each client that connects should be processed in parallel with all other connected clients. 11. How does a server listen for connections at a port? 12. What determines how many connect requests from clients can wait in a queue to connect to a server? 13. As described in the text, what reasons might cause a server to refuse a connection request from a client? 14. Use a socket connection to allow a client to specify a file name and have the server send the contents of the file or indicate that the file does not exist. 15. Modify Exercise 14 to allow the client to modify the contents of the file and send the file back to the server for storage. The user can edit the file in a JTextArea, then click a save changes button to send the file back to the server. 16. Modify program of Fig. 2 to allow users to add their own sites to the list and remove sites from the list. 17. Multithreaded servers are quite popular today, especially because of the increasing use of multiprocessing servers. Modify the simple server application presented in Section 6 to be a multithreaded server. Then use several client applications and have each of them connect to the server simultaneously. Use a Vector to store the client threads. Vector provides several methods of use in this exercise. Method size determines the number of elements in a Vector. Method elementAt returns the element in the specified location (as an Object reference). Method add places a new element at the end of the Vector. Method remove deletes its argument from the Vector. Method lastElement returns an Object reference to the last object you inserted in the Vector. 18. In the text, we presented a tic-tac-toe program controlled by a multithreaded server. Develop a checkers program modeled after the tic-tac-toe program. The two users should alternate making moves. Your program should mediate the players’ moves, determining whose turn it is and allowing only valid moves. The players themselves will determine when the game is over. 19. Develop a chess-playing program modeled after the checkers program in the Exercise 18. 2 20. Develop a Blackjack card game program in which the server application deals cards to each of the client applets. The server should deal additional cards (as per the rules of the game) to each player as requested. 21. Develop a Poker card game in which the server application deals cards to each of the client applets. The server should deal additional cards (as per the rules of the game) to each player as requested. 22. (Modifications to the Multithreaded Tic-Tac-Toe Program) The programs of Fig. 8 and Fig. 9 implemented a multithreaded, client/server version of the game Tic-Tac-Toe. Our goal in developing this game was to demonstrate a multithreaded server that could process multiple connections from clients at the same time. The server in the example is really a mediator between the two client applets—it makes sure that each move is valid and that each client moves in the proper order. The server does not determine who won or lost or if there was a draw. Also, there is no capability to allow a new game to be played or to terminate an existing game. The following is a list of suggested modifications to the multithreaded Tic-Tac-Toe application and applet. a) Modify the TicTacToeServer class to test for a win, loss or draw on each move in the game. Send a message to each client applet that indicates the result of the game when the game is over. b) Modify the TicTacToeClient class to display a button that when clicked allows the client to play another game. The button should be enabled only when a game completes. Note that both class TicTacToeClient and class TicTacToeServer must be modified to reset the board and all state information. Also, the other TicTacToeClient should be notified that a new game is about to begin so its board and state can be reset. c) Modify the TicTacToeClient class to provide a button that allows a client to terminate the program at any time. When the user clicks the button, the server and the other client should be notified. The server should then wait for a connection from another client so a new game can begin. d) Modify the TicTacToeClient class and the TicTacToeServer class so the winner of a game can choose game piece X or O for the next game. Remember: X always goes first. e) If you would like to be ambitious, allow a client to play against the server while the server waits for a connection from another client. 23. (3-D Multithreaded Tic-Tac-Toe) Modify the multithreaded, client/server Tic-Tac-Toe program to implement a three-dimensional 4-by-4-by-4 version of the game. Implement the server application to mediate between the two clients. Display the three-dimensional board as four boards containing four rows and four columns each. If you would like to be ambitious, try the following modifications: a) Draw the board in a three-dimensional manner. b) Allow the server to test for a win, loss or draw. Beware! There are many possible ways to win on a 4-by-4-by-4 board! 24. (Networked Morse Code) Write your solution to create two applets to send Morse Code messages to each other through a multithreaded server application. Each applet should allow the user to type normal characters in JTextAreas, translate the characters into Morse Code and send the coded message through the server to the other client. When messages are received, they should be decoded and displayed as normal characters and as Morse Code. The applet should have two JTextAreas: one for displaying the other client’s messages and one for typing. SELF-REVIEW EXERCISES 1. Fill in the blanks in each of the following statements: a) Exception occurs when an input/output error occurs when closing a socket. b) Exception occurs when a server address indicated by a client cannot be resolved. c) If a DatagramSocket constructor fails to set up a DatagramSocket properly, an exception of type occurs. d) Many of Java’s networking classes are contained in package . e) Class binds the application to a port for datagram transmission. f) An object of class contains an Internet address. g) The two types of sockets we discussed in this chapter are sockets and sockets. h) The acronym URL stands for . i) The acronym URI stands for . 3 j) The key protocol that forms the basis of the World Wide Web is . k) InetAddress method getLocalHost returns an object containing the local host name of the computer on which the program is executing. l) Method of class MulticastSocket subscribes the MulticastSocket to a multicast group. m) The URL constructor determines whether the String passed as an argument represents a valid Uniform Resource Identifier. If so, the URL object is initialized to contain the URI; otherwise, an exception of type occurs. 2. State whether each of the following is true or false. If false, explain why. a) An application that uses multicast broadcasts DatagramPackets to every host on the Internet. b) UDP is a connection-oriented protocol. c) With stream sockets a process establishes a connection to another process. d) A server waits at a port for connections from a client. e) Datagram packet transmission over a network is reliable—packets are guaranteed to arrive in sequence. f) For security reasons, many Web browsers such as Netscape Communicator allow Java applets to do file processing only on the machines on which they execute. g) Web browsers often restrict an applet so that it can only communicate with the machine from which it was originally downloaded. h) IP addresses in the range from 224.0.0.0 to 239.255.255.255 are reserved for multicast. 4 SOCKET PROGRAMMING Bài 1: Viết 1 chương trình có tên Calculator_server nhận 1 biểu thức gồm 2 chữ số và 1 phép toán sau đó thực thi biểu thức này và gửi kết quả lại cho client. Sữa chữa chương trình cho phép nhiều client kết nối cùng lúc. Phía client, viết giao diện gồm 2 JtextField cho việc nhập số, 1 JLabel cho việc xuất kết quả. Các nút Cộng, trừ, nhân,chia, clear và thoát. Giao diện cho client như sau: Bài 2: Viết 1 server cho phép nhiều client kết nối cùng lúc với các yêu cầu sau: Client có thể gửi yêu cầu là đường dẫn đến 1 ổ điã hoặc 1 thư mục nào đó bất kỳ trên server. Nếu đường dẫn đó tồn tại thị sẽ gửi về danh sách các thư mục con và các tập tin trong ổ đĩa / đường dẫn đó. Thiết kế client với cơ chế GUI nhận kết quả từ server và biểu diễn kết quả nhận được lên 1 JTree. Hướng dẫn: Đoạn code sau liệt kê tất cả thư mục, tập tin trong 1 đường dẫn path cho trước rồi đưa vào 1 đối tượng ArrayList File f=new File(path); ArrayList<File>lstFiles=null; if(f.exists()&& f.isDirectory()) { lstFiles=new ArrayList<File>(); File []files=f.listFiles(); for(File x:files) lstFiles.add(x); } } Đoạn code sau tạo luồng có thể chuyển 1 đối tượng tới client OutputStream os = income.getOutputStream(); ObjectOutputStream oos=new ObjectOutputStream(os); oos.writeObject(lstFiles); oos.flush(); Đoạn code sau đây nhận đối tượng từ server ObjectInputStream ois=new ObjectInputStream(soc.getInputStream()); ArrayList<File>list=(ArrayList<File>)ois.readObject(); Giao diện phía client như sau: 5 Bài 3: Cải tiến bài 2 thành 1 chương trình quản lý thư mục từ xa và cho phép các thao tác cơ bản như download, delete files / folders. Bài 4: Viết 1 chương trình từ điển cho phép tra từ qua mạng. VIệc tra từ này phải đảm bảo nhiều người có thể tra cùng lúc. Việc thiết kế chương trình gồm 2 phần: Phần server và phần client. Phần server: chỉ sẽ được chạy trên server có giao diện như sau: 6 Khi người dùng start server từ điển, server này sẽ lắng nghe trên cổng 2520 và sẽ nhận đầu vào là từ cần tra sau đó thực hiện việc tra từ và trả kết quả về cho client hoặc là 1 đối tượng từ đã tra trong trường hop745 tra thành công, hoặc là null nếu từ không tồn tại. Phần Client: sẽ được triển khai ở phía client, có giao diện như sau 7 UDP PROGRAMMING 1. Viết 1 chương trình làm server sử dụng UDP cho phép lắng nghe trên cổng 2520. Khi client gửi yêu cầu đến thì server này sẽ gửi 1 file hình ảnh về client. Ở phía client, sau khi nhận được image từ server thì lưu image này xuống file đồng thời hiển thị lên 1 JFrame. 2. Viết chương trình làm server sử dụng UDP cho phép lắng nghe trên cổng 2520. Khi client gửi yêu cầu đến thì server này sẽ gửi 1 file audio/video về client. Ở phía client, sau khi nhận được file audio/video từ server sẽ lưu file này xuống đĩa và cho thực file vừa nhận được. 3. V 8 RMI PROGRAMMING 1. Viết chương trình dùng kỹ thuật RMI cho việc chuyển đổi tiền tệ.

Ngày đăng: 13/05/2014, 11:15

Xem thêm: Networking exercises JAVA

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN

w