畢業(yè)設(shè)計論文外文文獻(xiàn)翻譯_第1頁
畢業(yè)設(shè)計論文外文文獻(xiàn)翻譯_第2頁
畢業(yè)設(shè)計論文外文文獻(xiàn)翻譯_第3頁
畢業(yè)設(shè)計論文外文文獻(xiàn)翻譯_第4頁
畢業(yè)設(shè)計論文外文文獻(xiàn)翻譯_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、 m工多次畢業(yè)設(shè)計(論文)外文參考文獻(xiàn)翻譯計算機科學(xué)與信息工程系系(院)2008 屆題 目企業(yè)即時通Instant Messaging for Enterprises課題類型技術(shù)開發(fā)課題來源自選學(xué)生姓名許帥 專業(yè)班級 04計算機科學(xué)與技術(shù)指導(dǎo)老師王占中 職 稱 工程師完成日期: 2008年4月 6日目錄 TOC o 1-5 h z HYPERLINK l bookmark6 o Current Document Instant Messaging for Enterprise1Tips1. HYPERLINK l bookmark9 o Current Document Introductio

2、n1. HYPERLINK l bookmark11 o Current Document First things first2. HYPERLINK l bookmark13 o Current Document The While-Accept loop4. HYPERLINK l bookmark15 o Current Document Per-Thread class6. HYPERLINK l bookmark17 o Current Document The Client class7. HYPERLINK l bookmark19 o Current Document 企業(yè)即

3、時通9.提示 9.簡介 9 HYPERLINK l bookmark21 o Current Document .首先第一件事 10 HYPERLINK l bookmark23 o Current Document .監(jiān)聽循環(huán) 11.單線程類 13 HYPERLINK l bookmark25 o Current Document .用戶端類 14Instant Messaging for EnterpriseTipsIf Java is, in fact, yet another computer programming language, you may question why it

4、is so important andwhy it is being promoted as a revolutionary step in computer programming. The answer isn t imme(if you re coming from aatritional programming perspective. Although Java is very useful for solving traditional standalone programming problems, it is also important because it will sol

5、ve programming problems on the World Wide Web. What is the Web?The Web can seem a bit of a mys tery at first, with all this talk of“ presenfteg, 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 computing

6、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

7、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 expe

8、rience doing networking programming. We 1l cover topics such as networking and multithreading in enough detail so that youll 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 prog

9、ramming in the Java language. In this tutorial, youll build a simple, centralized, connection-oriented Java server. In doing so, youll learn a basic framework that you can use when creating such a server, using time-honored techniques that work well in many situations.IntroductionWell also examine s

10、ome 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

11、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. Well be creating a stripped-down, connection-oriented server. Learning the basic framework wi

12、ll help you a great deal in creating other connection-oriented servers in the future.Why create from scratch?In creating this prototype server, well be using nothing more than the basic packages built into every Java implementation. This allows us to explore server programming at the very lowest lev

13、el 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.W

14、hat does the server do?Before we describe the Listener class, well 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 pro

15、cess running on its own machine. It wont require any support software other than a Java virtual machine. And it wont 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

16、 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 serviceso However, our example is going to stand all by itself. It will take care of all

17、networking responsibilities on its own. As well see, this isnt very hard.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

18、that often a single machine might serve as a Web server, a chat server, an FTPserver, 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 i

19、nternal 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 Prot

20、ocol) 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,

21、 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 dont know about them yet,you really should. See the java.io package for more information

22、.)So, now we get to the first of our seven elements, the Listener Class. Well 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

23、 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() routineWell also include a main() routine so that this Server class can be used as its own stand-alone application.

24、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. Heres the main() routine:/ Main routine/ Usage: java Server portportstatic public void main( String args口)throws Exception / Get

25、the port # from the command lineint port = Integer.parseInt( args0);/ Create a Server object, which will automatically begin/ accepting connections.new Server( port ); 現(xiàn)在我們開始監(jiān)聽,下一節(jié)我們將繼續(xù)介紹是如何接受連接的,看看我們是如何處理它 們的。我們已經(jīng)準(zhǔn)備從我們的客戶接受連接,這就是說料體內(nèi)是如何進(jìn)行的。.監(jiān)聽循環(huán)上面我們提到了 java中一個叫做套接字的對象,它代表著通過建立從別的地方的應(yīng)用程 序接收數(shù)據(jù)。一個客戶端從

26、定義,啟動到連接服務(wù)器,我們是怎么得到這個socket的呢?服務(wù)器端首 先要做的工作是等待連接的建立,也就是說我們需要發(fā)送一些信息到客戶端,代表著連接的 建立。這就是serversocket 是如何工作的,有一個 serversocket 對象,它一直監(jiān)聽著一個端 口,當(dāng)有一個新的連接到來的時候,它將創(chuàng)建一個socket對象代表著連接的建立。接受socket可能你服務(wù)器程序是為了服務(wù)來自互聯(lián)網(wǎng)上的很多客戶端,這些客戶端將彼此不相關(guān)的 與你的服務(wù)器建立連接,也就是說我們不能控制客戶端連接到來的時間和順序,下面我們將 介紹多線程一個比較優(yōu)越的方法處理這些連接不管他們什么時候到來。但是當(dāng)連接到來時我

27、們將試圖處理這個連接。Socket暗含了一個簡單直接的處理方法:它串行接受連接,正如你一個挨一個的問他們一樣,而它們專門在排著隊等待。下面就是模 型:/ start listening on the portServerSocket ss = new ServerSocket( port );/loop foreverwhile (true) / get a connectionSocket newSocket = ss.accept();/ deal with the connection/ . 當(dāng)serversocket的方法調(diào)用時,accept()實例將返回一個socket對象代表著新的

28、連接 的建立,這次連接處理完畢,將再次調(diào)用accpt ()處理下一個連接,就是用這種方法,不管連接到來的有多快,不管你的計算機有多少處理器和網(wǎng)絡(luò)接口,一個時刻只能建立一個連 接(如果一時間沒有連接請求,accpet ()實例將一直等待,知道有連接來請求。一般來說,系列化是一個有效處理事情同時發(fā)生的一個有效的方法,但是它的一個潛在 的缺點是消除了排比,也就是說,串行化阻止了我們在同一時間做很多的事情。上面的代碼,當(dāng)程序處理一個連接的時候,其他的連接是必須等待的。但是對我們來說系列化已經(jīng)不是一個問題,因為每次連接到來的時候,我們將建立一個 新的線程來處理它。一旦線程創(chuàng)建,它將去處理新的連接,我們的

29、循環(huán)接受accept ()將去等待接收新的連接。如果建立線程的速度夠快,連接將不會被阻塞。代碼:然我們看看代碼做的這些,下面的代碼涉及到我們談?wù)摰臇|西,監(jiān)聽一個端口,接收連 接,并且創(chuàng)建新的線程處理它們。下面它們將做一些有用的東西,讓我們看看:private void listen( int port ) throws IOException / Create the ServerSocketss = new ServerSocket( port );/ Tell the world were ready to goSystem.out.println( Listening on +ss );

30、/ Keep accepting connections foreverwhile (true) / Grab the next incoming connectionSocket s = ss.accept();/ Tell the world weve got itSystem.out.println( Connection from +s );/ Create a DataOutputStream for writing data to the/ other sideDataOutputStream dout = new DataOutputStream( s.getOutputStre

31、am();/ Save this stream so we dont need to make it againoutputStreams.put( s, dout );/ Create a new thread for this connection, and then forget/ about itnew ServerThread( this, s );代碼的最后一行創(chuàng)建了一個線程處理新的連接。對于serverthread ,這是該課題的下一節(jié)。.單線程類什么是thread?Java語言的兩個主要優(yōu)勢是網(wǎng)絡(luò)和多線程。這并不是說其他語言,不支持這些功能,其實其他語言也支持這些功能。但是ja

32、va用來提供這些功能非常優(yōu)雅,特別是作為一種商業(yè)語 言。一個線程是一般定義為一個單獨的控制線,它的真正意思是說一個多線程的程序包含有 多個活動在同時進(jìn)行。除了多線程是在一個程序里共享數(shù)據(jù)資源以外,它類似任務(wù)和多任務(wù)處理的概念。這使 它們共享數(shù)據(jù)直接高效,但也使它們更容易相互混淆。為什么要使用多線程?詳細(xì)討論多線程超出了補習(xí)的范圍,雖然你在程序中使用線程的原因不只一個,但最重 要的原因是是要建造一個輸入輸出的聊天服務(wù)器。你的聊天服務(wù)器和客戶端的用戶溝通,用戶通常情況下要比服務(wù)器慢的多,也就是說服 務(wù)器端要浪費很多的時間等待用戶發(fā)送消息。而我們又不知道誰先發(fā)送消息,如果使用單線 程,只有等到0號用

33、戶先發(fā)送消息然后才輪到1號到10的用戶發(fā)送。由于這個原因,我們?yōu)槊恳粋€用戶建立一個連接系統(tǒng)的線程。多線程的優(yōu)點是:當(dāng)一個 線程等待用戶緩慢發(fā)送消息時,他基本上是處于休眠狀態(tài),直到那個用戶發(fā)送消息。于此同 時,另外一個線程可以接受其他用戶發(fā)過來的消息。實際上,多線程讓我們彼此之間盡可能 的迅速。在java程序中,任何一個類可以實現(xiàn)一個線程通過繼承runable ()接口來實現(xiàn)??梢酝ㄟ^java.lang.Thread 來查詢。我們只有選擇后者。public class ServerThread extends Thread/ .Socket對象是必不可少的,因為線程的目的是用socket來讓通信

34、的雙方進(jìn)行通信的。下面是代碼:/ Constructor.public ServerThread( Server server, Socket socket ) / Save the parametersthis.server = server;this.socket = socket;/ Start up the thread start();.用戶端類既然我們到了要討論用戶的時候了,我們應(yīng)該說一下我們的通信協(xié)議。每一個客戶機/服務(wù)器系統(tǒng)有一個通信協(xié)議,這無疑是發(fā)送和接受消息的一種形式。協(xié)議是如此的簡單幾乎 不值得命名?;蛘咚呀?jīng)以一種復(fù)雜的標(biāo)準(zhǔn)發(fā)布到世界各地,無論是哪種方式,它就是一項 協(xié)議。我們將建立自己的協(xié)議,因為這對于java語言來說是很簡單的。我們不能從現(xiàn)有的標(biāo)準(zhǔn) 中得到什么,我們只有建立非常簡單的協(xié)議。Java語言有一對非常有用的類 datainputstream 和dataoutputstream 。這些類允許你 讀入和寫出各種類型的數(shù)據(jù)到流中,不用考慮它們將被寫到哪里,因為這些類使用的是相同 的格式,并且這種格式是不改變的。你可以確定一個整形的數(shù)據(jù)被寫到 DataOutputStream中 并且這個整形數(shù)據(jù)可以從另外一端的 DataInputStream讀出來。下面就是我們的協(xié)議:當(dāng)用戶把一些信息輸入到聊天窗口時,輸入的信息就像一用字符輸入到

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論