//package serverUDP;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayDeque;
import java.util.Date;
import java.util.Deque;
import java.util.concurrent.locks.ReentrantLock;
public class serverThread {
//建立接收线程
public class ReceivingThread extends Thread
{
public ReceivingThread()
{
super("ReceivingThread");
}
public void run()
{
while(true)
{
try {
if(!handleReceiving())
{
try
{
Thread.sleep(10);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
if(!moduleRunFlag)
break;
}
}
}
//建立发送线程
public class SendingThread extends Thread
{
public SendingThread()
{
//分配新的线程对象,新线程名为SendingThread
super("SendingThread");
}
public void run()
{
while(true)
{
try {
if(!handleSending())
{
try
{
Thread.sleep(10);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
if(!moduleRunFlag)
break;
}
}
}
protected Deque<DatagramPacket> messageQueue = null; //双向线性容器
private ReentrantLock queueLock; //可重入的互斥锁
protected ReceivingThread receiveThread = null; //收线程
protected SendingThread sendThread = null; //发线程
protected boolean moduleRunFlag; //module运行标识
byte[] sendData = null;
byte[] reciveData = null;
DatagramSocket serverSocket = null;
InetAddress IPAddress = null;
int Port;
DatagramPacket sendPacket = null;
DatagramPacket recivePacket = null;
public serverThread() throws UnknownHostException, SocketException
{
messageQueue = new ArrayDeque<DatagramPacket>();
queueLock = new ReentrantLock();
receiveThread = new ReceivingThread();
sendThread = new SendingThread();
moduleRunFlag = false;
serverSocket = new DatagramSocket(9000);
}
//开始
public void startModule()
{
moduleRunFlag = true;
receiveThread.start();
sendThread.start();
}
/* //停止(是否是线程同步?)
public void stopModule(){
moduleRunFlag = false;
while(receiveThread.isAlive()){
try{
Thread.sleep(50);
}catch(InterruptedException e){
e.printStackTrace();
}
}
while(sendThread.isAlive()){
try{
Thread.sleep(50);
}catch(InterruptedException e){
e.printStackTrace();
}
}
serverSocket.close();
}
*/
//客户端发送数据
private boolean handleSending() throws IOException {
boolean bHandle = false;
DatagramPacket receivedPacket = null;
String resultStr =null;
queueLock.lock();
if(messageQueue.size() > 0)
{
receivedPacket = messageQueue.removeLast();
//4.对客户端的请求给出响应:a.由收到的客户端的数据包解析其中的IP地址并作为发送数据包的发送地址
IPAddress = receivedPacket.getAddress();
//b.由收到的客户端的数据包解析其中的Port端口号并作为发送数据包的的到达端口
Port = receivedPacket.getPort();
resultStr = "服务器返回数据" + (new Date()).toString();
}
queueLock.unlock();
if(resultStr != null)
{
System.out.println("Sending message = " + resultStr);
sendData = resultStr.trim().getBytes("GBK");
//发送数据包到服务器端:a.构造数据包
sendPacket = new DatagramPacket(sendData, sendData.length,IPAddress,Port);
//b.发送数据包
serverSocket.send(sendPacket);
bHandle = true;
}
return bHandle;
}
//客户端接收数据
private boolean handleReceiving() throws IOException
{
boolean bHandle = false;
//2.服务器首先被动地等待客户端的访问数据包
reciveData = new byte[1024];
recivePacket = new DatagramPacket(reciveData, reciveData.length);
serverSocket.receive(recivePacket);
queueLock.lock();
messageQueue.addFirst(recivePacket);
queueLock.unlock();
//3.获取响应数据包中的数据
byte[] resultData = new byte[1024];
resultData = recivePacket.getData();
String resultStr = new String(resultData,"GBK").trim();
if(resultStr != null)
{
System.out.println("receiving message = " + resultStr);
bHandle = true;
}
return bHandle;
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
serverThread test = new serverThread();
test.startModule();
while (true)
{
try
{
Thread.sleep(1000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
//test.stopModule();
}
}