1 //header of class--------------------------------------------------------------- 2 import java.*; 3 import java.net.*; 4 5 abstract public class UDPService extends Service{ 6 private boolean stopservice; 7 8 protected InetAddress dpi; 9 protected DatagramPacket dp; 10 11 abstract protected DatagramPacket react(); 12 public final static int BUILD=31; 13 //run.zdys---------------------------------------------------------------------- 14 public void run(){ 15 try{ 16 init(); 17 int port= Integer.parseInt(arg[1]); 18 DatagramSocket ds; 19 if(arg[2].equals("-")){ 20 ds= new DatagramSocket(port); 21 }else{ 22 ds= new DatagramSocket(port, InetAddress.getByName(arg[2])); 23 } 24 Utils.pr(this, "starting UDPService "+ arg[0]+ " on " 25 + ds.getLocalAddress().getHostAddress()+ ":"+ ds.getLocalPort()); 26 ds.setSoTimeout(TIMEOUT); 27 int buffersize= Integer.parseInt(arg[3]); 28 dp= new DatagramPacket(new byte[buffersize], 0, 0); 29 while(true){ 30 if(dp.getData().length!= buffersize){ 31 dp.setData(new byte[buffersize]); 32 } 33 dp.setLength(buffersize); 34 try{ 35 ds.receive(dp); 36 }catch(java.io.InterruptedIOException ex2){ 37 if(stopservice){ 38 ds.close(); 39 Utils.pr(this, "UDPService "+ arg[0]+ " on " 40 + ds.getLocalAddress().getHostAddress()+ ":"+ ds.getLocalPort()+ " stopped"); 41 return; 42 }else{ 43 continue; 44 } 45 } 46 dpi= ds.getLocalAddress(); 47 DatagramPacket reply= react(); 48 if(reply!= null){ 49 ds.send(reply); 50 } 51 } 52 }catch(Exception ex){ 53 Utils.pr(this, "ERROR in UDPService.run:"+ ex); 54 } 55 } 56 //stopService.zdys-------------------------------------------------------------- 57 public void stopService(){ 58 stopservice= true; 59 } 60 //init.zdys--------------------------------------------------------------------- 61 public void init(){ 62 } 63 //end of class------------------------------------------------------------------ 64 }