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
A Client Socket Bean

by John Zukowski, MageLang Institute

[Help | API Docs | Short Course | Magercises]

For this Magercise you use the BeanBox, or another IDE, to design and test a nongraphical socket Bean. The Bean makes a socket connection to a server and then performs send and/or receive operations. You should design your Bean so that other users (programmers) can plug your socket Bean into their applications via an IDE.

Magercise 9 Prerequisites

Skeleton Code

Tasks

  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.

  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.

  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();

  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.

  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.

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

Where help exists, the task numbers above are linked to the step-by-step help page.

Solution Source

Demonstration

The test program TestConnect connects to a named host and port and interacts accordingly with the server at that port:

[prompt] java TestConnect www.magelang.com 7
read back: hello
[prompt] java TestConnect www.magelang.com 13
time: Tuesday, December 15, 1998 9:45:56

After loading the ClientSocket Bean, it should appear in the palette, from where it can be selected and instantiated on the worksheet:

In this case, the worksheet contains a nongraphical ClientSocket Bean and command buttons for connecting, sending, and, optionally, receiving data to and from a socket. Also, the worksheet contains instances of two different specializations of TextArea, both of which implement the SocketListener interface, one tailored to the received() and one to the sent() event notification methods.

The property sheet displays the published properties:

Note that the port is set to 7, the standard echo server location. Both the "Send" and "Read" buttons have been pressed to generate the output shown in the worksheet.

It's possible, of course, for the connection alone to generate output; it simply depends on the server functionality. For example, consider setting the port to 13, the standard date-time server location:

In this case, pressing the "Read" button is sufficient:

Your design and implementation will certainly differ, but the basic objective is to support connections to a server at a well-known location, as well as basic send and receive operations.

Magercise 10
Short Course
About This Short Course

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


[ This page was updated: 29-Sep-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.