Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
 
Training Index

Introduction to the JavaBeans API, Magercise 9 Help
A Client Socket Bean

by John Zukowski, MageLang Institute

[Magercise 9 | API Docs | Short Course | Magercises]

Help is available for each task.

Task 1

The first task is to design and implement the ClientSocket Bean with a no-argument constructor and two properties: port and machine (or host). You may also want to add a messageToWrite property for sending data to a server socket, but this depends on your overall design.


At many public sites, for example, www.magelang.com, there is an echo service at port 7 and a date-time service at port 13. If there is no Internet or local network connection available, see EchoServer.java.

Task 2

Next, implement a connect() method that (1) creates a client socket, based on the port and machine properties, and (2) opens the client socket's input and output streams for basic reading and writing.


You can filter the client socket's input and output streams through either DataInputStream and PrintStream instances, respectively, or BufferedReader and BufferedWriter, respectively.

Examples follow:

  out = new PrintStream(
    client.getOutputStream());
  in = new DataInputStream(
    client.getInputStream());

and

  out = new BufferedWriter(
    new OutputStreamWriter(
    client.getOutputStream()));
  in = new BufferedReader(
    new InputStreamReader(
    client.getInputStream()));

Task 3

Next, implement public methods for basic send and receive services, for example,

  • public void writeln(String s);
  • public String readln() throws IOException;
  • public void writeToSocket();
  • public void readFromSocket();

The first two methods are appropriate for standard, compile-time method invocations from a source file. The latter two methods are convenient for establishing hooks to the Bean's functionality within an IDE--they require no arguments and return nothing. You can use a messageToWrite property to provide data for writeToSocket(). For both writeToSocket() and readFromSocket, you can make the "results" available via target notifications.

Task 4

Next, implement an event notification scheme for objects that want to register an interest in send and/or receive operations that take place over the socket.


You must add a vector to manage the targets, plus the appropriate addXXXListener() and removeXXXListener() methods for targets. Depending on your design, implement interfaces such as ClientSocketListener or SocketListener and ClientSocketEvent or SocketEvent. Keep in mind that if the names deviate from the default patterns, you will have to provide a bean-info class and EventSetDescriptor instances.

Task 5

Next, add target classes that implement the listener interfaces, so that you can instantiate the targets from an IDE's worksheet and monitor the sent and received data--snooper objects.


For example, specialize TextArea and implement the listener interface(s) according to the requirements of your event-handling design.

Task 6

Design and implement a command-line program TestConnect.java that verifies the correct operation outside an IDE.


See the sample TestConnect.java and modify it according to your design.

Return to Magercise 9

Copyright © 1998-1999 MageLang Institute. All Rights Reserved.


[ This page was updated: 27-Oct-99 ]

Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-99 Sun Microsystems, Inc.
All Rights Reserved. Legal Terms. Privacy Policy.