11 public class UDP implements Runnable{
\r
12 private DatagramSocket $udpSock = null;
\r
13 private String $server = null;
\r
14 private int $sport = 0;
\r
16 public UDP(int $port, String $saddr, int $sport)throws Exception{
\r
17 $udpSock = new DatagramSocket($port);
\r
18 $udpSock.setReceiveBufferSize(1024*1024);
\r
20 this.$sport = $sport;
\r
21 new Thread(this).start();
\r
25 while($udpSock != null){
\r
30 public void receive(){
\r
32 DatagramPacket $packet = new DatagramPacket(new byte[1400],1400);
\r
33 $udpSock.receive($packet);
\r
34 System.out.println(new String($packet.getData()).trim());
\r
35 }catch(Exception $e){$e.printStackTrace();}
\r
38 public void send(String $msg){
\r
40 DatagramPacket $packet = new DatagramPacket($msg.getBytes(),$msg.length(),InetAddress.getByName($server),$sport);
\r
41 $udpSock.send($packet);
\r
42 }catch(Exception $e){$e.printStackTrace();}
\r
45 public void shutdown(){
\r
48 }catch(Exception $e){$e.printStackTrace();}
\r