快速业务通道

Java套接字编程(下) - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-22
mSocket(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 ();    }   } }

Java套接字编程(下)(3)

时间:2010-12-19

DGSClient由创建一个绑定任意本地(客户端)端口好的DatagramSocket对象开始,然后装入带有

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号