List of usage examples for java.net DatagramPacket DatagramPacket
public DatagramPacket(byte buf[], int length)
From source file:org.cloudfoundry.identity.statsd.integration.UaaMetricsEmitterIT.java
@Before public void setUp() throws IOException { serverSocket = new DatagramSocket(8125); receiveData = new byte[65535]; receivePacket = new DatagramPacket(receiveData, receiveData.length); }
From source file:org.jopenray.client.RendererListener.java
public void start() { Thread t = new Thread(new Runnable() { @Override/*from w ww .j a v a2 s . c o m*/ public void run() { connectionDate = System.currentTimeMillis(); DatagramPacket data = new DatagramPacket(udpData, udpData.length); try { while (true) { System.out.println("Listening on port:" + port); socket.receive(data); dataLength = data.getLength(); handlePacket(); } } catch (IOException e) { e.printStackTrace(); } } }); t.start(); }
From source file:network.UDPServer.java
@Override public void run() { try {//from ww w . j av a 2s . c om while (!isStopped()) { byte[] receiveData = new byte[MAX_PACKET_SIZE]; final DatagramPacket receivedPacket = new DatagramPacket(receiveData, receiveData.length); socket.receive(receivedPacket); final UDPServer server = this; //System.out.println("Message received!"); executorService.execute(new Runnable() { public void run() { try { serverHandler.handle(server, receivedPacket); } catch (Exception e) { e.printStackTrace(); } } }); } } catch (Exception e) { e.printStackTrace(); } }
From source file:org.jopenray.server.thinclient.DisplayReaderThread.java
@Override public void run() { byte[] buf = new byte[1500]; DatagramPacket packet = new DatagramPacket(buf, buf.length); try {// ww w . j a va2 s . co m while (!this.isInterrupted()) { client.getSocket().receive(packet); handlePacket(buf, packet.getLength()); } } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }
From source file:be.error.rpi.knx.UdpChannel.java
@Override public void run() { String s = null;/* w w w . j a va2 s. c om*/ try { logger.debug("UdpChannel on port " + clientSocket.getPort() + " started."); while (true) { byte b[] = new byte[256]; DatagramPacket receivePacket = new DatagramPacket(b, b.length); clientSocket.receive(receivePacket); s = new String(b, "UTF8").trim(); logger.debug("UdpChannel received " + s); String[] split = StringUtils.split(s, "|"); UdpChannelCommand udpChannelCommand = fromString(split[0]); synchronized (udpChannelCallbacks) { this.udpChannelCallbacks.stream().filter(cb -> cb.command() == udpChannelCommand) .forEach(cb -> { try { cb.callBack(StringUtils.join(remove(split, 0))); } catch (Exception e) { logger.error("UdpChannel on port " + clientSocket.getPort() + " with command " + cb.command().toString(), e); } }); } } } catch (Exception e) { logger.error("UdpChannel on port " + clientSocket.getPort() + " with raw command " + s, e); } }
From source file:MainClass.java
public void run() { byte[] buffer = new byte[65507]; while (true) { if (stopped) return; DatagramPacket dp = new DatagramPacket(buffer, buffer.length); try {/*from ww w. j a v a 2s. co m*/ socket.receive(dp); String s = new String(dp.getData(), 0, dp.getLength()); System.out.println(s); Thread.yield(); } catch (IOException ex) { System.err.println(ex); } } }
From source file:org.rifidi.emulator.io.comm.ip.udp.UDPCommunicationIncomingMessageHandler.java
/** * The main logic of the monitor. Reads in data from the client socket until * the client socket is closed/disconnected. * // www . j a v a2s . c o m * @see java.lang.Runnable#run() */ public void run() { /* Create a Datagram packet to hold the recieved message */ DatagramPacket pack = new DatagramPacket(new byte[PACKET_SIZE], PACKET_SIZE); /* Should the loop keep running? */ boolean keepRunning = true; /* This part loops until we catch an exception */ while (keepRunning) { /* receive messages on the socket */ try { System.out.println("Attempting to recieve a packet"); newSock.receive(pack); System.out.println("RECIEVED A PACKET OMG"); } catch (IOException e) { e.printStackTrace(); logger.warn(e.getMessage()); } /* put messages into the buffer */ if (pack.getData() != null) { try { // List<byte[]> listOfBytes = this.host.getProtocol() // .removeProtocol(pack.getData()); // for (byte[] b : listOfBytes) { this.host.getReceiveBuffer().addToBuffer(pack.getData()); this.host.setRemoteIPAddress(pack.getAddress().getHostAddress()); this.host.setRemotePort(pack.getPort()); // } } catch (DataBufferInterruptedException e) { /* Thrown because socket was interrupted */ logger.warn(e.getMessage()); keepRunning = false; } // catch (ProtocolValidationException e) { // /* Thrown because of a problem with the protocol */ // logger.warn(e.getMessage()); // } } } }
From source file:org.starnub.starnubserver.servers.starbound.UDPProxyServer.java
public void run() { InetAddress IPAddress;// w w w. j av a 2 s.c o m DatagramSocket ds; try { ds = new DatagramSocket(starnubPort); IPAddress = InetAddress.getByName(starboundAddress); } catch (SocketException | UnknownHostException e1) { StarNub.getLogger().cFatPrint("StarNub", ExceptionUtils.getMessage(e1)); e1.printStackTrace(); return; } byte[] request = new byte[1024]; byte[] reply = new byte[4096]; while (!stopping) { try { DatagramPacket from_client = new DatagramPacket(request, request.length); ds.receive(from_client); byte[] real_request = new byte[from_client.getLength()]; System.arraycopy(request, 0, real_request, 0, from_client.getLength()); DatagramPacket sendPacket = new DatagramPacket(real_request, real_request.length, IPAddress, starboundPort); ds.send(sendPacket); DatagramPacket from_server = new DatagramPacket(reply, reply.length); ds.receive(from_server); byte[] real_reply = new byte[from_server.getLength()]; System.arraycopy(reply, 0, real_reply, 0, from_server.getLength()); InetAddress address = from_client.getAddress(); int port = from_client.getPort(); DatagramPacket to_client = new DatagramPacket(real_reply, real_reply.length, address, port); ds.send(to_client); } catch (Exception e) { StarNub.getLogger().cFatPrint("StarNub", ExceptionUtils.getMessage(e)); return; } } }
From source file:gravity.android.discovery.DiscoveryServer.java
public void run() { Log.v("DISCOVERY_SERVER", "SERVER STARTED"); DatagramSocket serverSocket = null; try {/*ww w. j av a 2s.co m*/ serverSocket = new DatagramSocket(port); byte[] receiveData; byte[] sendData; while (this.isInterrupted() == false) { receiveData = new byte[128]; sendData = new byte[128]; try { Log.v("DISCOVERY_SERVER", "LISTENING"); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); serverSocket.receive(receivePacket); String sentence = new String(receivePacket.getData()); if (sentence != null) Log.v("DISCOVERY_SERVER", "RECEIVED: " + sentence.substring(0, receivePacket.getLength()).trim()); if (sentence != null && sentence.substring(0, receivePacket.getLength()).trim().equals(token)) { Log.v("DISCOVERY_SERVER", "SEND '" + nome + "' to " + receivePacket.getAddress().getHostAddress() + ":" + receivePacket.getPort()); JSONObject sendDataJson = new JSONObject(); sendDataJson.accumulate("name", nome); sendDataJson.accumulate("port_to_share", port_to_share); //sendData = (nome + "," + port_to_share).getBytes(); sendData = sendDataJson.toString().getBytes(); //Prakash: converts the data to json objects to avoid troubles DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, receivePacket.getAddress(), receivePacket.getPort()); serverSocket.send(sendPacket); } } catch (Exception ex) { ex.printStackTrace(); Log.e("DISCOVERY_SERVER", ex.toString()); } } } catch (Exception e) { e.printStackTrace(); Log.e("DISCOVERY_SERVER", e.toString()); } finally { try { if (serverSocket != null) serverSocket.close(); } catch (Exception ex) { } } }
From source file:org.springframework.integration.ip.udp.DatagramPacketMulticastSendingHandlerTests.java
@Test public void verifySendMulticast() throws Exception { MulticastSocket socket;/*from ww w . jav a 2s . c o m*/ try { socket = new MulticastSocket(); } catch (Exception e) { return; } final int testPort = socket.getLocalPort(); final String multicastAddress = this.multicastRule.getGroup(); final String payload = "foo"; final CountDownLatch listening = new CountDownLatch(2); final CountDownLatch received = new CountDownLatch(2); Runnable catcher = () -> { try { byte[] buffer = new byte[8]; DatagramPacket receivedPacket = new DatagramPacket(buffer, buffer.length); MulticastSocket socket1 = new MulticastSocket(testPort); socket1.setInterface(InetAddress.getByName(multicastRule.getNic())); InetAddress group = InetAddress.getByName(multicastAddress); socket1.joinGroup(group); listening.countDown(); LogFactory.getLog(getClass()).debug(Thread.currentThread().getName() + " waiting for packet"); socket1.receive(receivedPacket); socket1.close(); byte[] src = receivedPacket.getData(); int length = receivedPacket.getLength(); int offset = receivedPacket.getOffset(); byte[] dest = new byte[length]; System.arraycopy(src, offset, dest, 0, length); assertEquals(payload, new String(dest)); LogFactory.getLog(getClass()).debug(Thread.currentThread().getName() + " received packet"); received.countDown(); } catch (Exception e) { listening.countDown(); e.printStackTrace(); } }; Executor executor = Executors.newFixedThreadPool(2); executor.execute(catcher); executor.execute(catcher); assertTrue(listening.await(10000, TimeUnit.MILLISECONDS)); MulticastSendingMessageHandler handler = new MulticastSendingMessageHandler(multicastAddress, testPort); handler.setBeanFactory(mock(BeanFactory.class)); handler.setLocalAddress(this.multicastRule.getNic()); handler.afterPropertiesSet(); handler.handleMessage(MessageBuilder.withPayload(payload).build()); assertTrue(received.await(10000, TimeUnit.MILLISECONDS)); handler.stop(); socket.close(); }