List of usage examples for java.net DatagramSocket close
public void close()
From source file:Main.java
public static void main(String args[]) { try {/*from w w w. java 2 s . 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 {/*ww w. j av a 2s. c o m*/ InetAddress ia = InetAddress.getByName("www.java2s.com"); DatagramSocket ds = new DatagramSocket(InetSocketAddress.createUnresolved("google.com", 8080)); 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) throws Exception { DatagramSocket ds = new DatagramSocket(3000); byte[] buf = new byte[1024]; DatagramPacket dp = new DatagramPacket(buf, 1024); ds.receive(dp);//from w w w . j av a 2 s . c o m String strRecv = new String(dp.getData(), 0, dp.getLength()) + " from " + dp.getAddress().getHostAddress() + ":" + dp.getPort(); System.out.println(strRecv); ds.close(); }
From source file:Main.java
public static void main(String args[]) { try {/*w w w . j a v a2s .com*/ 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:Main.java
public static void main(String[] args) throws Exception { int mcPort = 12345; String mcIPStr = "230.1.1.1"; DatagramSocket udpSocket = new DatagramSocket(); InetAddress mcIPAddress = InetAddress.getByName(mcIPStr); byte[] msg = "Hello".getBytes(); DatagramPacket packet = new DatagramPacket(msg, msg.length); packet.setAddress(mcIPAddress);/*from w w w . j ava 2s .c o m*/ packet.setPort(mcPort); udpSocket.send(packet); System.out.println("Sent a multicast message."); System.out.println("Exiting application"); udpSocket.close(); }
From source file:GetDate.java
public static void main(String args[]) throws Exception { byte msg[] = new byte[256]; DatagramSocket dgSocket = new DatagramSocket(); InetAddress destination = InetAddress.getByName("web.mit.edu"); DatagramPacket datagram = new DatagramPacket(msg, msg.length, destination, PortDayTime); dgSocket.send(datagram);//from ww w . ja v a2s. c o m datagram = new DatagramPacket(msg, msg.length); dgSocket.receive(datagram); String received = new String(datagram.getData()); System.out.println("The time in Cambridge is now: " + received); dgSocket.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { DatagramSocket udpSocket = new DatagramSocket(); String msg = null;/*ww w . ja v a2 s . c om*/ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Please enter a message (Bye to quit):"); while ((msg = br.readLine()) != null) { if (msg.equalsIgnoreCase("bye")) { break; } DatagramPacket packet = Main.getPacket(msg); udpSocket.send(packet); udpSocket.receive(packet); displayPacketDetails(packet); System.out.print("Please enter a message (Bye to quit):"); } udpSocket.close(); }
From source file:UdpEchoClient.java
public static void main(String[] args) { InetAddress address;/*from w w w .j a v a 2 s. co m*/ try { address = InetAddress.getByName(args[0]); } catch (UnknownHostException host) { System.out.println(host); return; } DatagramPacket pack = new DatagramPacket(testString.getBytes(), testString.length(), address, 7); DatagramPacket incoming = new DatagramPacket(new byte[256], 256); DatagramSocket sock = null; try { Calendar start, end; sock = new DatagramSocket(); start = Calendar.getInstance(); sock.send(pack); sock.setSoTimeout(5000); sock.receive(incoming); end = Calendar.getInstance(); String reply = new String(incoming.getData()); reply = reply.substring(0, testString.length()); if (reply.equals(testString)) { System.out.println("Success"); System.out.println("Time = " + (end.getTime().getTime() - start.getTime().getTime()) + "mS"); } else System.out.println("Reply data did not match"); } catch (SocketException socke) { System.out.println(socke); } catch (IOException ioe) { System.out.println(ioe); } finally { sock.close(); } }
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 w w w.ja va 2s .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: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 .ja v a 2 s.c o m 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); }