毕业设计论文-外文文献翻译.doc

上传人:小飞机 文档编号:3879740 上传时间:2023-03-26 格式:DOC 页数:18 大小:755KB
返回 下载 相关 举报
毕业设计论文-外文文献翻译.doc_第1页
第1页 / 共18页
毕业设计论文-外文文献翻译.doc_第2页
第2页 / 共18页
毕业设计论文-外文文献翻译.doc_第3页
第3页 / 共18页
毕业设计论文-外文文献翻译.doc_第4页
第4页 / 共18页
毕业设计论文-外文文献翻译.doc_第5页
第5页 / 共18页
点击查看更多>>
资源描述

《毕业设计论文-外文文献翻译.doc》由会员分享,可在线阅读,更多相关《毕业设计论文-外文文献翻译.doc(18页珍藏版)》请在三一办公上搜索。

1、毕业设计(论文)外文参考文献翻译 计算机科学与信息工程系 系(院) 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

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

3、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 mys

4、tery 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

5、 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 ma

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

7、eading 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 programming in the Java language. In this tutorial, youll build a simple, centralized, conn

8、ection-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.2. IntroductionWell also examine some of the limitations of this framework and explore ways of getting around them.Wha

9、t 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

10、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 will help you a great deal in creating other connection-oriented servers in the future

11、.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 level possible in the Java language.There are certainly many systems available that can

12、 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, well describe the serv

13、er. 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 wont require any support software other than a J

14、ava 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 embed the server code within a larger framework. This framework might be used to su

15、pply 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 well see, this isnt very hard.3. First thi

16、ngs 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 ser

17、ver, 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 65

18、535.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

19、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 unfa

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

21、 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 construct

22、or: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. In practice, you might be embedding your basic server code in something larger, i

23、n 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 portstatic public void main( String args ) throws Exception / Get the port # from the command lineint port = Integer.parseInt( args0 );/ Create a Serv

24、er object, which will automatically begin/ accepting connections.new Server( port );Now that were all ready to listen, well continue to the next section to see how we accept new connections and what we do with them.4.The While-Accept loopWere ready to start accepting network connections from our cli

25、ents. Heres 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, initiate

26、s 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.Thats where the ServerSocket comes in. This is an object whose job is simple: listen on a port and when a new

27、 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, theres no way to contro

28、l the order, or the timing, with which the connections are arriving. As well see later, multithreading is an excellent way to deal with these connections once they have come in. However, were still trying to deal with the connections as they arrive.Heres what it looks like, in the abstract:/ start l

29、istening 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 n

30、ew connection that has come in. After youve 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 arent an

31、y connections coming in at the moment, then the accept () routine simply blocks - doesnt 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 serial

32、ization 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, were going to create a n

33、ew 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 wont pile up.The codeLets take a look at the code

34、 that does all this. The code below takes care of the things weve 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. Lets take a look:private void listen( int port ) throws IOException

35、 / Create the ServerSocketss = new ServerSocket( port );/ Tell the world were ready to goSystem.out.println( Listening on +ss );/ Keep accepting connections foreverwhile (true) / Grab the next incoming connectionSocket s = ss.accept();/ Tell the world weve got itSystem.out.println( Connection from +

36、s );/ Create a DataOutputStream for writing data to the/ other sideDataOutputStream dout = new DataOutputStream( s.getOutputStream() );/ 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(

37、 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 languages main strengths are networking and multithreading. That is no

38、t to say that other languages dont 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 th

39、is 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

40、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 youd want to use threads in your program, but there is one reason mos

41、t 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 ne

42、ver know who is going to say something first. If you have a single thread, and its waiting for user #0 to say something, then its not going to know that users #1 through #10 are talking like crazy.For this reason, were going to create a thread for each user connected to the system. The advantage of

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

44、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, bec

45、ause the whole purpose of this thread is to use a socket to communicate with the other side.Heres 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 weve g

46、otten to the point where were 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 hard

47、ly deserves the title of protocol, or it can be a sophisticated standard that has been ratified by consortia all over the world. Either way, its a protocol.Were going to create our own protocol, because in the Java language its very easy to do,and because theres little for us to gain from using an e

48、xisting 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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号