本文主要是讲Java的UDP服务端接收客户端消息的实现过程
package com.Demo;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class MyUdpServer {
int port = 0;
DatagramSocket server = null;
DatagramPacket packet = null;
byte[] b =null;
public MyUdpServer(int port){
super();
this.port = port;
try {
server = new DatagramSocket(port);
System.out.println("UDP服务端已启动,正在监听端口"+port);
} catch (SocketException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public void receiveData(){
b = new byte[1024];
int len=0;
packet = new DatagramPacket(b,b.length);
try {
while (true){
server.receive(packet);
if ( (len=packet.getLength()) >0 ){
String msg = new String(packet.getData(),0,len);
InetAddress ip = packet.getAddress();
System.out.println("来自主机"+ip + "的消息:" + msg);
}
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
package com.Demo;
import java.util.Scanner;
public class UDPServerDemo {
public static void main(String[] args) {
// TODO 自动生成的方法存根
MyUdpServer server = new MyUdpServer(8080);
Thread t = new Thread(new Runnable(){
@Override
public void run() {
// TODO 自动生成的方法存根
server.receiveData();
}
});
t.start();
}
}