网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > Java
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.学习用于异常处理的terminate()函.
.给 Java SE 注入脚本语言的活力.
.入门基础:Java用synth自定义皮肤.
.开发工具:Java规则引擎工作原理.
.Java Web Service.
.Java本地接口工作方式初探.
.Nokia开发者平台Series60介绍.
.使用模仿对象进行单元测试.
.EJB3.0开发指南之有状态会话Bean.
.JAVA程序实现监视----JAVA程序内.
.用Java实现音频播放.
.介绍——用户认证管理设计方案.
.用Spring AOP实现开发中松散耦合.
.Java连接数据库谈.
.您的 Java 代码安全吗.
.防止到 String 类的不恰当的类型.
.SAS9新体验:在DATA STEP中使用JA.
.JAVA基础知识(6).
.J2EE中使用Display标记库来展示表.
.轻松实现Java用户界面编程.

Java语言的Socket类

发表日期:2008-1-5


当客户程序需要与服务器程序通讯的时候,客户程序在客户机创建一个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

// 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)

 {

}

} }}

运行这段程序将会得到下面的结果:

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。

在SSClient源代码编译完成后,可以输入java SSClient 来执行这段程序,假如有合适的程序运行在不同的主机上,采用主机名/IP地址为参数的输入方式,比如www.sina.com.cn是运行服务器程序的主机,那么输入方式就是java SSClient www.sina.com.cn。

技巧


上一篇:Java的复杂数据类型 人气:475
下一篇:Java语言的util类 人气:430
浏览全部Java的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐