Client Class
The Client
(Figs. 28.9–28.10) works similarly to class Server
, except that the Client
sends packets only when the user types a message in a text field and presses the Enter key. When this occurs, the program calls method actionPerformed
(Fig. 28.9, lines 32–57), which converts the String
the user entered into a byte
array (line 41). Lines 44–45 create a DatagramPacket
and initialize it with the byte
array, the length of the String
that was entered by the user, the IP address to which the packet is to be sent (InetAddress.getLocalHost()
in this example) and the port number at which the Server
is waiting for packets (5000 in this example). Line 47 sends the packet. The client in this example must know that the server is receiving packets ...
Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.