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