科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道Java套接字编程(下)

Java套接字编程(下)

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

本文将为大家介绍Java套接字编程。

作者:Imain 来源:CSDN 2008年3月3日

关键字: 编程 套接字 java

  • 评论
  • 分享微博
  • 分享邮件

在本页阅读全文(共4页)

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 (); 
  }
 }
}

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章