毕业设计论文-外文文献翻译.doc
毕业设计(论文)外文参考文献翻译 计算机科学与信息工程系 系(院) 2008 届题 目 企 业 即 时 通 Instant Messaging for Enterprises 课题类型 技术开发 课题来源 自 选 学生姓名 许帅 专业班级 04计算机科学与技术 指导老师 王占中 职 称 工程师 完成日期: 2008年 4 月 6 日目 录Instant Messaging for Enterprise11. Tips12. Introduction13. First things first24.The While-Accept loop45. Per-Thread class66. The Client class7企业即时通91.提示92.简介93.首先第一件事104.监听循环115.单线程类136.用户端类14Instant Messaging for Enterprise1. TipsIf Java is, in fact, yet another computer programming language, you may question why it is so important and why it is being promoted as a revolutionary step in computer programming. The answer isnt immediately obvious if youre coming 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.” Its helpful to step back and see what it really is, but to do this you must understand client/server systems, another aspect of computing 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 computing, then, is not so complicated. The problems arise because you have a single server trying to serve many clients at once.Building a java chat serverShould 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 networking programming. Well cover topics such as networking and multithreading in enough detail so that you'll be able to follow the examples, even if you have little or no experience doing this kind of programming. You will, however, need to be familiar with basic object-oriented programming in the Java language. In this tutorial, you'll build a simple, centralized, connection-oriented Java server. In doing so, you'll learn a basic framework that you can use when creating such a server, using time-honored techniques that work well in many situations.2. IntroductionWe'll also examine some of the limitations of this framework and explore ways of getting around them.What is a connection-oriented server?Generally speaking, the job of any server is to provide a centralized service. However, there are many different ways of providing services, and many different ways to structure the communications. Chat is roughly described as a connection-oriented service, because a user establishes a connection and maintains that connection, sending and receiving text for the duration of the session. We'll be creating a stripped-down, connection-oriented server. Learning the basic framework will help you a great deal in creating other connection-oriented servers in the future.Why create from scratch?In creating this prototype server, we'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 networking details for you. In many cases, the best real-world solution is to use an existing framework, because it often provides useful features such as fault-tolerance, load-balancing.What does the server do?Before we describe the Listener class, we'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 stand-alone program - a single Java process running on its own machine. It won't require any support software other than a Java virtual machine. And it won'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 networking responsibilities on its own. As we'll see, this isn't very hard.3. First things firstListening on a portThe first thing we have to do is to get ready to receive incoming connections. To do this, we must listen on a port.A port can be thought of as an address within a single computer. 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 service within the machine. This internal address is a port and is represented by a single integer between 1 and 65535.SocketsOur communications between client and server will pass through a Java object called a Socket. Sockets are not at all Java-specific; the term is taken directly from the terminology of general IP (Internet Protocol) network programming. In Java programs, a Socket object is simply a wrapper around the low-level。The most important thing to know about a Socket object is that it contains (among other things) two Streams. One is for reading data coming in, and the other is for writing data out.That 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 don't know about them yet,you really should. See the java.io package for more information.)So, now we get to the first of our seven elements, the Listener Class. We'll call it Server.java.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 listenlisten( port );The main() routineWe'll also include a main() routine so that this Server class can be used as its own stand-alone 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. Here's the main() routine:/ Main routine/ Usage: java Server >port<static public void main( String args ) throws Exception / Get the port # from the command lineint port = Integer.parseInt( args0 );/ Create a Server object, which will automatically begin/ accepting connections.new Server( port );Now that we're all ready to listen, we'll continue to the next section to see how we accept new connections and what we do with them.4.The While-Accept loopWe're ready to start accepting network connections from our clients. Here'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 come in. That is, we need something to give us the Sockets that are connected to our clients.That's where the ServerSocket comes in. This is an object whose job is simple: listen on a port and when a new connection comes in, create a Socket that represents that new connection.Accepting SocketsRemember that your program will potentially be serving many clients from all over the Internet. And these clients will be connecting to your server without regard to each other.That is, there's no way to control the order, or the timing, with which the connections are arriving. As we'll see later, multithreading is an excellent way to deal with these connections once they have come in. However, we're still trying to deal with the connections as they arrive.Here's what it looks like, in the abstract:/ start listening on the portServerSocket ss = new ServerSocket( port );/ loop foreverwhile (true) / get a connectionSocket newSocket = ss.accept();/ deal with the connection/ .The accept() routine is the key here. When this method of ServerSocket is called, it returns a new Socket object that represents a new connection that has come in. After you've dealt with this connection, you call accept() and get the next one. This way, no matter how fast connections are coming, and no matter how many processors or network interfaces your machine has, you get the connections one at a time. (And if there aren't any connections coming in at the moment, then the accept () routine simply blocks - doesn't return - until there are.)Serialization is a useful way, in general, to deal with things that are happening simultaneously. A potential drawback, however, is that it can remove parallelism? That is to say serialization can prevent us from saving time by working on multiple things at the same time. In the code above, while the program is dealing with one connection, other connection might be piling up.But serialization is not a problem for us because each time a connection comes in, we're going to create a new thread to deal with it. Once the new thread is created, it can go off and deal with the new connection, and our while-accept loop can go back to accepting new connections. If the act of creating this new thread is fast enough, then the connections won't pile up.The codeLet's take a look at the code that does all this. The code below takes care of the things we've been talking about: listening on a port, accepting new connections, and creating threads to deal with them. It also does a few other things that will be useful later. Let's take a look:private void listen( int port ) throws IOException / Create the ServerSocketss = new ServerSocket( port );/ Tell the world we're ready to goSystem.out.println( "Listening on "+ss );/ Keep accepting connections foreverwhile (true) / Grab the next incoming connectionSocket s = ss.accept();/ Tell the world we've got itSystem.out.println( "Connection from "+s );/ Create a DataOutputStream for writing data to the/ other sideDataOutputStream dout = new DataOutputStream( s.getOutputStream() );/ Save this stream so we don't need to make it againoutputStreams.put( s, dout );/ Create a new thread for this connection, and then forget/ about itnew ServerThread( this, s );The last line of this listing creates a thread to deal with the new connection. This thread is an object called a ServerThread, which is the topic of the next section.5. Per-Thread classWhat is a thread?Two of the Java language's main strengths are networking and multithreading. That is not to say that other languages don't support these functions - they do. But the abstractions that the Java language uses to provide these features are particularly elegant, especially for a commercial language.A thread is generally defined as a separate line of control within a single process. What this really means is that a multithreaded program has multiple, semi-autonomous activities going on inside of it at the same time.Multithreading is similar to the concepts of a task and multitasking, except that the multiple threads within a program all share the same data space. This makes it easier for them to share data directly and efficiently - and it also makes it easier for them to mess each other up.Why use multithreading?A detailed discussion of threads is beyond the scope of this tutorial. There are a few reasons why you'd want to use threads in your program, but there is one reason most pertinent to the construction of a chat server: input/output.Your chat server is communicating (in a sense) with the users at the client. Users are usually much slower than servers, which means that your server code is going to spend a lot of time simply waiting for users to say things. And you never know who is going to say something first. If you have a single thread, and it's waiting for user #0 to say something, then it's not going to know that users #1 through #10 are talking like crazy.For this reason, we're going to create a thread for each user connected to the system. The advantage of multithreading is that when one thread is listening for a slow user to say something, it essentially goes to sleep until something comes in from that user. In the meantime, another thread can be receiving data from another user. In effect, multithreading allows us to respond as quickly as we can to each user.In Java programs, any class can be made into a thread by implementing the Runnable interface. Or you can simply subclass from the class java.lang.Thread. We have chosen the latter route, for no particular reason:public class ServerThread extends Thread/ .The Socket object is essential, because the whole purpose of this thread is to use a socket to communicate with the other side.Here's the code::/ Constructor.public ServerThread( Server server, Socket socket ) / Save the parametersthis.server = server;this.socket = socket;/ Start up the threadstart();6. The Client classNow that we've gotten to the point where we're going to be talking to the client, we should talk a little bit about our communication protocol.Every client/server system has a communications protocol, which is nothing more than the format you use to send the data back and forth. The protocol can be so simple it hardly deserves the title of protocol, or it can be a sophisticated standard that has been ratified by consortia all over the world. Either way, it's a protocol.We're going to create our own protocol, because in the Java language it's very easy to do,and because there's little for us to gain from using an existing standard. Our protocol will be very simple.The Java language has a pair of extremely useful classes called DataInputStream and DataOutputStream. These classes allow you to read and write low-level data objects (like integers and strings) to a stream, without having to consider the format in which they are written. Because these classes use the same format, an