【正文】
ine of control within a single process. What this really means is that a multithreaded program has multiple, semiautonomous 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 you39。ve finished building the server side of our chat system. We have one object (Server) listening for new connections and a bunch of perconnection objects (ServerThread) dealing with the connections themselves. Our client is going be an applet, because we39。 不過(guò)你需要熟悉基本的面向?qū)ο缶幊淘?Java 語(yǔ)言 。正如我們將會(huì)看到的,這是不是很辛 苦。 這就是 serversocket 是如何工作的,有一個(gè) serversocket 對(duì)象,它一直監(jiān)聽(tīng)著一個(gè)端口,當(dāng) 有一個(gè)新的連接到來(lái)的時(shí)候,它將創(chuàng)建一個(gè) socket 對(duì)象代表著連接的建立。 // Keep accepting connections forever while (true) { // Grab the next ining connection Socket s = ()。 你的聊天服務(wù)器和客戶(hù)端的用戶(hù)溝通,用戶(hù)通常情況下要比服務(wù)器慢的多,也就是說(shuō)服務(wù)器端要浪費(fèi)很多的時(shí)間等待用戶(hù)發(fā)送消息。 或者它 已經(jīng)以一種復(fù)雜的標(biāo)準(zhǔn)發(fā)布到世界各地,無(wú)論是哪種方式,它就是一項(xiàng)協(xié)議。 // We got a connection! Tell the world ( connected to +socket )。 add( Center, ta )。 // Start up the thread start()。 一個(gè)線程是一般定義為一個(gè)單獨(dú)的控制線 ,它的真正意思是說(shuō)一個(gè)多線程的程序包含有多個(gè)活動(dòng)在同時(shí)進(jìn)行。 代碼: 然我們看看代碼做的這些,下面的代碼涉及到我們談?wù)摰臇|西,監(jiān)聽(tīng)一個(gè)端口,接收連接,并且創(chuàng)建新的線程處理它們。 } 現(xiàn)在我們開(kāi)始監(jiān)聽(tīng), 下一節(jié)我們將繼續(xù)介紹是如何接受連接的,看看我們是如何處理它們的。我們的服務(wù)器是一個(gè)單獨(dú)的程序 一個(gè)單獨(dú)的java 運(yùn)行在獨(dú)立的機(jī)器上,除了 java 虛擬機(jī)它不再需要其它的任何軟件,雖然 Web 服務(wù)器或應(yīng)用服務(wù)器將可能被用來(lái)服務(wù)于客戶(hù)端程序到客戶(hù)端,但它不再需求 web 服務(wù)器或應(yīng)用程序服務(wù)器??蛻?hù)機(jī)/服務(wù)器系統(tǒng)的主要構(gòu)思 是 一個(gè)中央儲(chǔ)存庫(kù)的資料 ,往往是放在數(shù)據(jù)庫(kù)中的一些資料,然后可以滿足一些人或者是機(jī)器的需求。re going to create our own protocol, because in the Java language it39。t need to make it again ( s, dout )。 // deal with the connection // ... } 5 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 e in. After you39。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 )。 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。 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。re still trying to deal with the connections as they arrive. Here39。ve got it ( Connection from +s )。ve gotten to the point where we39。雖然 java 解決傳統(tǒng)的單機(jī)編程問(wèn)題非常有用的,但它解決網(wǎng)絡(luò)編程問(wèn)題也是非常有用的。當(dāng)然有很多提供網(wǎng)絡(luò)細(xì)節(jié)的系統(tǒng),在許多情況下,最好的真實(shí)世界的解決辦法是使用現(xiàn)有的架構(gòu),因?yàn)樗3L峁┝?有用的功能,如容錯(cuò),負(fù)載平衡。} Main() 方法例程: 11 Server 類(lèi)要作為一個(gè)單獨(dú)的應(yīng)用程序,它必須包含一個(gè) main()方法,在你有了 main() 方法后,你可以添加你自己的基本應(yīng)用服務(wù)器代碼,這就是我們的服務(wù)器我們要完成的目的。 但是對(duì)我們來(lái)說(shuō)系列化已經(jīng)不是一個(gè)問(wèn)題,因?yàn)槊看芜B接到來(lái)的時(shí)候,我們將建立一個(gè)新的線程來(lái)處理它。 什么是 thread? Java 語(yǔ)言的兩個(gè)主要優(yōu)勢(shì)是網(wǎng)絡(luò)和多線程。 public class ServerThread extends Thread { // ... } Socket 對(duì)象是必不可少的,因?yàn)榫€程的目的是用 socket 來(lái)讓通信的雙方進(jìn)行通信的。 這并不是必須的 客戶(hù)端是一個(gè)獨(dú)立的應(yīng)用程序,向服務(wù)器端一樣,我們將不會(huì)再進(jìn)行更多的和前面一樣的討論。 dout = new DataOutputStream( () )。 Java 語(yǔ)言有一對(duì)非常有用的類(lèi) datainputstream 和 dataoutputstream 。多線程的優(yōu)點(diǎn)是:當(dāng)一個(gè)線程等待用戶(hù)緩慢發(fā)送消息時(shí),他基本上是處于休眠狀態(tài),直到那個(gè)用戶(hù)發(fā)送消息。 // Create a DataOutputStream for writing data to the // other side DataOutputStream dout = new DataOutputStream( () )。 Socket 暗含了一個(gè)簡(jiǎn)單直接的處理方法:它串行接受連接,正如你一個(gè)挨一個(gè)的問(wèn)他們一樣,而它們專(zhuān)門(mén)在排著隊(duì)等待。 套接字 : 客戶(hù)機(jī)和服務(wù)器之間的通信是通過(guò)建立一個(gè)叫做 socket 的對(duì)象來(lái)完成的, socket 是直接來(lái)自網(wǎng)絡(luò)編程,在 java 編程中,一個(gè) socket 對(duì)象被程序員使用多年,因?yàn)樗容^清晰的比較方便,也許這就是 java 比 c 語(yǔ)言方便的原因吧。 我們還將研究一些關(guān)于此框架的局限性并探索規(guī)避它們。 add( North, tf )。s not going to know that users 1 through 10 are talking like crazy. For this reason, we39。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. Let39。ll continue to the next section to see how we accept new connections and what we do with them. WhileAccept loop 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