ds = new DatagramSocket(socketAddress);
}
/**
* 接收数据包,该方法会造成线程阻塞.
*
* @return 返回接收的数据串信息
* @throws IOException
* @author KuToKu.commailto:abczww@163.com">KuToKu.com Creation date:
* 2007-8-16 - 下午10:38:24
*/
public final String receive() throws IOException {
packet = new DatagramPacket(buffer, buffer.length);
ds.receive(packet);
orgIp = packet.getAddress().getHostAddress();
String info = new String(packet.getData(), 0, packet.getLength());
System.out.println("接收信息:" + info);
return info;
}
/**
* 将响应包发送给请求端.
*
* @param bytes
* 回应报文
* @throws IOException
* @author KuToKu.commailto:abczww@163.com">KuToKu.com Creation date:
* 2007-8-16 - 下午11:05:31
*/
public final void response(String info) throws IOException {
System.out.println("客户端地址 : " + packet.getAddress().getHostAddress()
+ ",端口:" + packet.getPort());
DatagramPacket dp = new DatagramPacket(buffer, buffer.length, packet
.getAddress(), packet.getPort());
dp.setData(info.getBytes());
ds.send(dp);
}
/**
* 设置报文的缓冲长度.
*
* @param bufsize
* 缓冲长度
* @author KuToKu.commailto:abczww@163.com">KuToKu.com Creation date:
* 2007-8-16 - 下午10:47:49
*/
public final void setLength(int bufsize) {
packet.setLength(bufsize);
}
/**
* 获得发送回应的IP地址.
*
* @return 返回回应的IP地址
* @author KuToKu.commailto:abczww@163.com">KuToKu.com Creation date:
* 2007-8-16 - 下午10:48:27
*/
public final InetAddress getResponseAddress() {
return packet.getAddress();
}
/**
* 获得回应的主机的端口.
*
* @return 返回回应的主机的端口.
* @author KuToKu.commailto:abczww@163.com">KuToKu.com Creation date:
* 2007-8-16 - 下午10:48:56
*/
public final int getResponsePort() {
return packet.getPort();
}
/**
* 关闭udp监听口.