Writing a Talk Server / Talk Client using Java Sockets
Write a talk server and client using the Socket and ServerSocket
classes provided in the java.net package. A ServerSocket listens on a
certain port until it receives an incoming connection, when the
ServerSocket manufactures a socket so the two ends of the connection
can communicate. When two sockets have connected, they exchange data
through InputStreams and OutputStreams which can be retrieved through
the getInputStream() and getOutputStream() methods of the Socket class
respectively. Since the InputStream and OutputStream classes can read
and write only integers and byte arrays, the DataInputStream and
DataOutputStream adapt the two to a more friendly interface.
Added Sophistication:
- Make the server persistent, so it remains after the first connection is completed.
- Use the awt to give the talk client a graphical user interface. For example,
a frame with a TextArea for the recieved data and a TextArea for the outgoing
data. The setEditable() method on TextArea can make it read only. In the demo
version of this assignment, The server listens on a port. When the client
connects on that port they reveal the talk Frame on their respective screens.
- Show your mastery of the TextArea class by implementing wrap around and backspace
functionality. Make it so one side can clear their TextArea and it will be reflected
on the other side of the connection.
- Extract the common abilities of the sending and receiving TextAreas and make a
reusable superclass for use in the other assignments implementing a talk server and talk
client.
Script to run the socket demo
References
Flanagan, David; Java in a Nutshell, Section 7 on Networking
The Java tutorial is a
useful online resource, especially the chapters
networking overview and all about sockets.
Look at the Javasoft Java API Documentation on the
java.net package and the java.io package.