當客戶程序需要與服務器程序通訊的時候客戶程序在客戶機創建一個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()方法關閉流和流套接字下面的代碼創建了一個服務程序主機地址為端口號為的Socket對象然後從這個新創建的Socket對象中讀取輸入流然後再關閉流和Socket對象
Socket s = new Socket ( );
InputStream is = sgetInputStream ();
// Read from the stream
isclose ();
sclose ();
接下面我們將示范一個流套接字的客戶程序這個程序將創建一個Socket對象Socket將訪問運行在指定主機端口上的服務程序如果訪問成功客戶程序將給服務程序發送一系列命令並打印服務程序的響應List使我們創建的程序SSClient的源代碼
Listing : SSClientjava
// SSClientjava
import javaio*;
import *;
class SSClient
{
public static void main (String [] args)
{
String host = localhost;
// If user specifies a commandline argument that argument
// redivsents the host name
if (argslength == )
host = args [];
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
s = new Socket (host );
// Create an input stream reader that chains to the sockets
// byteoriented input stream The input stream reader
// converts bytes read from the socket to characters The
// conversion is based on the platforms default character
// set
InputStreamReader isr;
isr = new InputStreamReader (sgetInputStream ());
// 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 sockets 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 platforms default character set
pw = new PrintWriter (sgetOutputStream () true);
// Send the DATE command to the server
pwprintln (DATE);
// Obtain and print the current date/time
Systemoutprintln (brreadLine ());
// Send the PAUSE command to the server This allows several
// clients to start and verifies that the server is spawning
// multiple threads
pwprintln (PAUSE);
// Send the DOW command to the server
pwprintln (DOW);
// Obtain and print the current day of week
Systemoutprintln (brreadLine ());
// Send the DOM command to the server
pwprintln (DOM);
// Obtain and print the current day of month
Systemoutprintln (brreadLine ());
// Send the DOY command to the server
pwprintln (DOY);
// Obtain and print the current day of year
Systemoutprintln (brreadLine ());
}
catch (IOException e)
{
Systemoutprintln (etoString ());
}
finally
{
try
{
if (br != null)
brclose ();
if (pw != null)
pwclose ();
if (s != null)
sclose ();
}
catch (IOException e)
{
}
}
}
}
運行這段程序將會得到下面的結果
Tue Jan :: CST
TUESDAY
SSClient創建了一個Socket對象與運行在主機端口的服務程序聯系主機的IP地址由host變量確定SSClient將獲得Socket的輸入輸出流圍繞BufferedReader的輸入流和PrintWriter的輸出流對字符串進行讀寫操作就變得非常容易SSClient個服務程序發出各種date/time命令並得到響應每個響應均被打印一旦最後一個響應被打印將執行Try/Catch/Finally結構的Finally子串Finally子串將在關閉Socket之前關閉BufferedReader 和 PrintWriter
在SSClient源代碼編譯完成後可以輸入java SSClient 來執行這段程序如果有合適的程序運行在不同的主機上采用主機名/IP地址為參數的輸入方式比如是運行服務器程序的主機那麼輸入方式就是java SSClient
技巧
Socket類包含了許多有用的方法比如getLocalAddress()將返回一個包含客戶程序IP地址的InetAddress子類對象的引用;getLocalPort()將返回客戶程序的端口號;getInetAddress()將返回一個包含服務器IP地址的InetAddress子類對象的引用getPort()將返回服務程序的端口號
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26758.html