List of usage examples for java.net DatagramPacket DatagramPacket
public DatagramPacket(byte buf[], int length, SocketAddress address)
From source file:MainClass.java
public static void main(String[] args) { try {/*from www . j av a 2 s.c o m*/ String data = "data in UDP"; byte[] buffer = data.getBytes(); DatagramPacket packet = new DatagramPacket(buffer, buffer.length, new InetSocketAddress("localhost", 5002)); DatagramSocket socket = new DatagramSocket(5003); System.out.println("Sending a packet..."); socket.send(packet); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String args[]) { try {/*w w w.ja v a 2 s. c o m*/ int port = 80; DatagramSocket ds = new DatagramSocket(port); while (true) { byte buffer[] = new byte[BUFSIZE]; DatagramPacket dp = new DatagramPacket(buffer, 0, buffer.length); ds.receive(dp); String str = new String(dp.getData()); System.out.println(str); } } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String args[]) { try {// www.j av a2s . co m int port = 80; DatagramSocket ds = new DatagramSocket(port); while (true) { byte buffer[] = new byte[BUFSIZE]; DatagramPacket dp = new DatagramPacket(buffer, buffer.length, InetSocketAddress.createUnresolved("google.com", 8080)); ds.receive(dp); String str = new String(dp.getData()); System.out.println(str); } } catch (Exception e) { e.printStackTrace(); } }
From source file:PacketReceiver.java
public static void main(String[] args) throws Exception { byte[] buffer = "data".getBytes(); DatagramPacket packet = new DatagramPacket(buffer, buffer.length, new InetSocketAddress("localhost", 5002)); DatagramSocket socket = new DatagramSocket(5003); socket.send(packet);//from w w w. j a va 2 s. c o m }
From source file:DaytimeClient.java
public static void main(String args[]) throws java.io.IOException { // Figure out the host and port we're going to talk to String host = args[0];/*from ww w . j a v a 2 s .c o m*/ int port = 13; if (args.length > 1) port = Integer.parseInt(args[1]); // Create a socket to use DatagramSocket socket = new DatagramSocket(); // Specify a 1-second timeout so that receive() does not block forever. socket.setSoTimeout(1000); // This buffer will hold the response. On overflow, extra bytes are // discarded: there is no possibility of a buffer overflow attack here. byte[] buffer = new byte[512]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length, new InetSocketAddress(host, port)); // Try three times before giving up for (int i = 0; i < 3; i++) { try { // Send an empty datagram to the specified host (and port) packet.setLength(0); // make the packet empty socket.send(packet); // send it out // Wait for a response (or timeout after 1 second) packet.setLength(buffer.length); // make room for the response socket.receive(packet); // wait for the response // Decode and print the response System.out.print(new String(buffer, 0, packet.getLength(), "US-ASCII")); // We were successful so break out of the retry loop break; } catch (SocketTimeoutException e) { // If the receive call timed out, print error and retry System.out.println("No response"); } } // We're done with the channel now socket.close(); }
From source file:net.tanesha.tftpd.core.OutPacket.java
private static DatagramPacket createPacket(byte[] data, SocketAddress remote) { DatagramPacket packet = new DatagramPacket(data, data.length, remote); return packet; }
From source file:com.bitbreeds.webrtc.dtls.DtlsMuxStunTransport.java
public int receive(byte[] buf, int off, int len, int waitMillis) throws IOException { socket.setSoTimeout(waitMillis);/*from www .j av a 2 s . co m*/ DatagramPacket packet = new DatagramPacket(buf, off, len); socket.receive(packet); logger.trace("Socket read msg: {}", Hex.encodeHexString(SignalUtil.copyRange(buf, new ByteRange(0, packet.getLength())))); if (buf.length >= 2 && buf[0] == 0 && buf[1] == 1) { SocketAddress currentSender = packet.getSocketAddress(); byte[] data = Arrays.copyOf(packet.getData(), packet.getLength()); byte[] out = bindingService.processBindingRequest(data, parent.getLocal().getUserName(), parent.getLocal().getPassword(), (InetSocketAddress) currentSender); logger.trace("Stun packet received, responding with {}", Hex.encodeHexString(out)); this.send(out, 0, out.length); return 0; //We do not want DTLS to process (not that it will anyway), so we return 0 here. } return packet.getLength(); }
From source file:com.clustercontrol.poller.impl.UdpTransportMappingImpl.java
public void sendMessage(UdpAddress targetAddress, byte[] message, TransportStateReference tmStateReference) throws java.io.IOException { InetSocketAddress targetSocketAddress = new InetSocketAddress(targetAddress.getInetAddress(), targetAddress.getPort());//from w w w. j a va 2 s . c o m if (logger.isDebugEnabled()) { logger.debug("Sending message to " + targetAddress + " with length " + message.length + ": " + new OctetString(message).toHexString()); } DatagramSocket s = ensureSocket(); s.send(new DatagramPacket(message, message.length, targetSocketAddress)); }
From source file:org.apache.axis2.transport.udp.UDPSender.java
@Override public void sendMessage(MessageContext msgContext, String targetEPR, OutTransportInfo outTransportInfo) throws AxisFault { UDPOutTransportInfo udpOutInfo;//from w w w .j a v a 2s . c o m if ((targetEPR == null) && (outTransportInfo != null)) { // this can happen only at the server side and send the message using back chanel udpOutInfo = (UDPOutTransportInfo) outTransportInfo; } else { udpOutInfo = new UDPOutTransportInfo(targetEPR); } MessageFormatter messageFormatter = TransportUtils.getMessageFormatter(msgContext); OMOutputFormat format = BaseUtils.getOMOutputFormat(msgContext); format.setContentType(udpOutInfo.getContentType()); byte[] payload = messageFormatter.getBytes(msgContext, format); try { DatagramSocket socket = new DatagramSocket(); if (log.isDebugEnabled()) { log.debug("Sending " + payload.length + " bytes to " + udpOutInfo.getAddress()); } try { socket.send(new DatagramPacket(payload, payload.length, udpOutInfo.getAddress())); if (!msgContext.getOptions().isUseSeparateListener() && !msgContext.isServerSide()) { waitForReply(msgContext, socket, udpOutInfo.getContentType()); } } finally { socket.close(); } } catch (IOException ex) { throw new AxisFault("Unable to send packet", ex); } }
From source file:org.springframework.integration.syslog.inbound.SyslogReceivingChannelAdapterTests.java
@Test public void testUdp() throws Exception { SyslogReceivingChannelAdapterFactoryBean factory = new SyslogReceivingChannelAdapterFactoryBean( SyslogReceivingChannelAdapterFactoryBean.Protocol.udp); int port = SocketUtils.findAvailableUdpSocket(1514); factory.setPort(port);/* w w w . j av a 2s . c o m*/ PollableChannel outputChannel = new QueueChannel(); factory.setOutputChannel(outputChannel); factory.setBeanFactory(mock(BeanFactory.class)); factory.afterPropertiesSet(); factory.start(); UdpSyslogReceivingChannelAdapter adapter = (UdpSyslogReceivingChannelAdapter) factory.getObject(); Thread.sleep(1000); byte[] buf = "<157>JUL 26 22:08:35 WEBERN TESTING[70729]: TEST SYSLOG MESSAGE".getBytes("UTF-8"); DatagramPacket packet = new DatagramPacket(buf, buf.length, new InetSocketAddress("localhost", port)); DatagramSocket socket = new DatagramSocket(); socket.send(packet); socket.close(); Message<?> message = outputChannel.receive(10000); assertNotNull(message); assertEquals("WEBERN", message.getHeaders().get("syslog_HOST")); adapter.stop(); }