UDP通信

来源:互联网 发布:百分百客户采集软件 编辑:程序博客网 时间:2024/05/22 15:47

1

/******************************************************************************* * Copyright (c) 2015, 2015 Hirain Technologies Corporation. ******************************************************************************/package udp;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.InetSocketAddress;import java.net.SocketException;/** * Copyright 2007 GuangZhou Cotel Co. Ltd. * All right reserved. * UTP服务类. *  * @author QPING */public class UdpServerSocket {public static int PORT = 1223;private final byte[] buffer = new byte[1024];private DatagramSocket datagramSocket = null;private DatagramPacket datagramPacket = null;private InetSocketAddress socketAddress = null;private String orgIp;/** * 构造函数,绑定主机和端口. *  * @param host *            主机 * @param port *            端口 * @throws Exception */public UdpServerSocket(final String host, final int port) throws Exception {socketAddress = new InetSocketAddress(host, port);datagramSocket = new DatagramSocket(socketAddress);System.out.println("服务端启动!");}public final String getOrgIp() {return orgIp;}/** * 设置超时时间,该方法必须在bind方法之后使用. *  * @param timeout *            超时时间 * @throws Exception */public final void setSoTimeout(final int timeout) throws Exception {datagramSocket.setSoTimeout(timeout);}/** * 获得超时时间. *  * @return 返回超时时间. * @throws Exception */public final int getSoTimeout() throws Exception {return datagramSocket.getSoTimeout();}/** * 绑定监听地址和端口. *  * @param host *            主机IP * @param port *            端口 * @throws SocketException */public final void bind(final String host, final int port) throws SocketException {socketAddress = new InetSocketAddress(host, port);datagramSocket = new DatagramSocket(socketAddress);}/** * 接收数据包,该方法会造成线程阻塞. *  * @return 返回接收的数据串信息 * @throws IOException */public final String receive() throws IOException {datagramPacket = new DatagramPacket(buffer, buffer.length);datagramSocket.receive(datagramPacket);orgIp = datagramPacket.getAddress().getHostAddress();final String info = new String(datagramPacket.getData(), 0, datagramPacket.getLength());System.out.println("服务端接收到客户端信息:" + info);return info;}/** * 将响应包发送给请求端. *  * @param bytes *            回应报文 * @throws IOException */public final void response(final String info) throws IOException {System.out.println("服务端响应客户端----->客户端地址 : " + datagramPacket.getAddress().getHostAddress() + ",端口:" + datagramPacket.getPort());final DatagramPacket dp = new DatagramPacket(buffer, buffer.length, datagramPacket.getAddress(), datagramPacket.getPort());dp.setData(info.getBytes());datagramSocket.send(dp);}/** * 设置报文的缓冲长度. *  * @param bufsize *            缓冲长度 */public final void setLength(final int bufsize) {datagramPacket.setLength(bufsize);}/** * 获得发送回应的IP地址. *  * @return 返回回应的IP地址 */public final InetAddress getResponseAddress() {return datagramPacket.getAddress();}/** * 获得回应的主机的端口. *  * @return 返回回应的主机的端口. */public final int getResponsePort() {return datagramPacket.getPort();}/** * 关闭udp监听口. */public final void close() {try {datagramSocket.close();} catch (final Exception ex) {ex.printStackTrace();}}/** * 测试方法. *  * @param args * @throws Exception */public static void main(final String[] args) throws Exception {final String serverHost = "127.0.0.1";final int serverPort = PORT;final UdpServerSocket udpServerSocket = new UdpServerSocket(serverHost, serverPort);while (true) {udpServerSocket.receive();udpServerSocket.response("服务端响应客户端内容:你好,sterning!");}}}


2

/******************************************************************************* * Copyright (c) 2015, 2015 Hirain Technologies Corporation. ******************************************************************************/package udp;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;/** * Copyright 2007 GuangZhou Cotel Co. Ltd. * All right reserved. * UDP客户端程序,用于对服务端发送数据,并接收服务端的回应信息. *  * @author QPING */public class UdpClientSocket {private final byte[] buffer = new byte[1024];private DatagramSocket datagramSocket = null;/** * 构造函数,创建UDP客户端 *  * @throws Exception */public UdpClientSocket() throws Exception {datagramSocket = new DatagramSocket();}/** * 设置超时时间,该方法必须在bind方法之后使用. *  * @param timeout *            超时时间 * @throws Exception */public final void setSoTimeout(final int timeout) throws Exception {datagramSocket.setSoTimeout(timeout);}/** * 获得超时时间. *  * @return 返回超时时间 * @throws Exception */public final int getSoTimeout() throws Exception {return datagramSocket.getSoTimeout();}public final DatagramSocket getSocket() {return datagramSocket;}/** * 向指定的服务端发送数据信息. *  * @param host *            服务器主机地址 * @param port *            服务端端口 * @param bytes *            发送的数据信息 * @return 返回构造后俄数据报 * @throws IOException */public final DatagramPacket send(final String host, final int port, final byte[] bytes) throws IOException {final DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress.getByName(host), port);datagramSocket.send(dp);return dp;}/** * 接收从指定的服务端发回的数据. *  * @param lhost *            服务端主机 * @param lport *            服务端端口 * @return 返回从指定的服务端发回的数据. * @throws Exception */public final String receive(final String lhost, final int lport) throws Exception {final DatagramPacket dp = new DatagramPacket(buffer, buffer.length);datagramSocket.receive(dp);final String info = new String(dp.getData(), 0, dp.getLength());return info;}/** * 关闭udp连接. */public final void close() {try {datagramSocket.close();} catch (final Exception ex) {ex.printStackTrace();}}/** * 测试客户端发包和接收回应信息的方法. *  * @param args * @throws Exception */public static void main(final String[] args) throws Exception {final UdpClientSocket client = new UdpClientSocket();final String serverHost = "127.0.0.1";final int serverPort = UdpServerSocket.PORT;// 此处打断点看debug信息client.send(serverHost, serverPort, "客户端向服务器发送:你好,阿蜜果!".getBytes());// 此处打断点看debug信息final String info = client.receive(serverHost, serverPort);System.out.println("客户端接收到服务端回应的数据:" + info);}}


3


0 0
原创粉丝点击