|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectexamples.example2_one_client_OO.Client
public class Client
NetworkingExamples project: examples.example2_one_client_OO. A simple example of networking (Sockets) in which: -- A single Server and Client exchange information, one after the other. This is the Client code. This is the same example asexamples.example1_one_client
except structured in a more OO way. In particular, this structure has 3 classes: -- Main: for starting the program. -- Server: for initiating and running the Server. -- Client: for initiating and running the Client. Seeexamples.example1_one_client
for: -- Exactly what information the Server and Client exchange in this demo. -- The 7 Key Statements that are all-you-need-to-know to do networking in Java. Seeexamples.example3_one_client_OO_library
for this same example but using the simple networking library in packagesimpleNetworking
.
Constructor Summary | |
---|---|
Client(String hostName,
int port)
Constructs readers and writers for talking to the Server, as follows: 1. |
Method Summary | |
---|---|
(package private) void |
close()
Closes all resources that this Client uses: writer, reader, socket. |
void |
run()
A simple example of communication between this Client and the Server. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public Client(String hostName, int port) throws Throwable
Constructs readers and writers for talking to the Server, as follows: 1. Constructs a Socket to the Server, using the given hostname and port. Note: The Server must be accepting clients at this point, else the construction of the Socket by this Client will fail. 2. Use that Socket to: a. Get an InputStream. Then decorate the InputStream into a BufferedReader (for more efficient communication). b. Get an OutputStream. Then decorate the OutputStream into a PrintWriter (for convenient ways to write messages). Finally, this constructor: 3. Starts a Thread in which the Client repeatedly communicates with the Server (so that you can see a simple example of network communication).
hostName
- HostName or IP address of the Server to which to connect.port
- Port to use for the Socket that connects to the Server
at the given hostName.
Throwable
- if any Exception or Error occurs.Method Detail |
---|
public void run()
A simple example of communication between this Client and the Server. It behaves as follows: Initiate the protocol by sending a random number to the Server. Then: Repeatedly, until the Server sends STOP or the user says to stop: -- Get a number from the Server. -- Add a random number between 0 and 10 to it. -- After pausing 0 to 5 seconds, send the revised number to the Server. The Server can send whatever number it wishes at each iteration; in fact, the particular Server in this program takes the number that the Client sends it, asks the user for another number, and sends the sum of the two numbers back to this Client. If the user says to stop, the Client sends a STOP message to the Server so that the Server will stop too.
run
in interface Runnable
void close()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES All Classes | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |