国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

Socket類

2019-11-18 15:10:30
字體:
來源:轉載
供稿:網(wǎng)友

  當客戶程序需要與服務器程序通訊的時候,客戶程序在客戶機創(chuàng)建一個socket對象,Socket類有幾個構造函數(shù)。兩個常用的構造函數(shù)是 Socket(InetAddress addr, int port) 和 Socket(String host, int port),兩個構造函數(shù)都創(chuàng)建了一個基于Socket的連接服務器端流套接字的流套接字。對于第一個InetAddress子類對象通過addr參數(shù)獲得服務器主機的ip地址,對于第二個函數(shù)host參數(shù)包被分配到InetAddress對象中,假如沒有IP地址與host參數(shù)相一致,那么將拋出UnknownHostException異常對象。兩個函數(shù)都通過參數(shù)port獲得服務器的端口號。假設已經(jīng)建立連接了,網(wǎng)絡API將在客戶端基于Socket的流套接字中捆綁客戶程序的IP地址和任意一個端口號,否則兩個函數(shù)都會拋出一個IOException對象。

  假如創(chuàng)建了一個Socket對象,那么它可能通過調(diào)用Socket的 getInputStream()方法從服務程序獲得輸入流讀傳送來的信息,也可能通過調(diào)用Socket的 getOutputStream()方法獲得輸出流來發(fā)送消息。在讀寫活動完成之后,客戶程序調(diào)用close()方法關閉流和流套接字,下面的代碼創(chuàng)建了一個服務程序主機地址為198.163.227.6,端口號為13的Socket對象,然后從這個新創(chuàng)建的Socket對象中讀取輸入流,然后再關閉流和Socket對象。

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

  接下面我們將示范一個流套接字的客戶程序,這個程序?qū)?chuàng)建一個Socket對象,Socket將訪問運行在指定主機端口10000上的服務程序,假如訪問成功客戶程序?qū)⒔o服務程序發(fā)送一系列命令并打印服務程序的響應。List2使我們創(chuàng)建的程序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)
   {
    }
  }
 }
}

  運行這段程序?qū)玫较旅娴慕Y果:

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

  SSClient創(chuàng)建了一個Socket對象與運行在主機端口10000的服務程序聯(lián)系,主機的IP地址由host變量確定。SSClient將獲得Socket的輸入輸出流,圍繞BufferedReader的輸入流和PrintWriter的輸出流對字符串進行讀寫操作就變得非常輕易,SSClient個服務程序發(fā)出各種date/time命令并得到響應,每個響應均被打印,一旦最后一個響應被打印,將執(zhí)行Try/Catch/Finally結構的Finally子串,F(xiàn)inally子串將在關閉Socket之前關閉BufferedReader 和 PrintWriter。

  在SSClient源代碼編譯完成后,可以輸入java SSClient 來執(zhí)行這段程序,假如有合適的程序運行在不同的主機上,采用主機名/IP地址為參數(shù)的輸入方式,比如www.sina.com.cn是運行服務器程序的主機,那么輸入方式就是java SSClient www.sina.com.cn。

  技巧

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 双城市| 克拉玛依市| 永仁县| 额尔古纳市| 彝良县| 卓尼县| 宣城市| 临城县| 桃源县| 舒城县| 新和县| 墨玉县| 宁国市| 兴和县| 湘潭市| 南木林县| 利川市| 彰化市| 分宜县| 北川| 桂东县| 靖西县| 葫芦岛市| 轮台县| 蓬莱市| 连江县| 聂拉木县| 汨罗市| 和田市| 嘉义市| 开封市| 二连浩特市| 安康市| 抚顺县| 平湖市| 农安县| 瑞金市| 丽江市| 吴忠市| 麦盖提县| 通榆县|