服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java套接字编程(下)(2)


  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

扫描关注微信公众号