Lab Exercises
- Exercise 1: Write client and server (30 minutes)
- Exercise 2: Write Multicast client and server (30 minutes)
- Homework Exercise (for people who are taking Sang Shin’s “Java Programming online course”)
Exercise 1: Writing Client and Server
(1.1) Build and run the server side code
1. Create a new NetBeans project
- Select File->New Project (Ctrl+Shift+N). The New Project dialog box appears.
- Under Choose Project pane, select Java under Categories and Java Application under Projects.
- Click Next.
- Under Name and Location pane, for the Project Name field, type in NetworkingServer as project name.
- For Create Main Class field, type in NetworkingServer. (Figure-1.10 below)
- Click Finish.
Figure-1.10: Create a new project
- Observe that NetworkingServer project appears and IDE generated NetworkingServer.java is displayed in the source editor window of NetBeans IDE.
2. Modify the IDE generated NetworkingServer.java as shown in Code-1.11 below. Study the code by paying special attention to the bold fonted parts.
/* SERVER – may enhance to work for multiple clients */ import java.net.*; import java.io.*; import java.util.*; public class NetworkingServer { public static void main(String [] args) { ServerSocket server = null; // Default port number we are going to use // Create Server side socket // Wait for the data from the client and reply try { // Listens for a connection to be made to System.out.println(“Connect request is accepted…”); // Read data from the client // Send response to the client // Close sockets } catch (IOException ie) { |
Code-1.11: NetworkingServer.java
3. Build and run the project
- Right click NetworkingServer project and select Run.
- Observe that the server is waiting for a connection request from a client in the Output window. (Figure-1.13 below)
ServerSocket is created ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=1234] Waiting for connect request… |
Figure-1.13: Result of running NetworkingSever application
Solution: This exercise up to this point is provided as a ready-to-open-and-run NetBeans project as part of hands-on lab zip file. You can find it as <LAB_UNZIPPED_DIRECTORY>/javanetworking/samples/NetworkingServer. You can just open it and run it.
(1.2) Build and run the client side code
- Select File->New Project (Ctrl+Shift+N). The New Project dialog box appears.
- Under Choose Project pane, select Java under Categories and Java Application under Projects.
- Click Next.
- Under Name and Location pane, for the Project Name field, type in NetworkingClient as project name.
- For Create Main Class field, type in NetworkingClient.
- Click Finish.
- Observe that NetworkingClient project appears and IDE generated NetworkingClient.java is displayed in the source editor window of NetBeans IDE.
2. Modify the IDE generated NetworkingClient.java as shown in Code-1.21 below. Study the code by paying special attention to the bold fonted parts.
/* CLIENT */ import java.io.*; import java.net.*; public class NetworkingClient { public static void main(String args[]) { Socket client = null; // Default port number we are going to use for (int i=0; i <10; i++) { // Create a client socket // Create an output stream of the client socket // Create an input stream of the client socket // Create BufferedReader for a standard input System.out.println(“Enter your name. Type Bye to exit. “); // Read data from standard input device and write it // Read data from the input stream of the client socket. pw.close(); // Stop the operation } catch (IOException ie) { |
Code-1.21: NetworkingClient.java
3. Build and run the project
- Right click NetworkingClient project and select Run.
- Observe the client is prompting you to enter your name. (Figure-1.23 below)
Client socket is created Socket[addr=Passion2/192.168.2.4,port=1234,localport=1775] Enter your name. Type Bye to exit. |
Figure-1.23: Waiting for the user to enter name
Trouble-shooting: If you see the following exception, it is highly likely that either you have not started the server or if you started the server, the firewall on your system blocks the incoming connection request.
I/O error java.net.ConnectException: Connection refused: connect I/O error java.net.ConnectException: Connection refused: connect I/O error java.net.ConnectException: Connection refused: connect I/O error java.net.ConnectException: Connection refused: connect |
Figure-1.24: Error condition
Solution: Make sure the server is run first. Also make sure the firewall on your system is turned off.
- Enter your name into the Input field, like Sang Shin in this example, and press Enter key. (Figure-1.25 below)
- Observe that the server responds back with “Hello, Sang Shin”.
Figure-1.25: Enter your name
- Enter a few more names and observe that the server kept sending back responses.
- Type Bye.
4. Observe the server side.
- Click NetworkingServer (run) tab to see the Output window of the server side.
- Observe that the server received a message, Sang Shin in this example. (Figure-1.27 and Figure-1.28 below.)
ServerSocket is created ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=1234] Waiting for connect request… Connect request is accepted… Client host = 192.168.2.4 Client port = 1775 Message received from client = Sang Shin Waiting for connect request… Connect request is accepted… Client host = 192.168.2.4 Client port = 1777 |
Figure-1.27: Client connection request is accepted and message is received
Figure-1.28: Client connection request is accepted and message is received
Solution: This exercise up to this point is provided as a ready-to-open-and-run NetBeans project as part of hands-on lab zip file. You can find it as <LAB_UNZIPPED_DIRECTORY>/javanetworking/samples/NeworkingClient. You can just open it and run it.
Summary
Exercise 2: Writing Multicast Client and Server
(2.1) Build and run the server side code
- Select File->New Project (Ctrl+Shift+N). The New Project dialog box appears.
- Under Choose Project pane, select Java under Categories and Java Application under Projects.
- Click Next.
- Under Name and Location pane, for the Project Name field, type in MulticastChatServer as project name.
- For Create Main Class field, type in MulticastChatServer.
- Observe that MulticastChatServer project appears and IDE generated MulticastChatServer.java is displayed in the source editor window of NetBeans IDE.
2. Modify the IDE generated MulticastChatServer.java as shown in Code-2.11 below. Study the code by paying special attention to the bold fonted parts.
import java.net.*;
public class MulticastChatServer { public static void main(String args[]) // Default port number we are going to use // Create a MulticastSocket // Determine the IP address of a host, given the host name // getByName- returns IP address of given host // Continually receives data and prints them |
Code-2.11: MulticastChatServer.java
3. Build and run the project
- Right click MulticastChatServer project and select Run.
- Observe that the server is waiting for a datagram message from a client in the Output window. (Figure-2.12 below)
MulticastSocket is created at port 5000 joinGroup method is called… |
Figure-2.12: Waiting for a message from a client
Solution: This exercise up to this point is provided as a ready-to-open-and-run NetBeans project as part of hands-on lab zip file. You can find it as <LAB_UNZIPPED_DIRECTORY>/javanetworking/samples/MulticastChatServer. You can just open it and run it.
(2.2) Build and run the client side code
- Select File->New Project (Ctrl+Shift+N). The New Project dialog box appears.
- Under Choose Project pane, select Java under Categories and Java Application under Projects.
- Click Next.
- Under Name and Location pane, for the Project Name field, type in MulticastChatClient as project name.
- For Create Main Class field, type in MulticastChatClient.
- Click Finish.
- Observe that MulticastChatClient project appears and IDE generated MulticastChatClient.java is displayed in the source editor window of NetBeans IDE.
2. Modify the IDE generated MulticastChatClient.java as shown in Code-2.21 below. Study the code by paying special attention to the bold fonted parts.
import java.net.*; import java.io.*; public class MulticastChatClient { public static void main(String args[]) // Default port number we are going to use // Create a MulticastSocket // Determine the IP address of a host, given the host name // Joins a multicast group // Prompt a user to enter a message // Send the message to Multicast address // Close the socket |
Code-2.21: MulticastChatClient.java
3. Build and run the project
- Right click MulticastChatClient project and select Run.
- Observe the client is prompting you to enter a message. (Figure-2.23 below)
Type a message for the server: |
Figure-2.23: Waiting for the user to enter a message
- Enter a message, like Hello Server in this example, and press Enter key. (Figure-2.24 below)
Figure-2.24: Enter a message
4. Observe the server side.
- Click MulticastChatServer (run) tab to see the Output window of the server side.
- Observe that the server received a multicast message, Hello, World! in this example. (Figure-2.25 below)
MulticastSocket is created at port 5000 joinGroup method is called… Message received from client = Hello Server |
Figure-2.25: Message is received from a client
Solution: This exercise up to this point is provided as a ready-to-open-and-run NetBeans project as part of hands-on lab zip file. You can find it as <LAB_UNZIPPED_DIRECTORY>/javanetworking/samples/MulticastChatClient. You can just open it and run it.
Summary
Homework exercise (for people who are taking Sang Shin’s “Java Programming online course”)