<b>Java网络编程底子(二) Socket类的操纵办法</b>[Java编程]
本文“<b>Java网络编程底子(二) Socket类的操纵办法</b>[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
当客户程序需求与服务器程序通讯的时刻,客户程序在客户机成立一个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) { } } } }
以上是“<b>Java网络编程底子(二) Socket类的操纵办法</b>[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |