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
17 public UDP(String $saddr, int $sport)throws Exception{
\r
18 System.out.println("CLIENT UDP -> "+$saddr+":"+$sport);
\r
19 $udpSock = new DatagramSocket();
\r
20 $udpSock.setReceiveBufferSize(1024*1024);
\r
22 this.$sport = $sport;
\r
23 new Thread(this).start();
\r
27 while($udpSock != null){
\r
32 public void receive(){
\r
34 DatagramPacket $packet = new DatagramPacket(new byte[1400],1400);
\r
35 $udpSock.receive($packet);
\r
36 System.out.println(new String($packet.getData()).trim());
\r
37 }catch(Exception $e){$e.printStackTrace();}
\r
40 public void send(String $msg){
\r
42 DatagramPacket $packet = new DatagramPacket($msg.getBytes(),$msg.length(),InetAddress.getByName($server),$sport);
\r
43 $udpSock.send($packet);
\r
44 }catch(Exception $e){$e.printStackTrace();}
\r
47 public void shutdown(){
\r
50 }catch(Exception $e){$e.printStackTrace();}
\r