【正文】
provides useful features such as faulttolerance, loadbalancing. What does the server do? Before we describe the Listener class, we39。ll describe the server. Doing so has a certain chronological elegance, because in our running system, the server will have to start before any of the clients can connect to it. Our server will be a standalone program a single Java process running on its own machine. It won39。t require any support software other than a Java virtual machine. And it won39。t require a Web server or application server, although a Web server or application server will likely be used to serve the client applet to the client. More advanced server systems often embed the server code within a larger framework. This framework might be used to supply features such as load balancing, special libraries for handling large numbers of clients, process migration, and database services。 However, our example is going to stand all by itself. It will take care of all working responsibilities on its own. As we39。ll see, this isn39。t very hard. 3. First things first Listening on a port The first thing we have to do is to get ready to receive ining connections. To do this, we must listen on a port. A port can be thought of as an address within a single puter. Remember that often a single machine might serve as a Web server, a chat server, an FTP server, and several other kinds of servers at the same time. Because of this, a connection to a server needs to specify not only the address of the machine itself, but also the particular 3 service within the machine. This internal address is a port and is represented by a single integer between 1 and 65535. Sockets Our munications between client and server will pass through a Java object called a Socket. Sockets are not at all Javaspecific。 the term is taken directly from the terminology of general IP (Inter Protocol) work programming. In Java programs, a Socket object is simply a wrapper around the lowlevel。 The most important thing to know about a Socket object is that it contains (among other things) two Streams. One is for reading data ing in, and the other is for writing data is to say, a Socket has an InputStream and an OutputStream.(If these Stream classes are unfamiliar to you, then suffice it to say that they are objects used for reading and writing data, often as a stream of bytes. If you don39。t know about them yet,you really should. See the package for more information.) So, now we get to the first of our seven elements, the Listener Class. We39。ll call it . The next few panels will show the essential elements of this class: the constructor and the main() routine. The constructor for Server takes a single parameter a port number. This tells us what port to listen on when we are ready to start accepting connections. Here is the constructor: public Server( int port ) throws IOException { // All we have to do is listen listen( port )。 } The main() routine We39。ll also include a main() routine so that this Server class can be used as its own standalone application. In practice, you might be embedding your basic server code in something larger, in which case you already have a main(). But for our purposes, the Server is all there is. Here39。s the main() routine: // Main routine // Usage: java Server port static public void main( String args[] ) throws Exception { // Get the port from the mand line int port = ( args[0] )。 // Create a Server object, which will automatically begin // accepting connections. 4 new Server( port )。 } Now that we39。re all ready to listen, we39。ll continue to the next section to see how we accept new connections and what we do with them. WhileAccept loop We39。re ready to start accepting work connections from our clients. Here39。s how the chat is going to work. We mentioned above that the Java language provides an object called a Socket, which represents a connection to a program somewhere else and through which data can pass. But how do we get this socket in the first place? A client, almost by definition, initiates the connection to a server. Therefore, the first job of a server is to wait for a connection to e in. That is, we need something to give us the Sockets that are connected to our clients. That39。s where the ServerSocket es in. This is an object whose job is simple: listen on a port and when a new connection es in, create a Socket that represents that new connection. Accepting Sockets Remember that your program will potentially be serving many clients from all over the Inter. And these clients will be connecting to your server without regard to each is, there39。s no way to control the order, or the timing, with which the connections are arriving. As we39。ll see later, multithreading is an excellent way to deal with these connections once they have e in. However, we39。re still