UDP服务器
public class UdpServer implements Runnable {
private static UdpServer utils = null;
private String ip = null;
public int port = 8880;
private DatagramPacket dpRcv = null,dpSend = null;
private static DatagramSocket ds = null;
private byte[] msgRcv = new byte[1024];
private boolean isExit = false; //udp生命线程
private InetAddress cilentAddress;
private int clientPort;
private JSONObject serverJson;
public synchronized static UdpServer getIntance(){
if (utils == null){
utils = new UdpServer();
}
return utils;
}
//设置超时
private void setSoTime(int ms) throws SocketException {
if (ds != null){
ds.setSoTimeout(ms);
}
}
//更改UDP生命线程因子
public void exit(){
isExit = true;
if (ds != null){
if (!ds.isClosed()){
ds.close();
}
ds.disconnect();
ds = null;
}
}
/****
* 发送
* @param sendStr
*/
public void Send(String sendStr) {
try {
if (ds != null && dpSend != null){
dpSend = new DatagramPacket(sendStr.getBytes(),sendStr.getBytes().length,cilentAddress,clientPort);
ds.send(dpSend);
}
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void run() {
ip = FTPServerUtils.getIntance().getLocalIpAddress();
try {
InetSocketAddress inetSocketAddress = new InetSocketAddress(ip, port);
if (ds != null && !ds.isConnected()){
LogUtils.printLog("UDP服务器", "断开重连");
}else {
LogUtils.printLog("UDP服务器", "开始连接");
ds = new DatagramSocket(inetSocketAddress);
}
LogUtils.printLog("SocketInfo", "UDP服务器已经启动");
// 设置超时,不需要可以删除
// setSoTime(3000);
dpRcv = new DatagramPacket(msgRcv, msgRcv.length);
while (!isExit) {
if (ds != null && !ds.isClosed()){
ds.receive(dpRcv);
cilentAddress = dpRcv.getAddress();
clientPort = dpRcv.getPort();
String string = new String(dpRcv.getData(), dpRcv.getOffset(), dpRcv.getLength());
receiveData(string);
}
}
if (ds != null){
if (!ds.isClosed()){
ds.close();
}
ds.disconnect();
ds = null;
}
LogUtils.printLog("UDP监听关闭");
} catch (Exception e) {
e.printStackTrace();
LogUtils.printLog("UDP服务器已经启动--Exception---" + e.getMessage());
}
}
private JSONObject jsonObject = null;
/****解析接收数据****/
public void receiveData(String data){
try {
LogUtils.printLog("-----接收数据----" + data);
serverJson = new JSONObject(data);
}catch (Exception e){
e.printStackTrace();
LogUtils.printLog("解析接收数据--Exception---" + e.getMessage());
}
}
/***应答****/
public void sendReq(String type){
try {
JSONObject json = new JSONObject();
json.put("type ", "respond");
json.put("req", type);
Send(json.toString());
LogUtils.printLog("----发送应答----" + json.toString());
}catch (Exception e){
e.printStackTrace();
}
}
}
UDP客户端
public class UDPClient implements Runnable{
final static int udpPort = 8880;
final static String hostIp = "192.168.5.21";
private static DatagramSocket socket = null;
private static DatagramPacket packetSend,packetRcv;
private boolean exit= true; //udp生命线程
private byte[] msgRcv = new byte[1024]; //接收消息
public UDPClient(){
super();
}
//是否关闭
public boolean isExit(){
if (exit){
return true;
}
return false;
}
//更改UDP生命线程因子
public void setExit(boolean b){
exit= b;
}
//发送消息
public String send(String msgSend){
InetAddress hostAddress = null;
try {
hostAddress = InetAddress.getByName(hostIp);
} catch (UnknownHostException e) {
PrintlnLog.printLog("udpClient","未找到服务器");
e.printStackTrace();
}
packetSend = new DatagramPacket(msgSend.getBytes() , msgSend.getBytes().length,hostAddress,udpPort);
try {
socket.send(packetSend);
} catch (IOException e) {
e.printStackTrace();
PrintlnLog.printLog("udpClient","发送失败");
}
// socket.close();
return msgSend;
}
@Override
public void run() {
try {
socket = new DatagramSocket();
// socket.setSoTimeout(3000);//设置超时为3s
} catch (SocketException e) {
PrintlnLog.printLog("udpClient","建立接收数据报失败");
e.printStackTrace();
}
packetRcv = new DatagramPacket(msgRcv,msgRcv.length);
while (exit){
try {
PrintlnLog.printLog("udpClient", "UDP监听");
socket.receive(packetRcv);
String RcvMsg = new String(packetRcv.getData(),packetRcv.getOffset(),packetRcv.getLength());
receiveData(RcvMsg);
PrintlnLog.printLog("Rcv",RcvMsg);
}catch (IOException e){
e.printStackTrace();
}
}
PrintlnLog.printLog("udpClient","UDP监听关闭");
socket.close();
}
/****解析数据****/
public void receiveData(String data){
try {
PrintlnLog.printLog("-----收到数据-----" + data);
}catch (Exception e){
e.printStackTrace();
}
}