【正文】
nnection, and our whileaccept loop can go back to accepting new connections. If the act of creating this new thread is fast enough, then the connections won39。ll see later, multithreading is an excellent way to deal with these connections once they have e in. However, we39。 // Create a Server object, which will automatically begin // accepting connections. 4 new Server( port )。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。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 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 objectoriented programming in the Java language. In this tutorial, you39。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。 } The main() routine We39。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 e in. That is, we need something to give us the Sockets that are connected to our clients. That39。ve dealt with this connection, you call accept() and get the next one. This way, no matter how fast connections are ing, and no matter how many processors or work interfaces your machine has, you get the connections one at a time. (And if there aren39。 // Tell the world we39。 // Create a new thread for this connection, and then forget // about it new ServerThread( this, s )。s the code:: // Constructor. public ServerThread( Server server, Socket socket ) { // Save the parameters = server。s very easy to do,and because there39。 // We want to receive messages when someone types a line // and hits return, using an anonymous class as // a callback ( new ActionListener() { public void actionPerformed( ActionEvent e ) { processMessage( () )。 客戶機 /服務(wù)器 的基本概念并不是那么復(fù)雜,這個概念的提出 你有一個單一的服務(wù)器試圖 在同一時刻 服務(wù)于許多客戶。但是,也有許多不同的方式提供服務(wù),并有很多不同的方式來構(gòu)建通信服務(wù)。更先進的服務(wù)器系統(tǒng)往往嵌入服務(wù)器代碼在一個較大的范圍內(nèi)。 現(xiàn)在然我們開始介紹七個類中的一個,監(jiān)聽類 下面的幾個版面我們來介紹這個類的基本的元素:構(gòu)造器和 main()實例。 我們已經(jīng)準備從我們的客戶接受連接,這就是說料體內(nèi)是如何進行的。 12 // loop forever while (true) { // get a connection Socket newSocket = ()。下面它們將做一些有用的東西,讓我們看看: private void listen( int port ) throws IOException { // Create the ServerSocket ss = new ServerSocket( port )。t need to make it again ( s, dout )。 除了多線程是在一個程序里共享數(shù)據(jù)資源以外,它類似任務(wù)和多任務(wù)處理的概念。實際上,多線程讓我們彼此之間盡可能的迅速。 } 既然我們到了要討論用戶的時候了, 我們應(yīng)該說一下我們的通信協(xié)議。你可以確定一個整形的數(shù)據(jù)被寫到 DataOutputStream 中并且這個整形數(shù)據(jù)可以從另外一端的 DataInputStream 讀出來。 // We want to receive messages when someone types a line // and hits return, using an anonymous class as // a callback ( new ActionListener() { public void actionPerformed( ActionEvent e ) { processMessage( () )。 } catch( IOException ie ) { ( ie )。 // ... The Constructor: Connect to the server: // Connect to the server try { // Initiate the connection socket = new Socket( host, port )。 *用戶用 DataInputStream 接收信息 客戶端的類: 不管怎么樣,我們已經(jīng)建立了我們聊天系統(tǒng)的服務(wù)器端,我們 有一個服務(wù)器對象監(jiān)聽到來的連接,和一堆處理自身的連接。協(xié)議是如此的簡單幾乎不值得命名??梢酝ㄟ^ 來查詢。 為什么要使用多線程? 詳細討論多線程超出了補習(xí)的范圍,雖然你在程序中使用線程的原因不只一個,但最重要的原因是是要建造一個輸入輸出的聊天服務(wù)器。 }} 代碼的