public class UDPServerThread extends Thread {
DatagramPacket datagrampackets = null;
// int superport=1024;
public UDPServerThread(DatagramPacket packetd) {
this.datagrampackets = packetd;
Log.info(“UDPServerThread”);
Log.info(“datagrampacket” + datagrampackets.getPort());
}
@Override
public void run() {
Log.info(“ten”);
int ten = datagrampackets.getLength();
Log.info(“ten” + ten);
if (ten > 0) {
String msg = new String(datagrampackets.getData(), 0,datagrampackets.getLength());
InetAddress ip = datagrampackets.getAddress();
int ports = datagrampackets.getPort();
Log.info(“客户端” + ip + ports + “发来消息:” + msg);
Document document = null;
try {
document = DocumentHelper.parseText(msg);
} catch (DocumentException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}// 转换document对象
Element root = document.getRootElement();
//返回客户端消息
DatagramSocket sockets=null;
try {
sockets = new DatagramSocket();
byte[] buf = null;
buf = respxml.getBytes();
DatagramPacket packets = new DatagramPacket(buf, buf.length,
new InetSocketAddress(ip, ports));
sockets.send(packets);
sockets.close();
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
今天的文章udp server java_java UDP服务端分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/25808.html