java实现udp通讯的代码

需求:应用A(通常有多个)和应用B(1个)进行 socket通讯,应用A必须知道应用B的ip地址(在应用A的配置文件中写死的),这个时候就必须把应用B的ip设成固定ip(但是某些时候如更换路由后要重新设置网络,但是操作人员不知道这个规则),就有可能造成应用A和应用B无法进行正常通讯,所以要改成应用A动态获取应用B的ip地址。

经过讨论决定采用udp协议实现,upd是一种无连接的传输层协议。应用A在不知道应用B的 ip情况下 可以使用广播地址255.255.255.255,将消息发送到在同一广播网络上的B。从而获取B的ip。

实现代码:

B应用为服务端:将udp监听放到一个线程中,当有客户端请求时就会进行响应

/**

* udp连接,用于动态ip, pos向255.255.255.255:5060发送请求即可

* **/

public class UdpServer extends Thread implements Runnable {

private final int MAX_LENGTH = 1024;

private final int PORT = 5060;

private DatagramSocket datagramSocket;

public void run() {

try {

init();

while(true){

try {

byte[] buffer = new byte[MAX_LENGTH];

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

receive(packet);

String receStr = new String(packet.getData(), 0 , packet.getLength());

System.out.println("接收数据包" + receStr);

byte[] bt = new byte[packet.getLength()];

System.arraycopy(packet.getData(), 0, bt, 0, packet.getLength());

System.out.println(packet.getAddress().getHostAddress() + ":" + packet.getPort() + ":" + Arrays.toString(bt));

packet.setData(bt);

response(packet);

} catch (Exception e) {

e.printStackTrace();

LoggerUtil.error("udp线程出现异常:" + e.toString());

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

public void receive(DatagramPacket packet) throws Exception {

datagramSocket.receive(packet);

}

public void response(DatagramPacket packet) throws Exception {

datagramSocket.send(packet);

}

/**

* 初始化连接

*/

public void init(){

try {

datagramSocket = new DatagramSocket(PORT);

System.out.println("udp服务端已经启动!");

} catch (Exception e) {

datagramSocket = null;

System.out.println("udp服务端启动失败!");

e.printStackTrace();

}

}

}

客户端:本来客户端是使用pb来实现的,但是这里使用java来模拟

/***

* UDP Client端

***/

public class UdpClient {

private String sendStr = "hello";

private String netAddress = "255.255.255.255";

private final int PORT = 5060;

private DatagramSocket datagramSocket;

private DatagramPacket datagramPacket;

public UdpClient(){

try {

datagramSocket = new DatagramSocket();

byte[] buf = sendStr.getBytes();

InetAddress address = InetAddress.getByName(netAddress);

datagramPacket = new DatagramPacket(buf, buf.length, address, PORT);

datagramSocket.send(datagramPacket);

byte[] receBuf = new byte[1024];

DatagramPacket recePacket = new DatagramPacket(receBuf, receBuf.length);

datagramSocket.receive(recePacket);

String receStr = new String(recePacket.getData(), 0 , recePacket.getLength());

       //获取服务端ip

String serverIp = recePacket.getAdress();

} catch (SocketException e) {

e.printStackTrace();

} catch (UnknownHostException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

// 关闭socket

if(datagramSocket != null){

datagramSocket.close();

}

}

}

public static void main(String[] args) {

for (int i = 0; i < 100; i++) {

new Thread(new Runnable() {

@Override

public void run() {

UdpClient udpClient = new UdpClient();

}

}).start();

}

}

}

以上是 java实现udp通讯的代码 的全部内容, 来源链接: utcz.com/z/324145.html

回到顶部