轉 Java通訊端編程(上)

來源:互聯網
上載者:User
用Java開發網路軟體非常方便和強大,Java的這種力量來源於他專屬的一套強大的用於網路的 API,這些API是一系列的類和介面,均位於包java.net和javax.net中。在這篇文章中我們將介紹通訊端(Socket)慨念,同時以執行個體說明如何使用Network API操縱通訊端,在完成本文後,你就可以編寫網路低端通訊軟體。

  什麼是通訊端(Socket)?

  Network API是典型的用於基於TCP/IP網路Java程式與其他程式通訊,Network API依靠Socket進行通訊。Socket可以看成在兩個程式進行通訊串連中的一個端點,一個程式將一段資訊寫入Socket中,該Socket將這段資訊發送給另外一個Socket中,使這段資訊能傳送到其他程式中。1

  我們來分析一1,Host A上的程式A將一段資訊寫入Socket中,Socket的內容被Host A的網路管理軟體訪問,並將這段資訊通過Host A的網路介面卡發送到Host B,Host B的網路介面卡接收到這段資訊後,傳送給Host B的網路管理軟體,網路管理軟體將這段資訊儲存在Host B的Socket中,然後程式B才能在Socket中閱讀這段資訊。

  假設在圖1的網路中添加第三個主機Host C,那麼Host A怎麼知道資訊被正確傳送到Host B而不是被傳送到Host C中了呢?基於TCP/IP網路中的每一個主機均被賦予了一個唯一的IP地址,IP地址是一個32位的不帶正負號的整數,由於沒有轉變成二進位,因此通常以小數點分隔,如:198.163.227.6,正如所見IP地址均由四個部分組成,每個部分的範圍都是0-255,以表示8位地址。

  值得注意的是IP地址都是32位地址,這是IP協議版本4(簡稱Ipv4)規定的,目前由於IPv4地址已近耗盡,所以IPv6地址正逐漸代替Ipv4地址,Ipv6地址則是128位不帶正負號的整數。

  假設第二個程式被加入圖1的網路的Host B中,那麼由Host A傳來的資訊如何能被正確的傳給程式B而不是傳給新加入的程式呢?這是因為每一個基於TCP/IP網路通訊的程式都被賦予了唯一的連接埠和連接埠號碼,連接埠是一個資訊緩衝區,用於保留Socket中的輸入/輸出資訊,連接埠號碼是一個16位不帶正負號的整數,範圍是0-65535,以區別主機上的每一個程式(連接埠號碼就像房屋中的房間號),低於256的短口號保留給標準應用程式,比如pop3的連接埠號碼就是110,每一個通訊端都組合進了IP地址、連接埠、連接埠號碼,這樣形成的整體就可以區別每一個通訊端t,下面我們就來談談兩種通訊端:流通訊端和自定址資料通訊端。

  流通訊端(Stream Socket)

  無論何時,在兩個網路應用程式之間發送和接收資訊時都需要建立一個可靠的串連,流通訊端依靠TCP協議來保證資訊正確到達目的地,實際上,IP包有可能在網路中丟失或者在傳送過程中發生錯誤,任何一種情況發生,作為接受方的 TCP將聯絡發送方TCP重新發送這個IP包。這就是所謂的在兩個流通訊端之間建立可靠的串連。

  流通訊端在C/S程式中扮演一個必需的角色,客戶機程式(需要訪問某些服務的網路應用程式)建立一個扮演伺服器程式的主機的IP地址和伺服器程式(為用戶端應用程式提供服務的網路應用程式)的連接埠號碼的流通訊端對象。

  用戶端流通訊端的初始化代碼將IP地址和連接埠號碼傳遞給用戶端主機的網路管理軟體,管理軟體將IP地址和連接埠號碼通過NIC傳遞給伺服器端主機;伺服器端主機讀到經過NIC傳遞來的資料,然後查看伺服器程式是否處於監聽狀態,這種監聽依然是通過通訊端和連接埠來進行的;如果伺服器程式處於監聽狀態,那麼伺服器端網路管理軟體就向客戶機網路管理軟體發出一個積極的響應訊號,接收到響應訊號後,用戶端流通訊端初始化代碼就給客戶程式建立一個連接埠號碼,並將這個連接埠號碼傳遞給伺服器程式的通訊端(伺服器程式將使用這個連接埠號碼識別傳來的資訊是否是屬於客戶程式)同時完成流通訊端的初始化。

  如果伺服器程式沒有處於監聽狀態,那麼伺服器端網路管理軟體將給用戶端傳遞一個消極訊號,收到這個消極訊號後,客戶程式的流通訊端初始化代碼將拋出一個異常對象並且不建立通訊串連,也不建立流通訊端對象。這種情形就像打電話一樣,當有人的時候通訊建立,否則電話將被掛起。

  這部分的工作包括了相關聯的三個類:InetAddress, Socket, 和 ServerSocket。 InetAddress對象描繪了32位或128位IP地址,Socket對象代表了客戶程式流通訊端,ServerSocket代表了服務程式流通訊端,所有這三個類均位於包java.net中。

  InetAddress類

  InetAddress類在網路API通訊端編程中扮演了一個重要角色。參數傳遞給流通訊端類和自定址通訊端類構造器或非構造器方法。InetAddress描述了32位或64位IP地址,要完成這個功能,InetAddress類主要依靠兩個支援類Inet4Address 和 Inet6Address,這三個類是繼承關係,InetAddrress是父類,Inet4Address 和 Inet6Address是子類。

  由於InetAddress類只有一個建構函式,而且不能傳遞參數,所以不能直接建立InetAddress對象,比如下面的做法就是錯誤的:

InetAddress ia = new InetAddress ();

  但我們可以通過下面的5個Factory 方法建立來建立一個InetAddress對象或InetAddress數組:

   . getAllByName(String host)方法返回一個InetAddress對象的引用,每個對象包含一個表示相應主機名稱的單獨的IP地址,這個IP地址是通過host參數傳遞的,對於指定的主機如果沒有IP地址存在那麼這個方法將拋出一個UnknownHostException 異常對象。

   . getByAddress(byte [] addr)方法返回一個InetAddress對象的引用,這個對象包含了一個Ipv4地址或Ipv6地址,Ipv4地址是一個4位元組數組,Ipv6地址是一個16位元組地址數組,如果返回的數組既不是4位元組的也不是16位元組的,那麼方法將會拋出一個UnknownHostException異常對象。

   . getByAddress(String host, byte [] addr)方法返回一個InetAddress對象的引用,這個InetAddress對象包含了一個由host和4位元組的addr數組指定的IP地址,或者是host和16位元組的addr數組指定的IP地址,如果這個數組既不是4位元組的也不是16位位元組的,那麼該方法將拋出一個UnknownHostException異常對象。

   . getByName(String host)方法返回一個InetAddress對象,該對象包含了一個與host參數指定的主機相對應的IP地址,對於指定的主機如果沒有IP地址存在,那麼方法將拋出一個UnknownHostException異常對象。

   . getLocalHost()方法返回一個InetAddress對象,這個對象包含了本地機的IP地址,考慮到本地主機既是客戶程式主機又是伺服器程式主機,為避免混亂,我們將客戶程式主機稱為客戶主機,將伺服器程式主機稱為伺服器主機。

  上面講到的方法均提到返回一個或多個InetAddress對象的引用,實際上每一個方法都要返回一個或多個Inet4Address/Inet6Address對象的引用,調用者不需要知道引用的子類型,相反調用者可以使用返回的引用調用InetAddress對象的非靜態方法,包括子類型的多態以確保重載方法被調用。

  InetAddress和它的子類型對象處理主機名稱到主機IPv4或IPv6地址的轉換,要完成這個轉換需要使用網域名稱系統,下面的代碼示範了如何通過調用getByName(String host)方法獲得InetAddress子類對象的方法,這個對象包含了與host參數相對應的IP地址:

InetAddress ia = InetAddress.getByName ("www.javajeff.com"));

  一但獲得了InetAddress子類對象的引用就可以調用InetAddress的各種方法來獲得InetAddress子類對象中的IP地址資訊,比如,可以通過調用getCanonicalHostName()從網域名稱服務 (DNS)中獲得標準的主機名稱;getHostAddress()獲得IP地址,getHostName()獲得主機名稱,isLoopbackAddress()判斷IP地址是否是一個loopback地址。

  List1 是一段示範代碼:InetAddressDemo

// InetAddressDemo.java

import java.net.*;

class InetAddressDemo
{
 public static void main (String [] args) throws UnknownHostException
 {
  String host = "localhost";

  if (args.length == 1)
   host = args [0];

  InetAddress ia = InetAddress.getByName (host);

  System.out.println ("Canonical Host Name = " +
        ia.getCanonicalHostName ());
  System.out.println ("Host Address = " +
        ia.getHostAddress ());
  System.out.println ("Host Name = " +
        ia.getHostName ());
  System.out.println ("Is Loopback Address = " +
        ia.isLoopbackAddress ());
 }
}

  當無命令列參數時,代碼輸出類似下面的結果:

Canonical Host Name = localhost
Host Address = 127.0.0.1
Host Name = localhost
Is Loopback Address = true

  InetAddressDemo給了你一個指定主機名稱作為命令列參數的選擇,如果沒有主機名稱被指定,那麼將使用localhost(客戶機的),InetAddressDemo通過調用getByName(String host)方法獲得一個InetAddress子類對象的引用,通過這個引用獲得了標準主機名稱,主機地址,主機名稱以及IP地址是否是loopback地址的輸出。

Socket類

  當客戶程式需要與伺服器程式通訊的時候,客戶程式在客戶機建立一個socket對象,Socket類有幾個建構函式。兩個常用的建構函式是 Socket(InetAddress addr, int port) 和 Socket(String host, int port),兩個建構函式都建立了一個基於Socket的串連伺服器端流通訊端的流通訊端。對於第一個InetAddress子類對象通過addr參數獲得伺服器主機的IP地址,對於第二個函數host參數包被分配到InetAddress對象中,如果沒有IP地址與host參數相一致,那麼將拋出UnknownHostException異常對象。兩個函數都通過參數port獲得伺服器的連接埠號碼。假設已經建立串連了,網路API將在用戶端基於Socket的流通訊端中捆綁客戶程式的IP地址和任意一個連接埠號碼,否則兩個函數都會拋出一個IOException對象。

  如果建立了一個Socket對象,那麼它可能通過調用Socket的 getInputStream()方法從服務程式獲得輸入資料流讀傳送來的資訊,也可能通過調用Socket的 getOutputStream()方法獲得輸出資料流來發送訊息。在讀寫活動完成之後,客戶程式調用close()方法關閉流和流通訊端,下面的代碼建立了一個服務程式主機地址為198.163.227.6,連接埠號碼為13的Socket對象,然後從這個新建立的Socket對象中讀取輸入資料流,然後再關閉流和Socket對象。

Socket s = new Socket ("198.163.227.6", 13);
InputStream is = s.getInputStream ();
// Read from the stream.
is.close ();
s.close ();

  接下面我們將示範一個流通訊端的客戶程式,這個程式將建立一個Socket對象,Socket將訪問運行在指定主機連接埠10000上的服務程式,如果訪問成功客戶程式將給服務程式發送一系列命令並列印服務程式的響應。List2使我們建立的程式SSClient的原始碼:

  Listing 2: SSClient.java

// SSClient.java

import java.io.*;
import java.net.*;

class SSClient
{
 public static void main (String [] args)
 {
  String host = "localhost";

  // If user specifies a command-line argument, that argument
  // represents the host name.

  if (args.length == 1)
   host = args [0];

  BufferedReader br = null;
  PrintWriter pw = null;
  Socket s = null;

  try
  {
   // Create a socket that attempts to connect to the server
   // program on the host at port 10000.

   s = new Socket (host, 10000);

   // 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);

   // Send the DATE command to the server.

   pw.println ("DATE");

   // Obtain and print the current date/time.

   System.out.println (br.readLine ());
   // Send the PAUSE command to the server. This allows several
   // clients to start and verifies that the server is spawning
   // multiple threads.

   pw.println ("PAUSE");
   // Send the DOW command to the server.

   pw.println ("DOW");

   // Obtain and print the current day of week.

   System.out.println (br.readLine ());

   // Send the DOM command to the server.
 
   pw.println ("DOM");

   // Obtain and print the current day of month.

   System.out.println (br.readLine ());

   // Send the DOY command to the server.

   pw.println ("DOY");

   // Obtain and print the current day of year.

   System.out.println (br.readLine ());
  }
  catch (IOException e)
  {
   System.out.println (e.toString ());
  }
  finally
  {
   try
   {
    if (br != null)
     br.close ();

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

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

  運行這段程式將會得到下面的結果:

Tue Jan 29 18:11:51 CST 2002
TUESDAY
29
29

  SSClient建立了一個Socket對象與運行在主機連接埠10000的服務程式聯絡,主機的IP地址由host變數確定。SSClient將獲得Socket的輸入輸出資料流,圍繞BufferedReader的輸入資料流和PrintWriter的輸出資料流對字串進行讀寫操作就變得非常容易,SSClient個服務程式發出各種date/time命令並得到響應,每個響應均被列印,一旦最後一個響應被列印,將執行Try/Catch/Finally結構的Finally子串,Finally子串將在關閉Socket之前關閉BufferedReader 和 PrintWriter。

  在SSClient原始碼編譯完成後,可以輸入java SSClient 來執行這段程式,如果有合適的程式運行在不同的主機上,採用主機名稱/IP地址為參數的輸入方式,比如www.sina.com.cn是運行伺服器程式的主機,那麼輸入方式就是java SSClient www.sina.com.cn。

  技巧

  Socket類包含了許多有用的方法。比如getLocalAddress()將返回一個包含客戶程式IP地址的InetAddress子類對象的引用;getLocalPort()將返回客戶程式的連接埠號碼;getInetAddress()將返回一個包含伺服器IP地址的InetAddress子類對象的引用;getPort()將返回服務程式的連接埠號碼。

ServerSocket類

  由於SSClient使用了流通訊端,所以服務程式也要使用流通訊端。這就要建立一個ServerSocket對象,ServerSocket有幾個建構函式,最簡單的是ServerSocket(int port),當使用ServerSocket(int port)建立一個ServerSocket對象,port參數傳遞連接埠號碼,這個連接埠就是伺服器監聽串連請求的連接埠,如果在這時出現錯誤將拋出IOException異常對象,否則將建立ServerSocket對象並開始準備接收串連請求。

  接下來服務程式進入無限迴圈之中,無限迴圈從調用ServerSocket的accept()方法開始,在調用開始後accept()方法將導致調用線程阻塞直到串連建立。在建立串連後accept()返回一個最近建立的Socket對象,該Socket對象綁定了客戶程式的IP地址或連接埠號碼。

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

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

  Listing 3: SSServer.java

// SSServer.java

import java.io.*;
import java.net.*;
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
   // object 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
   // loop is required. Keep looping until the client either
   // explicitly requests termination 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 DOM (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程式。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.