副标题#e#
当客户措施需要与处事器措施通讯的时候,客户措施在客户机建设一个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 // redivsents 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) { } } } }
#p#副标题#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。
#p#分页标题#e#
在SSClient源代码编译完成后,可以输入java SSClient 来执行这段措施,假如有符合的措施运行在差异的主机上,回收主机名/IP地点为参数的输入方法,好比www.sina.com.cn是运行处事器措施的主机,那么输入方法就是java SSClient www.sina.com.cn。
能力
Socket类包括了很多有用的要领。好比getLocalAddress()将返回一个包括客户措施IP地点的InetAddress子类工具的引用;getLocalPort()将返回客户措施的端标语;getInetAddress()将返回一个包括处事器IP地点的InetAddress子类工具的引用;getPort()将返回处事措施的端标语。