Java Reference
In-Depth Information
7. Write a class named WeatherWatcher that contains main().
8. Within main(), instantiate a DatagramSocket on port 4444. Also,
instantiate a DatagramPacket suitable for receipt, by using an array
of bytes of size 128.
9. Invoke the receive() method within main() so that the thread blocks
until a datagram packet is delivered.
10. After a packet is delivered, print out its contents. Create a loop so
that the WeatherWatcher then invokes receive() again, to be ready
for the next packet to be delivered.
11. Save, compile, and run the WeatherWatcher program.
12. Run the WeatherUpdater program, passing in the appropriate
host name (localhost if both are on the same computer) and port
number 4444.
The WeatherUpdater probably will not display any output. However,
in the WeatherWatcher program, you should see the statement “Current
temp: N ” every 15 seconds or so, with the temperature changing randomly.
Lab 17.3: The InstantMessage Server
This lab is a continuation of the Instant Messaging project from previous
chapters. In this lab, you will write the server application that receives an
InstantMessage object from a sender and sends the message to its
intended recipient.
1. Write a class named IMServer that implements Runnable. Add a
field of type Vector to contain all Participant objects currently
logged on.
2. Within the run() method, create a ServerSocket on port 5555. Invoke
the accept() method and wait for a client to connect.
3. When a client connects, they will send you a String using the
writeUTF() method of DataOutputStream. Read in this String using
the readUTF() method of DataInputStream, which will be their user-
name. Then, instantiate a new Participant, passing in the socket and
their username into the constructor.
4. Because Participant is a Thread, start it. Perform these steps in a
loop so that accept() is invoked on the ServerSocket after the new
Participant thread is started.
Search WWH ::




Custom Search