Java网络编程基础 Socket类的使用方法详解

上传人:hjk****65 文档编号:178019614 上传时间:2022-12-27 格式:DOC 页数:5 大小:30KB
收藏 版权申诉 举报 下载
Java网络编程基础 Socket类的使用方法详解_第1页
第1页 / 共5页
Java网络编程基础 Socket类的使用方法详解_第2页
第2页 / 共5页
Java网络编程基础 Socket类的使用方法详解_第3页
第3页 / 共5页
资源描述:

《Java网络编程基础 Socket类的使用方法详解》由会员分享,可在线阅读,更多相关《Java网络编程基础 Socket类的使用方法详解(5页珍藏版)》请在装配图网上搜索。

1、当客户程序需要与服务器程序通讯的时候,客户程序在客户机创建一个socket对象,Socket类有几个构造函数。 两个常用的构造函数是 Socket(InetAddress addr, int port) 和 Socket(String host, int port),两个构造函数都创建了一个基于Socket的连接服务器端流套接字的流套接字。对于第一个InetAddress子类对象通过addr参数获得服务器主机的IP地址,对于第二个函数host参数包被分配到InetAddress对象中,如果没有IP地址与host参数相一致,那么将抛出UnknownHostException异常对象。两个函数都通

2、过参数port获得服务器的端口号。假设已经建立连接了,网络API将在客户端基于Socket的流套接字中捆绑客户程序的IP地址和任意一个端口号,否则两个函数都会抛出一个IOException对象。 如果创建了一个Socket对象,那么它可能通过调用Socket的 getInputStream()方法从服务程序获得输入流读传送来的信息,也可能通过调用Socket的 getOutputStream()方法获得输出流来发送消息。在读写活动完成之后,客户程序调用close()方法关闭流和流套接字,下面的代码创建了一个服务程序主机地址为198.163.227.6,端口号为13的Socket对象,然后从这个

3、新创建的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:

4、 SSClient.java / SSClient.javaimport java.io.*;import .*; class SSClientpublic 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;S

5、ocket 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 sockets/ byte-oriented input stream. The input stream reader/ converts bytes read from the socket to character

6、s. The/ conversion is based on the platforms 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 B

7、ufferedReader (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 PrintWr

8、iter (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 threa

9、ds.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 DO

10、Y 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 ();finallytryif (br != null)br.close ();if (pw != null)pw.close ();if (s != null)s.close ();catch (IOException e)运行这段程序将会得到下面的结果:

11、 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之前关闭Buffered

12、Reader 和 PrintWriter。 在SSClient源代码编译完成后,可以输入java SSClient 来执行这段程序,如果有合适的程序运行在不同的主机上,采用主机名/IP地址为参数的输入方式,比如是运行服务器程序的主机,那么输入方式就是java SSClient 。 技巧 Socket类包含了许多有用的方法。比如getLocalAddress()将返回一个包含客户程序IP地址的InetAddress子类对象的引用;getLocalPort()将返回客户程序的端口号;getInetAddress()将返回一个包含服务器IP地址的InetAddress子类对象的引用;getPort()将返回服务程序的端口号。

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!