您的位置:首页 > 编程语言 > Java开发

UDP java接受数据与发送数据

2016-03-04 15:56 465 查看
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;

import java.util.Random;

/**

* Copyright 2007 GuangZhou Cotel Co. Ltd.

* All right reserved.

* UTP服务类.

* @author QPING

*/

public class UdpServer {

private byte[] buffer = new byte[1024];

private DatagramSocket ds = null;

private DatagramPacket packet = null;

private InetSocketAddress socketAddress = null;

private String orgIp;

/**

* 构造函数,绑定主机和端口.

* @param host 主机

* @param port 端口

* @throws Exception

*/

public UdpServer(String host, int port) throws Exception {

socketAddress = new InetSocketAddress(host, port);

ds = new DatagramSocket(socketAddress);

System.out.println("server start...");

}

public final String getOrgIp() {

return orgIp;

}

/**

* 设置超时时间,该方法必须在bind方法之后使用.

* @param timeout 超时时间

* @throws Exception

*/

public final void setSoTimeout(int timeout) throws Exception {

ds.setSoTimeout(timeout);

}

/**

* 获得超时时间.

* @return 返回超时时间.

* @throws Exception

*/

public final int getSoTimeout() throws Exception {

return ds.getSoTimeout();

}

/**

* 绑定监听地址和端口.

* @param host 主机IP

* @param port 端口

* @throws SocketException

*/

public final void bind(String host, int port) throws SocketException {

socketAddress = new InetSocketAddress(host, port);

ds = new DatagramSocket(socketAddress);

}

/**

* 接收数据包,该方法会造成线程阻塞.

* @return 返回接收的数据串信息

* @throws IOException

*/

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("server info:" + info);

return info;

}

/**

* 将响应包发送给请求端.

* @param bytes 回应报文

* @throws IOException

*/

public final void response(String info) throws IOException {

System.out.println("client IP:" + packet.getAddress().getHostAddress()

+ ",port:" + packet.getPort());

DatagramPacket dp = new DatagramPacket(buffer, buffer.length, packet

.getAddress(), packet.getPort());

dp.setData(info.getBytes());

ds.send(dp);

}

/**

* 设置报文的缓冲长度.

* @param bufsize 缓冲长度

*/

public final void setLength(int bufsize) {

packet.setLength(bufsize);

}

/**

* 获得发送回应的IP地址.

* @return 返回回应的IP地址

*/

public final InetAddress getResponseAddress() {

return packet.getAddress();

}

/**

* 获得回应的主机的端口.

* @return 返回回应的主机的端口.

*/

public final int getResponsePort() {

return packet.getPort();

}

/**

* 关闭udp监听口.

*/

public final void close() {

try {

ds.close();

} catch (Exception ex) {

ex.printStackTrace();

}

}

/**

* 测试方法.

* @param args

* @throws Exception

*/

public static void main(String[] args) throws Exception {

String serverHost = "服务器ip";

int serverPort = 7005;

UdpServer udpServerSocket = new UdpServer(serverHost, serverPort);

while (true) {

udpServerSocket.receive();

udpServerSocket.response("return num:"+new Random().nextInt(1000));

}

}

}

//客户端

package udp;

import java.io.*;

import java.net.*;

import java.util.Random;

/**

* Copyright 2007 GuangZhou Cotel Co. Ltd.

* All right reserved.

* UDP客户端程序,用于对服务端发送数据,并接收服务端的回应信息.

* @author QPING

*/

public class UdpClient {

private byte[] buffer = new byte[1024];

private DatagramSocket ds = null;

/**

* 构造函数,创建UDP客户端

* @throws Exception

*/

public UdpClient() throws Exception {

ds = new DatagramSocket();

}

/**

* 设置超时时间,该方法必须在bind方法之后使用.

* @param timeout 超时时间

* @throws Exception

*/

public final void setSoTimeout(final int timeout) throws Exception {

ds.setSoTimeout(timeout);

}

/**

* 获得超时时间.

* @return 返回超时时间

* @throws Exception

*/

public final int getSoTimeout() throws Exception {

return ds.getSoTimeout();

}

public final DatagramSocket getSocket() {

return ds;

}

/**

* 向指定的服务端发送数据信息.

* @param host 服务器主机地址

* @param port 服务端端口

* @param bytes 发送的数据信息

* @return 返回构造后俄数据报

* @throws IOException

*/

public final DatagramPacket send(final String host, final int port,

final byte[] bytes) throws IOException {

DatagramPacket dp = new DatagramPacket(bytes, bytes.length, InetAddress

.getByName(host), port);

ds.send(dp);

return dp;

}

/**

* 接收从指定的服务端发回的数据.

* @param lhost 服务端主机

* @param lport 服务端端口

* @return 返回从指定的服务端发回的数据.

* @throws Exception

*/

public final String receive(final String lhost, final int lport)

throws Exception {

DatagramPacket dp = new DatagramPacket(buffer, buffer.length);

ds.receive(dp);

String info = new String(dp.getData(), 0, dp.getLength());

return info;

}

/**

* 关闭udp连接.

*/

public final void close() {

try {

ds.close();

} catch (Exception ex) {

ex.printStackTrace();

}

}

/**

* 测试客户端发包和接收回应信息的方法.

* @param args

* @throws Exception

*/

public static void main(String[] args) throws Exception {

UdpClient client = new UdpClient();

String serverHost = "ip地址"; //需要发送的服务器ip

int serverPort = 7005; //端口

client.send(serverHost, serverPort, ("send message "+new Random().nextDouble()).getBytes());

String info = client.receive(serverHost, serverPort);

System.out.println(info);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: