當客戶程式需要與伺服器程式通訊的時候,客戶程式在客戶機建立一個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)
{
}
} }}