日期:2014-05-20 浏览次数:20898 次
package com.njust.socket; import java.net.DatagramPacket; import java.net.DatagramSocket; public class ServerUDPReceiver { // 侦听的UDP端口号 private int port = -1; public static DatagramSocket socket_for_receive_udp = null; private byte[] buffer = new byte[8192]; private DatagramPacket incomming = null; private boolean isFlag = true; //private Thread serverThread = null; public ServerUDPReceiver(int i_port) { port = i_port; } public void startUDPReceiver() { try { socket_for_receive_udp = new DatagramSocket(this.port); Thread serverThread = new Thread(new Runnable() { public void run() { try { while (isFlag) { try { // 构造一个接收数据包 incomming = new DatagramPacket(buffer,buffer.length); // 接收数据报 System.out.println("before receive!"); socket_for_receive_udp.receive(incomming); int length, offset; byte[] realData = null; length = incomming.getLength(); offset = incomming.getOffset(); realData = new byte[length]; System.arraycopy(buffer, offset, realData,offset, length); System.out.println("reveive data:" + new String(realData)); } catch (Exception ee) { ee.printStackTrace(); System.out.println("In while!!!"); } } } catch (Exception e) { e.printStackTrace(); } finally { socket_for_receive_udp.close(); } } }); serverThread.start(); } catch (Exception e) { e.printStackTrace(); if (e.toString().indexOf( "java.net.BindException: Address already in use: JVM_Bind") != -1) { System.out.println("Server 已经启动,不能重复启动!"); } } } public void stopUDPReceiver(){ if(socket_for_receive_udp!=null){ //serverThread.stop(); this.isFlag = false; System.out.println("before socket close!"); socket_for_receive_udp.close(); } } }