List of usage examples for java.net DatagramSocket DatagramSocket
public DatagramSocket(int port, InetAddress laddr) throws SocketException
From source file:Main.java
public static void main(String[] args) throws Exception { final int LOCAL_PORT = 12345; final String SERVER_NAME = "localhost"; DatagramSocket udpSocket = new DatagramSocket(LOCAL_PORT, InetAddress.getByName(SERVER_NAME)); System.out.println("Created UDP server socket at " + udpSocket.getLocalSocketAddress() + "..."); while (true) { System.out.println("Waiting for a UDP packet..."); DatagramPacket packet = new DatagramPacket(new byte[1024], 1024); udpSocket.receive(packet);//from w w w . java 2 s . c o m displayPacketDetails(packet); udpSocket.send(packet); } }
From source file:Main.java
public static void main(String args[]) { try {//from w ww. j a v a 2s . c o m InetAddress ia = InetAddress.getByName("www.java2s.com"); DatagramSocket ds = new DatagramSocket(8080, ia); byte buffer[] = "hello".getBytes(); DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80); ds.connect(InetSocketAddress.createUnresolved("google.com", 8080)); ds.send(dp); ds.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void main(String args[]) { try {/* w w w . jav a2 s . co m*/ InetAddress ia = InetAddress.getByName("www.java2s.com"); DatagramSocket ds = new DatagramSocket(8080, ia); byte buffer[] = "hello".getBytes(); DatagramPacket dp = new DatagramPacket(buffer, buffer.length, ia, 80); ds.connect(InetSocketAddress.createUnresolved("google.com", 8080)); DatagramChannel channel = ds.getChannel(); ds.send(dp); ds.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:SDRecord.java
public static void main(String[] args) { boolean recordToInf = false; long recordTo = 0, txsize = 0, wr = 0, max = 0; int sourcePort = 0, destPort = 0; String val; OutputStream writer = null;// ww w .jav a2 s .c om InetAddress rhost = null, lhost = null; DatagramSocket socket = null; //Default values int buffSize = 1500; try { lhost = InetAddress.getByName("0.0.0.0"); } catch (UnknownHostException e1) { System.err.println("ERROR!: Host not reconized"); System.exit(3); } recordToInf = true; sourcePort = 7355; Options options = new Options(); options.addOption("m", true, "Minutes to record, default is no limit"); options.addOption("l", true, "Bind to a specific local address, default is 0.0.0.0"); options.addOption("p", true, "Local port to use, default is 7355"); options.addOption("r", true, "Remote address where to send data"); options.addOption("d", true, "Remote port, to use with -r option"); options.addOption("f", true, "Output file where to save the recording"); options.addOption("s", true, "Stop recording when reaching specified MBs"); options.addOption("h", false, "Help"); CommandLineParser parser = new DefaultParser(); CommandLine cmd = null; try { cmd = parser.parse(options, args); } catch (ParseException e1) { System.err.println("ERROR!: Error while parsing the command line"); System.exit(1); } if (cmd.hasOption("m")) { val = cmd.getOptionValue("m"); try { if (Long.parseLong(val) < 0) { System.err.println("ERROR!: -m argument value cannot be negative"); System.exit(3); } recordTo = System.currentTimeMillis() + (Long.parseLong(val) * 60000); recordToInf = false; } catch (NumberFormatException e) { System.err.println("ERROR!: -m argument not an integer"); System.exit(3); } } if (cmd.hasOption("l")) { val = cmd.getOptionValue("l"); try { lhost = InetAddress.getByName(val); } catch (UnknownHostException e) { System.err.println("ERROR!: Host not reconized"); System.exit(3); } } if (cmd.hasOption("p")) { val = cmd.getOptionValue("p"); try { sourcePort = Integer.parseInt(val); } catch (NumberFormatException e) { System.err.println("ERROR!: -p argument not an integer"); System.exit(3); } } if (cmd.hasOption("r")) { val = cmd.getOptionValue("r"); try { rhost = InetAddress.getByName(val); } catch (UnknownHostException e) { System.err.println("ERROR!: Host not reconized"); System.exit(3); } } if (cmd.hasOption("d")) { val = cmd.getOptionValue("d"); try { destPort = Integer.parseInt(val); } catch (NumberFormatException e) { System.err.println("-ERROR!: -d argument not an integer"); System.exit(3); } } if (cmd.hasOption("f")) { val = cmd.getOptionValue("f"); try { writer = new FileOutputStream(val); } catch (FileNotFoundException e) { System.err.println("ERROR!: File not found"); System.exit(3); } } if (cmd.hasOption("s")) { val = cmd.getOptionValue("s"); try { max = (long) (Double.parseDouble(val) * 1000000); } catch (NumberFormatException e) { System.err.println("ERROR!: -s argument not valid"); System.exit(3); } if (Double.parseDouble(val) < 0) { System.err.println("ERROR!: -s argument value cannot be negative"); System.exit(3); } } if (cmd.hasOption("h")) { HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("SDRecord", options); System.exit(0); } try { socket = new DatagramSocket(sourcePort, lhost); //socket options socket.setReuseAddress(true); } catch (SocketException e) { e.printStackTrace(); System.exit(3); } byte[] buffer = new byte[buffSize]; DatagramPacket packet = new DatagramPacket(buffer, buffer.length); System.err.println("Listening " + lhost.toString() + " on port " + sourcePort); while (recordToInf == true || System.currentTimeMillis() <= recordTo) { //Stop recording when reaching max bytes if (max != 0 && txsize >= max) break; packet.setData(buffer); try { socket.receive(packet); } catch (IOException e) { e.printStackTrace(); System.exit(4); } //Ignoring packets with no data if (basicFilter(packet) == null) continue; if (writer == null && rhost == null) wr = recordToStdout(packet); if (writer != null) wr = recordToFile(packet, writer); if (rhost != null) wr = recordToSocket(packet, socket, rhost, destPort); txsize += wr; System.err .print("\r" + formatSize(txsize) + " transferred" + "\033[K" + "\t Press Ctrl+c to terminate"); } //closing socket and exit System.err.print("\r" + formatSize(txsize) + " transferred" + "\033[K"); socket.close(); System.out.println(); System.exit(0); }
From source file:be.error.rpi.knx.UdpChannel.java
public UdpChannel(int port, UdpChannelCallback... udpChannelCallbacks) throws Exception { addUdpChannelCallback(udpChannelCallbacks); clientSocket = new DatagramSocket(port, getByName(LOCAL_IP)); }
From source file:org.eredlab.g4.ccl.net.DefaultDatagramSocketFactory.java
/*** * Creates a DatagramSocket at the specified address on the local host * at a specified port.//from w w w. j a v a 2 s. co m * <p> * @param port The port to use for the socket. * @param laddr The local address to use. * @exception SocketException If the socket could not be created. ***/ public DatagramSocket createDatagramSocket(int port, InetAddress laddr) throws SocketException { return new DatagramSocket(port, laddr); }
From source file:org.openhab.binding.hexabus.internal.HexaBusBinding.java
public void activate() { logger.debug("activate() is called."); // checks if the jackdaw_sock isn't created yet if (jackdaw_sock == null) { try {// w w w.ja v a2 s . c o m jackdaw_sock = new DatagramSocket(jackdaw_port, jackdaw_ip); } catch (SocketException e) { e.printStackTrace(); logger.debug("Could not create Jackdaw Socket in activate()!"); } } }
From source file:com.clustercontrol.systemlog.util.SyslogReceiver.java
public synchronized void start() throws SocketException, UnknownHostException { log.info(String.format("starting SyslogReceiver. [address = %s, port = %s, charset = %s, handler = %s]", listenAddress, listenPort, charset.name(), _handler.getClass().getName())); // ?????????handler, receiver, socket????? // ???Hinemos???Listen??? if (!HinemosManagerMain._isClustered) { socket = new DatagramSocket(listenPort, InetAddress.getByName(listenAddress)); socket.setReceiveBufferSize(socketBufferSize); socket.setSoTimeout(socketTimeout); }//from w w w .j av a 2s . c om _executor = Executors.newSingleThreadExecutor(new ThreadFactory() { @Override public Thread newThread(Runnable r) { return new Thread(r, "SystemLogReceiver"); } }); _handler.start(); if (!HinemosManagerMain._isClustered) { _executor.submit(new ReceiverTask(socket, _handler)); } }
From source file:org.openhealthtools.openatna.net.UdpServerConnection.java
public void connect() { int port = description.getPort(); String addr = description.getHostname(); try {//w w w. j a va 2 s . co m socket = new DatagramSocket(port, InetAddress.getByName(addr)); } catch (SocketException e) { log.error("UDP socket Connection Error", e); } catch (UnknownHostException e) { log.error("Unknown host for UDP socket", e); } }
From source file:org.mule.transport.udp.UdpSocketFactory.java
protected DatagramSocket createSocket(int port, InetAddress inetAddress) throws IOException { return new DatagramSocket(port, inetAddress); }