The goal of these programming exercises is to practise:
- communication with a remote server
- using the
java.net.*
package capabilities
For this assignment, we've provided the starter project above.
Modify App.java to connect to the main CBF website at https://codingblackfemales.com, then read its content line by line and print each line to the screen.
You will need to use HttpURLConnection
, BufferedReader
and InputStreamReader
classes.
Whenever possible, use the try-with-resources construct we saw earlier in the course, and ensure all resources are released.
To verify that your code works as expected, run the tests. In your terminal, ensure that you are in the root of this repository, then run the following command:
./mvnw clean test
Create two executable classes:
- a
SocketServer
class which uses aServerSocket
to listen for connections onlocalhost:4040
, then prints message it receives on the screen. - a
SocketClient
class which requests a connection to server, sends a simple text message to the server.
Ensure all resources created in your programmes are released appropriately. To test your code manually, you'll need to run each application in a separate terminal:
Terminal 1:
./mvnw compile exec:java -Dexec.mainClass="com.cbfacademy.SocketServer"
Terminal 2:
./mvnw compile exec:java -Dexec.mainClass="com.cbfacademy.SocketClient"
To verify that your code works as expected, run the tests:
./mvnw clean test
Due to the nature of the tests needing to bind the same port, it's advisable to run the server and client tests separately until both classes are complete, i.e.:
./mvnw clean test -Dtest=SocketServerTest
./mvnw clean test -Dtest=SocketClientTest