List of usage examples for java.net DatagramSocket DatagramSocket
public DatagramSocket(int port) throws SocketException
From source file:uk.co.propter.sleeponlan.SleepService.java
private void openSocket() { try {// w w w . j a v a 2s . c o m socket = new DatagramSocket(listeningPort); } catch (SocketException e) { logger.severe("Could not bind socket on port " + listeningPort); logger.fine(e.getMessage()); System.exit(1); } }
From source file:org.openehealth.ipf.commons.ihe.core.atna.UdpServer.java
@Override public void run() { byte[] buffer = new byte[BUFFER_SIZE]; try {//from w w w. ja v a 2 s.co m socket = new DatagramSocket(port); LOG.debug("UDP server started on port " + port); } catch (SocketException e) { LOG.error("cannot open datagram socket", e); return; } try { while (true) { DatagramPacket packet = new DatagramPacket(buffer, buffer.length); socket.receive(packet); packets.add(packetToString(packet)); countDown(); } } catch (SocketException e) { LOG.debug("socket closed"); } catch (IOException e) { LOG.error(e); } }
From source file:org.vesalainen.nmea.processor.SNTPServer.java
@Override public void run() { try {//from w w w.j av a 2s . c om socket = new DatagramSocket(NTP_PORT); ntpMessage = new NtpV4Impl(); DatagramPacket datagramPacket = ntpMessage.getDatagramPacket(); while (true) { socket.receive(datagramPacket); log.info("NTP: received %s from %s %d", ntpMessage, datagramPacket.getAddress(), datagramPacket.getPort()); TimeStamp transmitTimeStamp = ntpMessage.getTransmitTimeStamp(); ntpMessage.setLeapIndicator(LI_NO_WARNING); ntpMessage.setMode(MODE_SERVER); ntpMessage.setStratum(1); ntpMessage.setPrecision(-20); ntpMessage.setRootDelay(0); ntpMessage.setRootDispersion(0); ntpMessage.setReferenceId(ReferenceIdentifier.GPS); ntpMessage.setReferenceTime(TimeStamp.getNtpTime(clock.getZonedDateTime().toEpochSecond())); ntpMessage.setOriginateTimeStamp(transmitTimeStamp); long time = clock.millis(); TimeStamp timeStamp = TimeStamp.getNtpTime(time); ntpMessage.setReceiveTimeStamp(timeStamp); ntpMessage.setTransmitTime(timeStamp); socket.send(datagramPacket); log.info("NTP: sent %s to %s %d diff=%d", ntpMessage, datagramPacket.getAddress(), datagramPacket.getPort(), time - transmitTimeStamp.getTime()); } } catch (Exception ex) { log.log(Level.SEVERE, ex, ""); } }
From source file:AvailablePortFinder.java
/** * Checks to see if a specific port is available. * * @param port the port to check for availability *//*from w w w . j a va2 s. co m*/ public static boolean available(int port) { if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) { throw new IllegalArgumentException("Invalid start port: " + port); } ServerSocket ss = null; DatagramSocket ds = null; try { ss = new ServerSocket(port); ss.setReuseAddress(true); ds = new DatagramSocket(port); ds.setReuseAddress(true); return true; } catch (IOException e) { } finally { if (ds != null) { ds.close(); } if (ss != null) { try { ss.close(); } catch (IOException e) { /* should not be thrown */ } } } return false; }
From source file:com.navercorp.pinpoint.profiler.sender.NioUdpDataSenderTest.java
@Before public void setUp() throws SocketException { receiver = new DatagramSocket(PORT); receiver.setSoTimeout(1000); }
From source file:com.chiralBehaviors.slp.hive.hardtack.configuration.AggregatorConfiguration.java
@Override public Engine construct() throws IOException { NetworkInterface intf = getNetworkInterface(); DatagramSocket socket;//from www . j a va 2s.c om InetSocketAddress address; if (endpoint.getAddress().isAnyLocalAddress()) { address = new InetSocketAddress(Utils.getAddress(intf, ipv4), endpoint.getPort()); } else { address = endpoint; } try { socket = new DatagramSocket(address); } catch (BindException e) { BindException bindException = new BindException(String.format("Cannot bind to %s", address)); bindException.initCause(e); throw bindException; } return new AggregatorEngine(socket, getMac(), Executors.newSingleThreadScheduledExecutor(), getFdFactory()); }
From source file:org.proto.led.network.WiFiControllerService.java
private void listenAndWaitAndThrowIntent(InetAddress broadcastIP, Integer port) throws Exception { byte[] recvBuf = new byte[400]; if (socket == null || socket.isClosed()) { socket = new DatagramSocket(port); socket.setSoTimeout(5000);/*from w w w. j a v a 2 s. c o m*/ socket.setBroadcast(true); } DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.length); Log.i(TAG, "Waiting for UDP broadcast"); socket.receive(packet); String senderIP = packet.getAddress().getHostAddress(); parseMessage(recvBuf, senderIP); String message = new String(packet.getData()).trim(); Log.i(TAG, "Got UDP broadcast from " + senderIP + ", message: " + message); socket.close(); }
From source file:com.chiralBehaviors.slp.hive.hardtack.configuration.PushConfiguration.java
@Override public Engine construct() throws IOException { NetworkInterface intf = getNetworkInterface(); InetAddress address = Utils.getAddress(intf, ipv4); DatagramSocket p2pSocket = new DatagramSocket(new InetSocketAddress(address, Utils.allocatePort(address))); int i = 0;/*from w w w.j av a2 s . c om*/ for (InetSocketAddress aggregator : aggregators) { log.info(String.format("Adding aggregator: %s", aggregator)); if (aggregator.getAddress().isAnyLocalAddress()) { aggregators.set(i++, new InetSocketAddress(address, aggregator.getPort())); } } return new PushEngine(p2pSocket, getMac(), Generators.timeBasedGenerator(), aggregators, heartbeatPeriod, heartbeatUnit, Executors.newSingleThreadScheduledExecutor()); }
From source file:org.jopenray.client.RendererListener.java
public int getPort() throws IOException { if (port < 0) { int rcvPort = 3000; int maxPort = 4000; while (rcvPort <= maxPort) { try { socket = new DatagramSocket(rcvPort); port = rcvPort;/*from ww w . java 2 s . co m*/ return port; } catch (SocketException bind_ex) { rcvPort++; } catch (SecurityException sec_ex) { rcvPort++; } } throw new IOException("Unable to find an available port for UDP communication"); } return port; }
From source file:org.squidy.nodes.artracking.ARUDPServer.java
public ARUDPServer(ARTracking art, int port) { this.arTracking = art; try {//from w ww . j a v a 2 s . co m socket = new DatagramSocket(port); start(); LOG.info("UDP Server started on port " + port); } catch (IOException e) { LOG.error("Couldn't start udp server on port " + port); } }