Java Reference
In-Depth Information
}
}
Executing this program along with the client that is built in Recipe 21-2 will result
in the following output from the SocketServer :
have opened port 1234 locally
client has made socket connection
received from client: Here is a test.
closing socket
Note To run the two recipes so that they work with each other, first start the Sock-
etServer program so that the client can create a socket using the port that is opened
in the server program. After the SocketServer starts, initiate the SocketClient
program to see the two work together.
Caution This SocketServer program opens a port on your machine ( 1234 ). Be
sure that you have a firewall set running on your machine; otherwise, you will be open-
ing port 1234 to everyone. This could result in your machine being attacked. Open
ports create vulnerabilities for attackers to break into machines, kind of like leaving a
door in your house open. Note that the example in this recipe has a minimal attack pro-
file because the server is run through only one pass and will print only a single message
from the client before the session is closed.
How It Works
Server applications can be used to enable work to be performed on a server via direct
communication from one or more client applications. Client applications normally
communicate to the server application, send messages or data to the server for process-
ing, and then disconnect. The server application typically listens for client applications,
and then performs some processing against a client request once a connection is re-
ceived and accepted. In order for a client application to connect to a server application,
the server application must be listening for connections and then processing them
somehow. You cannot simply run a client against any given host and port number com-
bination because doing so would likely result in a refused connection. The server-side
Search WWH ::




Custom Search