【正文】
......................... 1 2. Introduction ............................................................................................................................... 1 3. First things first ......................................................................................................................... 2 WhileAccept loop .............................................................................................................. 4 5. PerThread class ........................................................................................................................ 6 6. The Client class ......................................................................................................................... 7 企業(yè)即時(shí)通 ..................................................... 9 ..................................................................... 9 ..................................................................... 9 ............................................................ 10 ................................................................ 11 ................................................................ 13 ................................................................ 14 1 Instant Messaging for Enterprise 1. Tips If Java is, in fact, yet another puter programming language, you may question why it is so important and why it is being promoted as a revolutionary step in puter programming. The answer isn’t immediately obvious if you’re ing from a traditional programming perspective. Although Java is very useful for solving traditional standalone programming problems, it is also important because it will solve programming problems on the World Wide Web. What is the Web? The Web can seem a bit of a mystery at first, with all this talk of “surfing,” “ presence,” and “home pages.” It’s helpful to step back and see what it really is, but to do this you must understand client/server systems, another aspect of puting that is full of confusing issues. The primary idea of a client/server system is that you have a central repository of information, some kind of data, often in a database。 That you can distribute on demand to some set of people or machines. The basic concept of client/server puting, then, is not so plicated. The problems arise because you have a single server trying to serve many clients at once. Building a java chat server Should I take this tutorial? in this tutorial, we will build both the server and client sides of a simple chat system this tutorial is for someone with little or no experience doing working programming. We’ll cover topics such as working and multithreading in enough detail so that you39。ll build a simple, centralized, connectionoriented Java server. In doing so, you39。ll also examine some of the limitations of this framework and explore ways of getting around them. What is a connectionoriented server? Generally speaking, the job of any server is to provide a centralized service. However, there are many 2 different ways of providing services, and many different ways to structure the munications. Chat is roughly described as a connectionoriented service, because a user establishes a connection and maintains that connection, sending and receiving text for the duration of the session. We39。ll be using nothing more than the basic packages built into every Java implementation. This allows us to explore server programming at the very lowest level possible in the Java language. There are certainly many systems available that can take care of many of these working details for you. In many cases, the best realworld solution is to use an existing framework, because it often provides useful features such as faulttolerance, loadbalancing. What does the server do? Before we describe the Listener class, we39。t require any support software other than a Java virtual machine. And it won39。 However, our example is going to stand all by itself. It will take care of all working responsibilities on its own. As we39。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 most important thing to know about a Socket object is that it con