examples.example1_one_client
Class MainForServer
java.lang.Object
examples.example1_one_client.MainForServer
public class MainForServer
- extends Object
NetworkingExamples project: examples.example1_one_client.
A simple example of networking (Sockets) in which:
-- A single Server and Client exchange information, one after the other.
This is the Server's code.
Brevity is king in this example, so it has few comments / instructions after this
and is NOT organized in a OO way that permits easy reuse of code.
See the other examples for an OO approach:
-- examples.example2_one_client_OO
does this same example in a more OO way
-- examples.example3_one_client_OO_library
does this same example
using the simple networking library in package simpleNetworking
-- examples.example4_two_clients_OO_library
also uses the OO networking library simpleNetworking
-- examples.example5_chat_OO_library
also uses the OO networking library simpleNetworking
Run this example by:
1. On one computer, run MainForServer.
It starts up, nothing else happens.
2. On another computer (or the same computer, your choice), run MainForClient.
It asks in the console window/tab for the hostname of the Server.
Enter the name of the computer running MainForServer.
You can enter localhost if you are using a single computer for both processes.
You should see (in successive dialog boxes):
-- Server got 42 from the Client and will add 1 to it and thus send 43 to the Client
-- Client got 43 from the Server and will add 10 to it and thus send 53 to the Server
-- Server got 53 from the Client and will add 1 to it and thus send 54 to the Client
-- Client got 54 from the Server and will add 10 to it and thus send 64 to the Server
etc
Stop the programs by using the dialog boxes (which ask if you want to continue).
Stopping the programs in other ways may prevent them from releasing their resources
(and especially the PORT being used). Also, if the Client stops (for whatever reason)
without having connected to the Server, the Server is probably still running (without you
noticing it). In this situation, simply run the Client WITHOUT first starting the Server
to clear out the lock / running Server.
Before running this program, you may need to tell your Firewall not to
block the port that this program uses (4444, chosen arbitrarily).
In Windows, do so by:
Control Panel ~ Windows Firewall ~ Exceptions tab ~ Add Port
and enter the port number (4444) with any name you like.
Here is what the Server and Client are doing (to illustrate server/client communication):
-- The Server starts with the number 42 and repeatedly:
-- Adds 1 to its current number.
-- Sends that number to the Client.
-- Gets back a number from the Client
and makes that number the Server's current number.
-- The Client repeatedly:
-- Gets a number from the Server.
-- Adds 10 to that number.
-- Sends that number back to the Server after pausing a few seconds.
The Server's user can ask it to stop and the Client's user can ask it to stop.
In either case, the Server/Client sends a STOP message to the Client/Server to ask it to stop too.
Note that most of the code is for handling errors. There are only 7 Key Statements
(see details in the code) that are all-you-need-to-know to do networking in Java:
-- Key #1: Server constructs a ServerSocket.
-- Key #2: Server uses its ServerSocket to accept a connection to a Client.
Note that accept blocks (waits) until a Client asks for a Connection.
See Key #7 for how the Client does so.
-- Key #3: Constructs a BufferedReader that the Server can use to read
from the Client. The BufferedReader is constructed from the
Socket that the Server got from Key #2 statement.
-- Key #4: Constructs a PrintWriter that the Server can use to write
to the Client. The PrintWriter is constructed from the
Socket that the Server got from Key #2 statement.
-- Key #5: Server reads from the Client by using its BufferedReader.
-- Key #6: Server writes to the Client by using its PrintWriter.
For the last Key statement, turn to the Client:
-- Key #7: Client constructs a Socket for communicating with the Server.
The Client runs its Key #7 statement while the Server is waiting for
its Key #2 statement to complete; the Client's Key #7 statement causes
the Server's Key #2 statement to complete.
The Client continues from its Key #7 Statement to do as the Server did:
-- Construct a BufferedReader and PrintReader (as per Key #3 and Key #4).
-- Use the BufferedReader and PrintReader to read from and write to the Server
(as per Key #5 and Key #6).
Reading blocks (waits) for input to arrive; however, there are methods
available to see if any input has arrived yet.
- Author:
- David Mutchler, based on the Java Tutorials on networking. May, 2009.
Field Summary |
(package private) static int |
PORT
Port: Both Server and Client must use the same one, and it must be open. |
Method Summary |
static void |
main(String[] commandLineArguments)
Constructs a Server and starts it running. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
PORT
static int PORT
- Port: Both Server and Client must use the same one, and it must be open.
MainForServer
public MainForServer()
main
public static void main(String[] commandLineArguments)
- Constructs a Server and starts it running.
- Parameters:
commandLineArguments
- Ignored here.