ITP4723辅导、Java程序调试、辅导TCP Socket、讲解Java设计 解析R语言编程|解析Haskell程序
- 首页 >> 其他 Hong Kong Institute of Vocational Education
Department of Information Technology
ITP4723
Lab 7 – TCP Socket Programming in Android
Exercise 1 – Simple Android Client for an echo server
Extract the EchoServer from NetworkSocketServer.zip.
Start the EchoServer on the host computer by typing startserver.bat in command prompt.
Import the partial project EchoClientSample
Declare the related permission(s) in AndroidManifest.xml
Completing the missing part of the following file.
MainActivity.java
Fill in the ip address to the String ip in onClick()
Send message by calling write() method.
Invoke flush()
Receive the echo message from server by calling readLine() method.
Send EXIT message and followed by a flush().
Test the Android Client with the EchoServer and understand the code behind before continue next questions.
Exercise 2 – EchoClientUISample – an enhanced version of android client in ex.1
Using the same server in ex.1.
Create a new project.
Create an Android Client UI that allows user to input the following:
IP Address
Port Number
Echo Message
Display the echo message from the server after pressing Send button.
Tips: You can apply the code learned in ex.1 to complete this exercise.
Ex. 2 Ex. 3
Exercise 3 – Android Chat Room Client
Extract ChatRoomClientServer.zip
Start the Chat Room Server
Import the partial project SocketChatClient
Complete the missing part so that it fulfill the following requirement.
Initially, edit text for inputting message and the send button are disabled.
Enable them after connected to the ChatServer.
After clicking Connect, the app connected to chat server. The edit text for name and connect button disabled.
Messages received continuously.
You may test it by starting either:
2 Android Clients or
1 Android client and Java GUI
Note: to start Java GUI version of Chat Client by typing “java ”
Bonus Exercise (Optional) – Create a simple game server with an Android client
You have to create a multiplayer mini and simple game. The description of the game as below:
Server
Keeps an array (or other data structure) to store the player name and Heath Point (HP) of all active players
Accept multiple clients
When the clients connect, store the player name together with HP. Initially, the HP is 100 by default.
Send the list of the player with HP to the client.
When the “ATTACK” command received from one client, the HP of player being attacked reduce from 1 to 10 by generating a random number from 1 to 10. A message “SOMEBODY reduced 3 HP” is broadcast to all players. The player is being attacked receive its own updated HP.
The server disconnect with the client when HP drop to 0 or below or receive a “QUIT” command from the client.
Broadcast a message when a player left or die.
Client
Connects server by specifying player name, IP address and port.
Receive the list of player with name and player’s own HP after connecting to the server.
Player can input attack command by typing “ATTACK OTHER_PLAYER_NAME” and the client application sends the command to server.
Receive broadcast messages continuously.
After user type “QUIT” command to quit the application.
Player1 Server Player2
Connect-> Player1:100
Player1:100 < Player1:100
Player2:100 <-Connect
Player2 entered < > Player2:100
ATTACK Player2 >
Player2 reduced 4 HP < Player1:100
Player2:96
> Player2 reduced 4 HP
Player1 reduced 1 HP < Player1:99
Player2:96 < ATTACK Player 1
> Player1 reduced 1 HP
… … …
Player1 reduced 3 HP < Player1:2
Player2:10 < ATTACK Player 1
> Player1 reduced 3 HP
Player1 Died <
Disconnect<- Player2:10 > Player1 Died
Create a JAVA program for the Game Server
Create a new Android project called MyGame
Make your own assumption.
End.
Department of Information Technology
ITP4723
Lab 7 – TCP Socket Programming in Android
Exercise 1 – Simple Android Client for an echo server
Extract the EchoServer from NetworkSocketServer.zip.
Start the EchoServer on the host computer by typing startserver.bat in command prompt.
Import the partial project EchoClientSample
Declare the related permission(s) in AndroidManifest.xml
Completing the missing part of the following file.
MainActivity.java
Fill in the ip address to the String ip in onClick()
Send message by calling write() method.
Invoke flush()
Receive the echo message from server by calling readLine() method.
Send EXIT message and followed by a flush().
Test the Android Client with the EchoServer and understand the code behind before continue next questions.
Exercise 2 – EchoClientUISample – an enhanced version of android client in ex.1
Using the same server in ex.1.
Create a new project.
Create an Android Client UI that allows user to input the following:
IP Address
Port Number
Echo Message
Display the echo message from the server after pressing Send button.
Tips: You can apply the code learned in ex.1 to complete this exercise.
Ex. 2 Ex. 3
Exercise 3 – Android Chat Room Client
Extract ChatRoomClientServer.zip
Start the Chat Room Server
Import the partial project SocketChatClient
Complete the missing part so that it fulfill the following requirement.
Initially, edit text for inputting message and the send button are disabled.
Enable them after connected to the ChatServer.
After clicking Connect, the app connected to chat server. The edit text for name and connect button disabled.
Messages received continuously.
You may test it by starting either:
2 Android Clients or
1 Android client and Java GUI
Note: to start Java GUI version of Chat Client by typing “java
Bonus Exercise (Optional) – Create a simple game server with an Android client
You have to create a multiplayer mini and simple game. The description of the game as below:
Server
Keeps an array (or other data structure) to store the player name and Heath Point (HP) of all active players
Accept multiple clients
When the clients connect, store the player name together with HP. Initially, the HP is 100 by default.
Send the list of the player with HP to the client.
When the “ATTACK” command received from one client, the HP of player being attacked reduce from 1 to 10 by generating a random number from 1 to 10. A message “SOMEBODY reduced 3 HP” is broadcast to all players. The player is being attacked receive its own updated HP.
The server disconnect with the client when HP drop to 0 or below or receive a “QUIT” command from the client.
Broadcast a message when a player left or die.
Client
Connects server by specifying player name, IP address and port.
Receive the list of player with name and player’s own HP after connecting to the server.
Player can input attack command by typing “ATTACK OTHER_PLAYER_NAME” and the client application sends the command to server.
Receive broadcast messages continuously.
After user type “QUIT” command to quit the application.
Player1 Server Player2
Connect-> Player1:100
Player1:100 < Player1:100
Player2:100 <-Connect
Player2 entered < > Player2:100
ATTACK Player2 >
Player2 reduced 4 HP < Player1:100
Player2:96
> Player2 reduced 4 HP
Player1 reduced 1 HP < Player1:99
Player2:96 < ATTACK Player 1
> Player1 reduced 1 HP
… … …
Player1 reduced 3 HP < Player1:2
Player2:10 < ATTACK Player 1
> Player1 reduced 3 HP
Player1 Died <
Disconnect<- Player2:10 > Player1 Died
Create a JAVA program for the Game Server
Create a new Android project called MyGame
Make your own assumption.
End.