Java网络编程基础

Java网络编程基础
Java网络编程基础

Java网络编程基础

参数传递给流套接字类和自寻址套接字类构造器或非构造器方法。InetAddress描述了32位或64位IP地址,要完成这个功能,InetAddress类主要依靠两个支持类Inet4Addres s 和 Inet6Address,这三个类是继承关系,InetAddrress是父类,Inet4Address 和 Inet 6Address是子类。

由于InetAddress类只有一个构造函数,而且不能传递参数,所以不能直接创建Inet Address对象,比如下面的做法就是错误的:

InetAddress ia = new InetAddress ();

但我们可以通过下面的5个工厂方法创建来创建一个InetAddress对象或InetAddres s数组:

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

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

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

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

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

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

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

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

List1 是一段示范代码:

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

当客户程序需要与服务器程序通讯的时候,客户程序在客户机创建一个socket对象,Socket类有几个构造函数。

两个常用的构造函数是 Socket(InetAddress addr, int port) 和 Socket(String h ost, 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对象。

// SSClient.java

import java.io.*;

import https://www.360docs.net/doc/b81417844.html,.*;

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)

{

}

}

}

Datagram(数据包)是一种尽力而为的传送数据的方式,它只是把数据的目的地记录在数据包中,然后就直接放在网络上,系统不保证数据是否能安全送到,或者什么时候可以送到,也就是说它并不保证传送质量。

1 UDP套接字

数据报(Datagram)是网络层数据单元在介质上传输信息的一种逻辑分组格式,它是一种在网络中传播的、独立的、自身包含地址信息的消息,它能否到达目的地、到达的时间、到达时内容是否会变化不能准确地知道。它的通信双方是不需要建立连接的,对于一些不需要很高质量的应用程序来说,数据报通信是一个非常好的选择。还有就是对实时性要求很高

的情况,比如在实时音频和视频应用中,数据包的丢失和位置错乱是静态的,是可以被人们所忍受的,但是如果在数据包位置错乱或丢失时要求数据包重传,就是用户所不能忍受的,这时就可以利用UDP协议传输数据包。在Java的https://www.360docs.net/doc/b81417844.html,包中有两个类DatagramSocket 和DatagramPacket,为应用程序中采用数据报通信方式进行网络通信。

使用数据包方式首先将数据打包,Java.net包中的DategramPacket类用来创建数据包。数据包有两种,一种用来传递数据包,该数据包有要传递到的目的地址;另一种数据包用来接收传递过来的数据包中的数据。要创建接收的数据包,通过DatagramPackett类的方法构造:

public DatagramPacket(byte ibuft[],int ilength)

public DatagramPacket( byte ibuft[],int offset ,int ilength)

ibuf[]为接受数据包的存储数据的缓冲区的长度,ilength为从传递过来的数据包中读取的字节数。当采用第一种构造方法时,接收到的数据从ibuft[0]开始存放,直到整个数据包接收完毕或者将ilength的字节写入ibuft为止。采用第二种构造方法时,接收到的数据从ibuft[offset]开始存放。如果数据包长度超出了ilength,则触发IllegalArgument-Exception。不过这是RuntimeException,不需要用户代码捕获。示范代码如下:byte[ ] buffer=new byte[8912];

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

创建发送数据包的构造方法为:

public DatagramPacket(byt ibuf[],int ilength,InetAddrss iaddr,int port)public DatagramPacket(byt ibuf[],int offset , int ilength,InetAddrss iaddr, int port)

iaddr为数据包要传递到的目标地址,port为目标地址的程序接受数据包的端口号(即目标地址的计算机上运行的客户程序是在哪一个端口接收服务器发送过来的数据包)。ibu f[]为要发送数据的存储区,以ibuf数组的offset位置开始填充数据包ilength字节,如果没有offset,则从ibuf数组的0位置开始填充。以下示范代码是要发送一串字符串:

public int getLocalPort() 返回本地套接字的正在监听的端口号。

public void receive(DatagramPacket p) 从网络上接收数据包并将其存储在Datagr amPacket对象p中。p中的数据缓冲区必须足够大,receive()把尽可能多的数据存放在p 中,如果装不下,就把其余的部分丢弃。接收数据出错时会抛出IOException异常。

public Void Send(DatagramPacket p) 发送数据包,出错时会发生IOException异常。

下面,我们详细解释在Java中实现客户端与服务器之间数据报通信的方法。

应用程序的工作流程如下:

(1)首先要建立数据报通信的Socket,我们可以通过创建一个DatagramSocket对象实现它,在Java中DatagramSocket类有如下两种构造方法:

public DatagramSocket() 构造一个数据报socket,并使其与本地主机任一可用的端口连接。若打不开socket则抛出SocketException异常。

publi

由于SSClient使用了流套接字,所以服务程序也要使用流套接字。

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

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

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

为了示范我们在上面谈到的慨念并完成SSClient程序,下面我们创建一个SSServer 程序,程序将创建一个ServerSocket对象来监听端口10000的连接请求,如果成功服务程

序将等待连接输入,开始一个线程处理连接,并响应来自客户程序的命令。下面就是这段程序的代码:

Listing 3: SSServer.java

{

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");

相关主题
相关文档
最新文档