Code example for DatagramSocket

Methods: closesendsetBroadcast

0
     
    public static void runUdpClient(String a)  {
	    String udpMsg = a;
	    DatagramSocket ds = null;
	    try { 
	        ds = new DatagramSocket();
	        ds.setBroadcast(true);
	         
	        InetAddress serverAddr = InetAddress.getByName("28.184.3.85");
	        // 28.168.172.220 
	        DatagramPacket dp;
	        dp = new DatagramPacket(udpMsg.getBytes(), udpMsg.length(), serverAddr, UDP_SERVER_PORT);
	        ds.send(dp);
	    } catch (SocketException e) {
	        e.printStackTrace();
	    
	    }catch (UnknownHostException e) {
	        e.printStackTrace();
	     
	    } catch (IOException e) {
	        e.printStackTrace();
	     
	    } catch (Exception e) {
	        e.printStackTrace();
	     
	    } finally { 
	        if (ds != null) {
	            ds.close();
	        } 
	    } 
	 
	} 
     
Stop searching for code, let great code find you!  Add Codota to your java IDE