45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 电脑教程 > 阅读资讯:java socket资料的详细介绍

java socket资料的详细介绍

2016-08-28 20:13:49 来源:www.45fan.com 【

java socket资料的详细介绍

其实网工的socket考试要求是很低的,基本上是送分题,基本上搞清楚这些应付网工的socket考试就没有问题了,顺便说一下,一个有影响的考试不可能频繁的改变考试内容,所以我个人认为今后的几年网工应该还是考java的socket。

Java套接字编程(上)
用Java开发网络软件非常方便和强大,Java的这种力量来源于他独有的一套强大的用于网络的 API,这些API是一系列的类和接口,均位于包java.net和javax.net中。在这篇文章中我们将介绍套接字(Socket)慨念,同时以实例说明如何使用Network API操纵套接字,在完成本文后,你就可以编写网络低端通讯软件。

什么是套接字(Socket)?

Network API是典型的用于基于TCP/IP网络Java程序与其他程序通讯,Network API依靠Socket进行通讯。Socket可以看成在两个程序进行通讯连接中的一个端点,一个程序将一段信息写入Socket中,该Socket将这段信息发送给另外一个Socket中,使这段信息能传送到其他程序中。

我们来分析一下,Host A上的程序A将一段信息写入Socket中,Socket的内容被Host A的网络管理软件访问,并将这段信息通过Host A的网络接口卡发送到Host B,Host B的网络接口卡接收到这段信息后,传送给Host B的网络管理软件,网络管理软件将这段信息保存在Host B的Socket中,然后程序B才能在Socket中阅读这段信息。

假设在图1的网络中添加第三个主机Host C,那么Host A怎么知道信息被正确传送到Host B而不是被传送到Host C中了呢?基于TCP/IP网络中的每一个主机均被赋予了一个唯一的IP地址,IP地址是一个32位的无符号整数,由于没有转变成二进制,因此通常以小数点分隔,如:198.163.227.6,正如所见IP地址均由四个部分组成,每个部分的范围都是0-255,以表示8位地址。

值得注意的是IP地址都是32位地址,这是IP协议版本4(简称Ipv4)规定的,目前由于IPv4地址已近耗尽,所以IPv6地址正逐渐代替Ipv4地址,Ipv6地址则是128位无符号整数。

假设第二个程序被加入图1的网络的Host B中,那么由Host A传来的信息如何能被正确的传给程序B而不是传给新加入的程序呢?这是因为每一个基于TCP/IP网络通讯的程序都被赋予了唯一的端口和端口号,端口是一个信息缓冲区,用于保留Socket中的输入/输出信息,端口号是一个16位无符号整数,范围是0-65535,以区别主机上的每一个程序(端口号就像房屋中的房间号),低于256的短口号保留给标准应用程序,比如pop3的端口号就是110,每一个套接字都组合进了IP地址、端口、端口号,这样形成的整体就可以区别每一个套接字t,下面我们就来谈谈两种套接字:流套接字和自寻址数据套接字。

流套接字(Stream Socket)

无论何时,在两个网络应用程序之间发送和接收信息时都需要建立一个可靠的连接,流套接字依靠TCP协议来保证信息正确到达目的地,实际上,IP包有可能在网络中丢失或者在传送过程中发生错误,任何一种情况发生,作为接受方的 TCP将联系发送方TCP重新发送这个IP包。这就是所谓的在两个流套接字之间建立可靠的连接。

流套接字在C/S程序中扮演一个必需的角色,客户机程序(需要访问某些服务的网络应用程序)创建一个扮演服务器程序的主机的IP地址和服务器程序(为客户端应用程序提供服务的网络应用程序)的端口号的流套接字对象。

客户端流套接字的初始化代码将IP地址和端口号传递给客户端主机的网络管理软件,管理软件将IP地址和端口号通过NIC传递给服务器端主机;服务器端主机读到经过NIC传递来的数据,然后查看服务器程序是否处于监听状态,这种监听依然是通过套接字和端口来进行的;如果服务器程序处于监听状态,那么服务器端网络管理软件就向客户机网络管理软件发出一个积极的响应信号,接收到响应信号后,客户端流套接字初始化代码就给客户程序建立一个端口号,并将这个端口号传递给服务器程序的套接字(服务器程序将使用这个端口号识别传来的信息是否是属于客户程序)同时完成流套接字的初始化。

如果服务器程序没有处于监听状态,那么服务器端网络管理软件将给客户端传递一个消极信号,收到这个消极信号后,客户程序的流套接字初始化代码将抛出一个异常对象并且不建立通讯连接,也不创建流套接字对象。这种情形就像打电话一样,当有人的时候通讯建立,否则电话将被挂起。

这部分的工作包括了相关联的三个类:InetAddress, Socket, 和 ServerSocket。 InetAddress对象描绘了32位或128位IP地址,Socket对象代表了客户程序流套接字,ServerSocket代表了服务程序流套接字,所有这三个类均位于包java.net中。

InetAddress类

InetAddress类在网络API套接字编程中扮演了一个重要角色。参数传递给流套接字类和自寻址套接字类构造器或非构造器方法。InetAddress描述了32位或64位IP地址,要完成这个功能,InetAddress类主要依靠两个支持类Inet4Address 和 Inet6Address,这三个类是继承关系,InetAddrress是父类,Inet4Address 和 Inet6Address是子类。

由于InetAddress类只有一个构造函数,而且不能传递参数,所以不能直接创建InetAddress对象,比如下面的做法就是错误的:
InetAddress ia = new InetAddress ();
但我们可以通过下面的5个工厂方法创建来创建一个InetAddress对象或InetAddress数组:

. getAllByName(String host)方法返回一个InetAddress对象的引用,每个对象包含一个表示相应主机名的单独的IP地址,这个IP地址是通过host参数传递的,对于指定的主机如果没有IP地址存在那么这个方法将抛出一个UnknownHostException 异常对象。

. getByAddress(byte [] addr)方法返回一个InetAddress对象的引用,这个对象包含了一个Ipv4地址或Ipv6地址,Ipv4地址是一个4字节数组,Ipv6地址是一个16字节地址数组,如果返回的数组既不是4字节的也不是16字节的,那么方法将会抛出一个UnknownHostException异常对象。

. getByAddress(String host, byte [] addr)方法返回一个InetAddress对象的引用,这个InetAddress对象包含了一个由host和4字节的addr数组指定的IP地址,或者是host和16字节的addr数组指定的IP地址,如果这个数组既不是4字节的也不是16位字节的,那么该方法将抛出一个UnknownHostException异常对象。

. getByName(String host)方法返回一个InetAddress对象,该对象包含了一个与host参数指定的主机相对应的IP地址,对于指定的主机如果没有IP地址存在,那么方法将抛出一个UnknownHostException异常对象。

. getLocalHost()方法返回一个InetAddress对象,这个对象包含了本地机的IP地址,考虑到本地主机既是客户程序主机又是服务器程序主机,为避免混乱,我们将客户程序主机称为客户主机,将服务器程序主机称为服务器主机。

上面讲到的方法均提到返回一个或多个InetAddress对象的引用,实际上每一个方法都要返回一个或多个Inet4Address/Inet6Address对象的引用,调用者不需要知道引用的子类型,相反调用者可以使用返回的引用调用InetAddress对象的非静态方法,包括子类型的多态以确保重载方法被调用。

InetAddress和它的子类型对象处理主机名到主机IPv4或IPv6地址的转换,要完成这个转换需要使用域名系统,下面的代码示范了如何通过调用getByName(String host)方法获得InetAddress子类对象的方法,这个对象包含了与host参数相对应的IP地址:
InetAddress ia = InetAddress.getByName (www.javajeff.com));

一但获得了InetAddress子类对象的引用就可以调用InetAddress的各种方法来获得InetAddress子类对象中的IP地址信息,比如,可以通过调用getCanonicalHostName()从域名服务中获得标准的主机名;getHostAddress()获得IP地址,getHostName()获得主机名,isLoopbackAddress()判断IP地址是否是一个loopback地址。

List1 是一段示范代码:InetAddressDemo
// InetAddressDemo.java

import java.net.*;

class InetAddressDemo
{
public static void main (String [] args) throws UnknownHostException
{
String host = "localhost";

if (args.length == 1)
host = args [0];

InetAddress ia = InetAddress.getByName (host);

System.out.println ("Canonical Host Name = " +
ia.getCanonicalHostName ());
System.out.println ("Host Address = " +
ia.getHostAddress ());
System.out.println ("Host Name = " +
ia.getHostName ());
System.out.println ("Is Loopback Address = " +
ia.isLoopbackAddress ());
}
}

当无命令行参数时,代码输出类似下面的结果:
Canonical Host Name = localhost
Host Address = 127.0.0.1
Host Name = localhost
Is Loopback Address = true

InetAddressDemo给了你一个指定主机名作为命令行参数的选择,如果没有主机名被指定,那么将使用localhost(客户机的),InetAddressDemo通过调用getByName(String host)方法获得一个InetAddress子类对象的引用,通过这个引用获得了标准主机名,主机地址,主机名以及IP地址是否是loopback地址的输出。

 


Socket类

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

技巧

Socket类包含了许多有用的方法。比如getLocalAddress()将返回一个包含客户程序IP地址的InetAddress子类对象的引用;getLocalPort()将返回客户程序的端口号;getInetAddress()将返回一个包含服务器IP地址的InetAddress子类对象的引用;getPort()将返回服务程序的端口号。

 

ServerSocket类

由于SSClient使用了流套接字,所以服务程序也要使用流套接字。这就要创建一个ServerSocket对象,ServerSocket有几个构造函数,最简单的是ServerSocket(int port),当使用ServerSocket(int port)创建一个ServerSocket对象,port参数传递端口号,这个端口就是服务器监听连接请求的端口,如果在这时出现错误将抛出IOException异常对象,否则将创建ServerSocket对象并开始准备接收连接请求。

接下来服务程序进入无限循环之中,无限循环从调用ServerSocket的accept()方法开始,在调用开始后accept()方法将导致调用线程阻塞直到连接建立。在建立连接后accept()返回一个最近创建的Socket对象,该Socket对象绑定了客户程序的IP地址或端口号。

由于存在单个服务程序与多个客户程序通讯的可能,所以服务程序响应客户程序不应该花很多时间,否则客户程序在得到服务前有可能花很多时间来等待通讯的建立,然而服务程序和客户程序的会话有可能是很长的(这与电话类似),因此为加快对客户程序连接请求的响应,典型的方法是服务器主机运行一个后台线程,这个后台线程处理服务程序和客户程序的通讯。

为了示范我们在上面谈到的慨念并完成SSClient程序,下面我们创建一个SSServer程序,程序将创建一个ServerSocket对象来监听端口10000的连接请求,如果成功服务程序将等待连接输入,开始一个线程处理连接,并响应来自客户程序的命令。下面就是这段程序的代码:

Listing 3: SSServer.java
// SSServer.java

import java.io.*;
import java.net.*;
import java.util.*;

class SSServer
{
public static void main (String [] args) throws IOException
{
System.out.println ("Server starting.../n");

// Create a server socket that listens for incoming connection
// requests on port 10000.

ServerSocket server = new ServerSocket (10000);

while (true)
{
// Listen for incoming connection requests from client
// programs, establish a connection, and return a Socket
// object that represents this connection.

Socket s = server.accept ();

System.out.println ("Accepting Connection.../n");

// Start a thread to handle the connection.

new ServerThread (s).start ();
}
}
}

class ServerThread extends Thread
{
private Socket s;

ServerThread (Socket s)
{
this.s = s;
}

public void run ()
{
BufferedReader br = null;
PrintWriter pw = null;

try
{
// 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);

// Create a calendar that makes it possible to obtain date
// and time information.

Calendar c = Calendar.getInstance ();

// Because the client program may send multiple commands, a
// loop is required. Keep looping until the client either
// explicitly requests termination by sending a command
// beginning with letters BYE or implicitly requests
// termination by closing its output stream.

do
{
// Obtain the client program's next command.

String cmd = br.readLine ();

// Exit if client program has closed its output stream.

if (cmd == null)
break;

// Convert command to uppercase, for ease of comparison.

cmd = cmd.toUpperCase ();

// If client program sends BYE command, terminate.

if (cmd.startsWith ("BYE"))
break;

// If client program sends DATE or TIME command, return
// current date/time to the client program.

if (cmd.startsWith ("DATE") || cmd.startsWith ("TIME"))
pw.println (c.getTime ().toString ());

// If client program sends DOM (Day Of Month) command,
// return current day of month to the client program.

if (cmd.startsWith ("DOM"))
pw.println ("" + c.get (Calendar.DAY_OF_MONTH));

// If client program sends DOW (Day Of Week) command,
// return current weekday (as a string) to the client
// program.

if (cmd.startsWith ("DOW"))
switch (c.get (Calendar.DAY_OF_WEEK))
{
case Calendar.SUNDAY : pw.println ("SUNDAY");
break;

case Calendar.MONDAY : pw.println ("MONDAY");
break;

case Calendar.TUESDAY : pw.println ("TUESDAY");
break;

case Calendar.WEDNESDAY: pw.println ("WEDNESDAY");
break;

case Calendar.THURSDAY : pw.println ("THURSDAY");
break;

case Calendar.FRIDAY : pw.println ("FRIDAY");
break;

case Calendar.SATURDAY : pw.println ("SATURDAY");
}

// If client program sends DOY (Day of Year) command,
// return current day of year to the client program.

if (cmd.startsWith ("DOY"))
pw.println ("" + c.get (Calendar.DAY_OF_YEAR));

// If client program sends PAUSE command, sleep for three
// seconds.

if (cmd.startsWith ("PAUSE"))
try
{
Thread.sleep (3000);
}
catch (InterruptedException e)
{
}
}
while (true);
{
catch (IOException e)
{
System.out.println (e.toString ());
}
finally
{
System.out.println ("Closing Connection.../n");

try
{
if (br != null)
br.close ();

if (pw != null)
pw.close ();

if (s != null)
s.close ();
}
catch (IOException e)
{
}
}
}
}
运行这段程序将得到下面的输出:

 


Server starting...
Accepting Connection...
Closing Connection...
SSServer的源代码声明了一对类:SSServer 和ServerThread;SSServer的main()方法创建了一个ServerSocket对象来监听端口10000上的连接请求,如果成功, SSServer进入一个无限循环中,交替调用ServerSocket的 accept() 方法来等待连接请求,同时启动后台线程处理连接(accept()返回的请求)。线程由ServerThread继承的start()方法开始,并执行ServerThread的run()方法中的代码。

一旦run()方法运行,线程将创建BufferedReader, PrintWriter和 Calendar对象并进入一个循环,这个循环由读(通过BufferedReader的 readLine())来自客户程序的一行文本开始,文本(命令)存储在cmd引用的string对象中,如果客户程序过早的关闭输出流,会发生什么呢?答案是:cmd将得不到赋值。

注意必须考虑到这种情况:在服务程序正在读输入流时,客户程序关闭了输出流,如果没有对这种情况进行处理,那么程序将产生异常。

一旦编译了SSServer的源代码,通过输入Java SSServer来运行程序,在开始运行SSServer后,就可以运行一个或多个SSClient程序。

自寻址套接字(Datagram Sockets)

,因为使用流套接字的每个连接均要花费一定的时间,要减少这种开销,网络API提供了第二种套接字:自寻址套接字(datagram socket),自寻址使用UDP发送寻址信息(从客户程序到服务程序或从服务程序到客户程序),不同的是可以通过自寻址套接字发送多IP信息包,自寻址信息包含在自寻址包中,此外自寻址包又包含在IP包内,这就将寻址信息长度限制在60000字节内。图2显示了位于IP包内的自寻址包的自寻址信息。
与TCP保证信息到达信息目的地的方式不同,UDP提供了另外一种方法,如果自寻址信息包没有到达目的地,,那么UDP也不会请求发送者重新发送自寻址包,这是因为UDP在每一个自寻址包中包含了错误检测信息,在每个自寻址包到达目的地之后UDP只进行简单的错误检查,如果检测失败,UDP将抛弃这个自寻址包,也不会从发送者那里重新请求替代者,这与通过邮局发送信件相似,发信人在发信之前不需要与收信人建立连接,同样也不能保证信件能到达收信人那里

自寻址套接字工作包括下面三个类:DatagramPacket, DatagramSocket,和 MulticastSocket。DatagramPacket对象描绘了自寻址包的地址信息,DatagramSocket表示客户程序和服务程序自寻址套接字,MulticastSocket描绘了能进行多点传送的自寻址套接字,这三个类均位于java.net包内。

DatagramPacket类

在使用自寻址包之前,你需要首先熟悉DatagramPacket类,地址信息和自寻址包以字节数组的方式同时压缩入这个类创建的对象中

DatagramPacket有数个构造函数,即使这些构造函数的形式不同,但通常情况下他们都有两个共同的参数:byte [] buffer 和 int length,buffer参数包含了一个对保存自寻址数据包信息的字节数组的引用,length表示字节数组的长度。

最简单的构造函数是DatagramPacket(byte [] buffer, int length),这个构造函数确定了自寻址数据包数组和数组的长度,但没有任何自寻址数据包的地址和端口信息,这些信息可以后面通过调用方法setAddress(InetAddress addr)和setPort(int port)添加上,下面的代码示范了这些函数和方法。
byte [] buffer = new byte [100];
DatagramPacket dgp = new DatagramPacket (buffer, buffer.length);
InetAddress ia = InetAddress.getByName ("www.disney.com");
dgp.setAddress (ia);
dgp.setPort (6000); // Send datagram packet to port 6000.

 

如果你更喜欢在调用构造函数的时候同时包括地址和端口号,可以使用DatagramPacket(byte [] buffer, int length, InetAddress addr, int port)函数,下面的代码示范了另外一种选择。

 


byte [] buffer = new byte [100];
InetAddress ia = InetAddress.getByName ("www.disney.com");
DatagramPacket dgp = new DatagramPacket (buffer, buffer.length, ia,
6000);

 


有时候在创建了DatagramPacket对象后想改变字节数组和他的长度,这时可以通过调用setData(byte [] buffer) 和 setLength(int length)方法来实现。在任何时候都可以通过调用getData() 来得到字节数组的引用,通过调用getLength()来获得字节数组的长度。下面的代码示范了这些方法:

 


byte [] buffer2 = new byte [256];
dgp.setData (buffer2);
dgp.setLength (buffer2.length);

 


关于DatagramPacket的更多信息请参考SDK文档。
DatagramSocket类

DatagramSocket类在客户端创建自寻址套接字与服务器端进行通信连接,并发送和接受自寻址套接字。虽然有多个构造函数可供选择,但我发现创建客户端自寻址套接字最便利的选择是DatagramSocket()函数,而服务器端则是DatagramSocket(int port)函数,如果未能创建自寻址套接字或绑定自寻址套接字到本地端口,那么这两个函数都将抛出一个SocketException对象,一旦程序创建了DatagramSocket对象,那么程序分别调用send(DatagramPacket dgp)和 receive(DatagramPacket dgp)来发送和接收自寻址数据包,

List4显示的DGSClient源代码示范了如何创建自寻址套接字以及如何通过套接字处理发送和接收信息

 


Listing 4: DGSClient.java
// DGSClient.java

import java.io.*;
import java.net.*;

class DGSClient
{
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];

DatagramSocket s = null;

try
{
// Create a datagram socket bound to an arbitrary port.

s = new DatagramSocket ();

// Create a byte array that will hold the data portion of a
// datagram packet's message. That message originates as a
// String object, which gets converted to a sequence of
// bytes when String's getBytes() method is called. The
// conversion uses the platform's default character set.

byte [] buffer;
buffer = new String ("Send me a datagram").getBytes ();

// Convert the name of the host to an InetAddress object.
// That object contains the IP address of the host and is
// used by DatagramPacket.

InetAddress ia = InetAddress.getByName (host);

// Create a DatagramPacket object that encapsulates a
// reference to the byte array and destination address
// information. The destination address consists of the
// host's IP address (as stored in the InetAddress object)
// and port number 10000 -- the port on which the server
// program listens.

DatagramPacket dgp = new DatagramPacket (buffer,
buffer.length,
ia,
10000);

// Send the datagram packet over the socket.

s.send (dgp);

// Create a byte array to hold the response from the server.
// program.

byte [] buffer2 = new byte [100];

// Create a DatagramPacket object that specifies a buffer
// to hold the server program's response, the IP address of
// the server program's computer, and port number 10000.

dgp = new DatagramPacket (buffer2,
buffer.length,
ia,
10000);

// Receive a datagram packet over the socket.

s.receive (dgp);

// Print the data returned from the server program and stored
// in the datagram packet.

System.out.println (new String (dgp.getData ()));

}
catch (IOException e)
{
System.out.println (e.toString ());
}
finally
{
if (s != null)
s.close ();
}
}
}


DGSClient由创建一个绑定任意本地(客户端)端口好的DatagramSocket对象开始,然后装入带有文本信息的数组buffer和描述服务器主机IP地址的InetAddress子类对象的引用,接下来,DGSClient创建了一个DatagramPacket对象,该对象加入了带文本信息的缓冲器的引用,InetAddress子类对象的引用,以及服务端口号10000, DatagramPacket的自寻址数据包通过方法sent()发送给服务器程序,于是一个包含服务程序响应的新的DatagramPacket对象被创建,receive()得到响应的自寻址数据包,然后自寻址数据包的getData()方法返回该自寻址数据包的一个引用,最后关闭DatagramSocket。

DGSServer服务程序补充了DGSClient的不足,List5是DGSServer的源代码:

 

Listing 5: DGSServer.java
// DGSServer.java

import java.io.*;
import java.net.*;

class DGSServer
{
public static void main (String [] args) throws IOException
{
System.out.println ("Server starting .../n");

// Create a datagram socket bound to port 10000. Datagram
// packets sent from client programs arrive at this port.

DatagramSocket s = new DatagramSocket (10000);

// Create a byte array to hold data contents of datagram
// packet.

byte [] data = new byte [100];

// Create a DatagramPacket object that encapsulates a reference
// to the byte array and destination address information. The
// DatagramPacket object is not initialized to an address
// because it obtains that address from the client program.

DatagramPacket dgp = new DatagramPacket (data, data.length);

// Enter an infinite loop. Press Ctrl+C to terminate program.

while (true)
{
// Receive a datagram packet from the client program.

s.receive (dgp);

// Display contents of datagram packet.

System.out.println (new String (data));

// Echo datagram packet back to client program.

s.send (dgp);
}
}
}

 


DGSServer创建了一个绑定端口10000的自寻址套接字,然后创建一个字节数组容纳自寻址信息,并创建自寻址包,下一步,DGSServer进入一个无限循环中以接收自寻址数据包、显示内容并将响应返回客户端,自寻址套接没有关闭,因为循环是无限的。

在编译DGSServer 和DGSClient的源代码后,由输入java DGSServer开始运行DGSServer,然后在同一主机上输入Java DGSClient开始运行DGSClient,如果DGSServer与DGSClient运行于不同主机,在输入时注意要在命令行加上服务程序的主机名或IP地址,如:java DGSClient www.yesky.com


多点传送和MulticastSocket类

前面的例子显示了服务器程序线程发送单一的消息(通过流套接字或自寻址套接字)给唯一的客户端程序,这种行为被称为单点传送(unicasting),多数情况都不适合于单点传送,比如,摇滚歌手举办一场音乐会将通过互联网进行播放,画面和声音的质量依赖于传输速度,服务器程序要传送大约10亿字节的数据给客户端程序,使用单点传送,那么每个客户程序都要要复制一份数据,如果,互联网上有10000个客户端要收看这个音乐会,那么服务器程序通过Internet要传送10000G的数据,这必然导致网络阻塞,降低网络的传输速度。

如果服务器程序要将同一信息发送给多个客户端,那么服务器程序和客户程序可以利用多点传送(multicasting)方式进行通信。多点传送就是服务程序对专用的多点传送组的IP地址和端口发送一系列自寻址数据包,通过加入操作IP地址被多点传送Socket注册,通过这个点客户程序可以接收发送给组的自寻址包(同样客户程序也可以给这个组发送自寻址包),一旦客户程序读完所有要读的自寻址数据包,那么可以通过离开组操作离开多点传送组。

注意:IP地址224.0.0.1 到 239.255.255.255(包括)均为保留的多点传送组地址。

网络API通过MulticastSocket类和MulticastSocket,以及一些辅助类(比如NetworkInterface)支持多点传送,当一个客户程序要加入多点传送组时,就创建一个MulticastSocket对象。MulticastSocket(int port)构造函数允许应用程序指定端口(通过port参数)接收自寻址包,端口必须与服务程序的端口号相匹配,要加入多点传送组,客户程序调用两个joinGroup()方法中的一个,同样要离开传送组,也要调用两个leaveGroup()方法中的一个。

由于MulticastSocket扩展了DatagramSocket类,一个MulticastSocket对象就有权访问DatagramSocket方法。

List6是MCClient的源代码,这段代码示范了一个客户端加入多点传送组的例子。

 


Listing 6: MCClient.java
// MCClient.java

import java.io.*;
import java.net.*;

class MCClient
{
public static void main (String [] args) throws IOException
{
// Create a MulticastSocket bound to local port 10000. All
// multicast packets from the server program are received
// on that port.

MulticastSocket s = new MulticastSocket (10000);

// Obtain an InetAddress object that contains the multicast
// group address 231.0.0.1. The InetAddress object is used by
// DatagramPacket.

InetAddress group = InetAddress.getByName ("231.0.0.1");

// Join the multicast group so that datagram packets can be
// received.

s.joinGroup (group);

// Read several datagram packets from the server program.

for (int i = 0; i < 10; i++)
{
// No line will exceed 256 bytes.

byte [] buffer = new byte [256];

// The DatagramPacket object needs no addressing
// information because the socket contains the address.

DatagramPacket dgp = new DatagramPacket (buffer,
buffer.length);

// Receive a datagram packet.

s.receive (dgp);

// Create a second byte array with a length that matches
// the length of the sent data.

byte [] buffer2 = new byte [dgp.getLength ()];

// Copy the sent data to the second byte array.

System.arraycopy (dgp.getData (),
0,
buffer2,
0,
dgp.getLength ());

// Print the contents of the second byte array. (Try
// printing the contents of buffer. You will soon see why
// buffer2 is used.)

System.out.println (new String (buffer2));
}

// Leave the multicast group.

s.leaveGroup (group);

// Close the socket.

s.close ();
}
}

 


MCClient创建了一个绑定端口号10000的MulticastSocket对象,接下来他获得了一个InetAddress子类对象,该子类对象包含多点传送组的IP地址231.0.0.0,然后通过joinGroup(InetAddress addr)方法加入多点传送组中,接下来MCClient接收10个自寻址包,同时输出他们的内容,然后使用leaveGroup(InetAddress addr)方法离开传送组,最后关闭套接字。

也许你对使用两个字节数组buffer 和 buffer2感到奇怪,当接收到一个自寻址包后,getData()方法返回一个引用,自寻址包的长度是256个字节,如果要输出所有数据,在输出完实际数据后会有很多空格,这显然是不合理的,所以我们必须去掉这些空格,因此我们创建一个小的字节数组buffer2,buffer2的实际长度就是数据的实际长度,通过调用DatagramPacket's getLength()方法来得到这个长度。从buffer 到 buffer2快速复制getLength()的长度的方法是调用System.arraycopy()方法。
List7 MCServer的源代码显示了服务程序是怎样工作的。

 

Listing 7: MCServer.java
// MCServer.java

import java.io.*;
import java.net.*;

class MCServer
{
public static void main (String[] args) throws IOException
{
System.out.println ("Server starting.../n");

// Create a MulticastSocket not bound to any port.

MulticastSocket s = new MulticastSocket ();

// Because MulticastSocket subclasses DatagramSocket, it is
// legal to replace MulticastSocket s = new MulticastSocket ();
// with the following line.

 

document.write("");
// DatagramSocket s = new DatagramSocket ();

// Obtain an InetAddress object that contains the multicast
// group address 231.0.0.1. The InetAddress object is used by
// DatagramPacket.

InetAddress group = InetAddress.getByName ("231.0.0.1");

// Create a DatagramPacket object that encapsulates a reference
// to a byte array (later) and destination address
// information. The destination address consists of the
// multicast group address (as stored in the InetAddress object)
// and port number 10000 -- the port to which multicast datagram
// packets are sent. (Note: The dummy array is used to prevent a
// NullPointerException object being thrown from the
// DatagramPacket constructor.)

byte [] dummy = new byte [0];

DatagramPacket dgp = new DatagramPacket (dummy,
0,
group,
10000);

// Send 30000 Strings to the port.

for (int i = 0; i < 30000; i++)
{
// Create an array of bytes from a String. The platform's
// default character set is used to convert from Unicode
// characters to bytes.

byte [] buffer = ("Video line " + i).getBytes ();

// Establish the byte array as the datagram packet's
// buffer.

dgp.setData (buffer);

// Establish the byte array's length as the length of the
// datagram packet's buffer.

dgp.setLength (buffer.length);

// Send the datagram to all members of the multicast group
// that listen on port 10000.

s.send (dgp);
}

// Close the socket.

s.close ();
}
}

 


MCServer创建了一个MulticastSocket对象,由于他是DatagramPacket对象的一部分,所以他没有绑定端口号,DatagramPacket有多点传送组的IP地址(231.0.0.0),一旦创建DatagramPacket对象,MCServer就进入一个发送30000条的文本的循环中,对文本的每一行均要创建一个字节数组,他们的引用均存储在前面创建的DatagramPacket对象中,通过send()方法,自寻址包发送给所有的组成员。

在编译了MCServer 和 MCClient后,通过输入java MCServer开始运行MCServer,最后再运行一个或多个MCClient。

结论

本文通过研究套接字揭示了Java的网络API的应用方法,我们介绍了套接自的慨念和套接字的组成,以及流套接字和自寻址套接字,以及如何使用InetAddress, Socket, ServerSocket, DatagramPacket, DatagramSocket和MulticastSocket类。在完成本文后就可以编写基本的底层通讯程序。

 

 

本文地址:http://www.45fan.com/dnjc/69003.html
Tags: 资料 Java Socket
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部