Java網路程式設計4 (轉)

themoney發表於2007-10-02
Java網路程式設計4 (轉)[@more@] 

ServerSocket

  由於SSClient使用了流套接字,所以服務也要使用流套接字。這就要建立一個ServerSocket,ServerSocket有幾個構造,最簡單的是ServerSocket(int port),當使用ServerSocket(int port)建立一個ServerSocket物件,port引數傳遞埠號,這個埠就是連線請求的埠,如果在這時出現錯誤將丟擲IOException異常物件,否則將建立ServerSocket物件並開始準備接收連線請求。

  接下來服務程式進入無限迴圈之中,無限迴圈從ServerSocket的accept()方法開始,在呼叫開始後accept()方法將導致呼叫執行緒阻塞直到連線建立。在建立連線後accept()返回一個最近建立的Socket物件,該Socket物件繫結了客戶程式的或埠號。

  由於存在單個服務程式與多個客戶程式通訊的可能,所以服務程式響應客戶程式不應該花很多時間,否則客戶程式在得到服務前有可能花很多時間來等待通訊的建立,然而服務程式和客戶程式的會話有可能是很長的(這與電話類似),因此為加快對客戶程式連線請求的響應,典型的方法是伺服器主機執行一個後臺執行緒,這個後臺執行緒處理服務程式和客戶程式的通訊。

  為了示範我們在上面談到的慨念並完成SSClient程式,下面我們建立一個SSServer程式,程式將建立一個ServerSocket物件來監聽埠10000的連線請求,如果成功服務程式將等待連線輸入,開始一個執行緒處理連線,並響應來自客戶程式的命令。下面就是這段程式的程式碼:

  Listing 3: SSServer.:namespace prefix = o ns = "urn:schemas--com::office" />

// SSServer.java

import java.io.*;
import java.*;
import java.util.*;

class SSServer
{
 public static void main (String [] args) throws IOException
 { 
  System.out.println ("Server starting...n");

  // Create a server socket that listens for incoming connection
  // requests on port 10000.

  ServerSocket server = new ServerSocket (10000);

  while (true)
  {
   // Listen for incoming connection requests from client
   // programs, establish a connection, and return a Socket
   // that represents this connection.

   Socket s = server.accept ();

   System.out.println ("Accepting Connection...n");

   // Start a thread to handle the connection.

   new ServerThread (s).start ();
  }
 }
}

class ServerThread extends Thread
{
 private Socket s;

 ServerThread (Socket s)
 {
  this.s = s;
 }

 public void run ()
 {
  BufferedReader br = null;
  PrintWriter pw = null;

  try
  {
   // Create an input stream reader that chains to the socket's
   // byte-oriented input stream. The input stream reader
   // converts bytes read from the socket to characters. The
   // conversion is based on the platform's default character
   // set.

   InputStreamReader isr;
   isr = new InputStreamReader (s.getInputStream ());

   // Create a buffered reader that chains to the input stream
   // reader. The buffered reader supplies a convenient method
   // for reading entire lines of text.

   br = new BufferedReader (isr);

   // Create a print writer that chains to the socket's byte-
   // oriented output stream. The print writer creates an
   // intermediate output stream writer that converts
   // characters sent to the socket to bytes. The conversion
   // is based on the platform's default character set.

   pw = new PrintWriter (s.getOutputStream (), true);

   // Create a calendar that makes it possible to obtain date
   // and time information.

   Calendar c = Calendar.getInstance ();

 // Because the client program may send multiple commands, a
   // l is required. Keep loo until the client either
   // explicitly requests tenation by sending a command
   // beginning with letters BYE or implicitly requests
   // termination by closing its output stream.

   do
   {
    // Obtain the client program's next command.

    String cmd = br.readLine ();

    // Exit if client program has closed its output stream.

    if (cmd == null)
     break;
  
    // Convert command to uppercase, for ease of comparison.

    cmd = cmd.toUpperCase ();

    // If client program sends BYE command, terminate.

    if (cmd.startsWith ("BYE"))
     break;

    // If client program sends DATE or TIME command, return
    // current date/time to the client program.

    if (cmd.startsWith ("DATE") || cmd.startsWith ("TIME"))
     pw.println (c.getTime ().toString ());

    // If client program sends (Day Of Month) command,
    // return current day of month to the client program.

    if (cmd.startsWith ("DOM"))
     pw.println ("" + c.get (Calendar.DAY_OF_MONTH));

    // If client program sends DOW (Day Of Week) command,
    // return current weekday (as a string) to the client
    // program.

    if (cmd.startsWith ("DOW"))
     switch (c.get (Calendar.DAY_OF_WEEK))
 {
     case Calendar.SUNDAY : pw.println ("SUNDAY");
      break;

     case Calendar.MONDAY : pw.println ("MONDAY");
      break;

     case Calendar.TUESDAY : pw.println ("TUESDAY");
      break;

     case Calendar.WEDNESDAY: pw.println ("WEDNESDAY");
      break;

     case Calendar.THURSDAY : pw.println ("THURSDAY");
      break;

     case Calendar.FRIDAY : pw.println ("FRIDAY");
      break;

     case Calendar.SATURDAY : pw.println ("SATURDAY");
    }

    // If client program sends DOY (Day of Year) command,
    // return current day of year to the client program.

    if (cmd.startsWith ("DOY"))
     pw.println ("" + c.get (Calendar.DAY_OF_YEAR));

     // If client program sends PAUSE command, sleep for three
     // seconds.
 
    if (cmd.startsWith ("PAUSE"))
    try
    {
     Thread.sleep (3000);
    }
    catch (InterruptedException e)
    {
    }
   }
   while (true);
   {
   catch (IOException e)
   {
       System.out.println (e.toString ());
   }
   finally
   {
    System.out.println ("Closing Connection...n");
    try
    {
     if (br != null)
      br.close ();

      if (pw != null)
       pw.close ();

      if (s != null)
       s.close ();
    }
    catch (IOException e)
    {
    }
   }
  }
}


  執行這段程式將得到下面的輸出:

Server starting...
Accepting Connection...
Closing Connection...


  SSServer的宣告瞭一對類:SSServer 和ServerThread;SSServer的main()方法建立了一個ServerSocket物件來監聽埠10000上的連線請求,如果成功, SSServer進入一個無限迴圈中,交替呼叫ServerSocket的 accept() 方法來等待連線請求,同時啟動後臺執行緒處理連線(accept()返回的請求)。執行緒由ServerThread繼承的start()方法開始,並ServerThread的run()方法中的程式碼。

  一旦run()方法執行,執行緒將建立BufferedReader, PrintWriter和 Calendar物件並進入一個迴圈,這個迴圈由讀(透過BufferedReader的 readLine())來自客戶程式的一行文字開始,文字(命令)在cmd引用的string物件中,如果客戶程式過早的關閉輸出流,會發生什麼呢?答案是:cmd將得不到賦值。

  注意必須考慮到這種情況:在服務程式正在讀輸入流時,客戶程式關閉了輸出流,如果沒有對這種情況進行處理,那麼程式將產生異常。

  一旦編譯了SSServer的原始碼,透過輸入Java SSServer來執行程式,在開始執行SSServer後,就可以執行一個或多個SSClient程式。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10794571/viewspace-974551/,如需轉載,請註明出處,否則將追究法律責任。

相關文章