List of usage examples for java.net DatagramSocket DatagramSocket
public DatagramSocket() throws SocketException
From source file:be.error.rpi.test.UdpTest.java
public static void main(String[] args) throws Exception { final InetAddress IPAddress = InetAddress.getByName("192.168.0.10"); final DatagramSocket clientSocket = new DatagramSocket(); new Thread() { @Override// www. j ava2 s . com public void run() { try { while (true) { String s = "0:0:0:"; DatagramPacket sendPacket = new DatagramPacket(s.getBytes(), s.getBytes().length, IPAddress, 8000); clientSocket.send(sendPacket); Thread.sleep(100); } } catch (Exception e) { } } }.start(); new Thread() { @Override public void run() { try { while (true) { String s = "1:1:1:"; DatagramPacket sendPacket = new DatagramPacket(s.getBytes(), s.getBytes().length, IPAddress, 8000); clientSocket.send(sendPacket); Thread.sleep(100); } } catch (Exception e) { } } }.start(); }
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];/*ww w.jav 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:UDPSend.java
public static void main(String args[]) { try {//from w w w.j a va 2s . co m // Check the number of arguments if (args.length < 3) throw new IllegalArgumentException("Wrong number of args"); // Parse the arguments String host = args[0]; int port = Integer.parseInt(args[1]); // Figure out the message to send. // If the third argument is -f, then send the contents of the file // specified as the fourth argument. Otherwise, concatenate the // third and all remaining arguments and send that. byte[] message; if (args[2].equals("-f")) { File f = new File(args[3]); int len = (int) f.length(); // figure out how big the file is message = new byte[len]; // create a buffer big enough FileInputStream in = new FileInputStream(f); int bytes_read = 0, n; do { // loop until we've read it all n = in.read(message, bytes_read, len - bytes_read); bytes_read += n; } while ((bytes_read < len) && (n != -1)); } else { // Otherwise, just combine all the remaining arguments. String msg = args[2]; for (int i = 3; i < args.length; i++) msg += " " + args[i]; // Convert the message to bytes using UTF-8 encoding message = msg.getBytes("UTF-8"); } // Get the internet address of the specified host InetAddress address = InetAddress.getByName(host); // Initialize a datagram packet with data and address DatagramPacket packet = new DatagramPacket(message, message.length, address, port); // Create a datagram socket, send the packet through it, close it. DatagramSocket dsocket = new DatagramSocket(); dsocket.send(packet); dsocket.close(); } catch (Exception e) { System.err.println(e); System.err.println(usage); } }
From source file:Main.java
public static void senddatagram(InetAddress target, int port, byte[] outbuf, int len) { try {/*from w w w. ja v a2s. c o m*/ DatagramPacket request = new DatagramPacket(outbuf, len, target, port); DatagramSocket socket = new DatagramSocket(); socket.send(request); } catch (SocketException e) { } catch (IOException e) { } }
From source file:Main.java
public static void sendWoLMagicPacket(final String broadcastIp, final String macAddress) { new Thread(new Runnable() { @Override/*from w ww . j a v a2 s . c o m*/ public void run() { try { byte[] macBytes = getMacBytes(macAddress); byte[] bytes = new byte[6 + 16 * macBytes.length]; for (int i = 0; i < 6; i++) { bytes[i] = (byte) 0xff; } for (int i = 6; i < bytes.length; i += macBytes.length) { System.arraycopy(macBytes, 0, bytes, i, macBytes.length); } InetAddress address = InetAddress.getByName(broadcastIp); DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, 9); DatagramSocket socket = new DatagramSocket(); socket.send(packet); socket.close(); packet = new DatagramPacket(bytes, bytes.length, address, 7); socket = new DatagramSocket(); socket.send(packet); socket.close(); Log.e("WAKE_UP", "Wake-on-LAN packet sent."); final String output = getStringFromBytes(bytes); Log.i("WAKE_UP", output); } catch (Exception e) { Log.e("WAKE_UP", "Failed to send Wake-on-LAN packet: " + e); } } }).start(); }
From source file:gravity.android.discovery.DiscoveryClient.java
public static ArrayList<DiscoveryServerInfo> findServer(WifiManager mWifi, int port, String token) { ArrayList<DiscoveryServerInfo> ret = new ArrayList<DiscoveryServerInfo>(); try {//from w ww. j av a 2s. co m DatagramSocket clientSocket = new DatagramSocket(); clientSocket.setBroadcast(true); InetAddress IPAddress = Utils.getBroadcastAddress(mWifi); Log.v("DISCOVERY_CLIENT", "broadcast addr " + IPAddress.getHostAddress()); byte[] receiveData = new byte[2048]; byte[] sendData = new byte[128]; sendData = token.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port); Log.v("DISCOVERY_CLIENT", "sent " + token); clientSocket.send(sendPacket); long t1 = System.currentTimeMillis(); while (System.currentTimeMillis() - t1 <= 4000) // 4 secondi { DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); try { clientSocket.setSoTimeout(500); clientSocket.receive(receivePacket); if (receivePacket.getAddress() != null && receivePacket.getAddress().getHostAddress() != null) { String discovered_name, discovered_ip; int discovered_port; String received = new String(receivePacket.getData()); if (received != null) { received = received.trim().substring(0, receivePacket.getLength()).trim(); Log.d("Recieved Msg Packet:", received); //StringTokenizer st = new StringTokenizer(received, ","); //Gravity Code JSONObject recievedJson = new JSONObject(received); try { //discovered_name = st.nextToken(); discovered_name = recievedJson.getString("name"); //Gravity code discovered_ip = receivePacket.getAddress().getHostAddress(); //discovered_port = Integer.parseInt(st.nextToken()); discovered_port = recievedJson.getInt("port_to_share"); //Gravity code Log.v("DISCOVERY_CLIENT", "discovered " + discovered_name + ", " + discovered_ip + ":" + discovered_port); boolean add = true; if (ret.size() > 0) { for (DiscoveryServerInfo dsi : ret) { if (dsi != null && dsi.ip.equals(discovered_ip)) { add = false; break; } } } if (add) { ret.add(new DiscoveryServerInfo(discovered_name, discovered_ip, discovered_port)); } } catch (NoSuchElementException nsee) { Log.v("DISCOVERY_CLIENT", nsee.getLocalizedMessage()); } } } } catch (SocketTimeoutException tex) { /* ignored */ } } clientSocket.close(); } catch (Exception ex) { ex.printStackTrace(); Log.e("DISCOVERY_CLIENT", ex.toString()); } return ret; }
From source file:MainClass.java
public SenderThread(InetAddress address, int port) throws SocketException { this.server = address; this.port = port; this.socket = new DatagramSocket(); this.socket.connect(server, port); }
From source file:poisondog.net.udp.SendMission.java
public Void execute(DatagramParameter parameter) throws IOException { DatagramSocket socket = new DatagramSocket(); socket.setReuseAddress(true);// w ww .j a v a2s .c o m socket.setBroadcast(parameter.getBroadcast()); if (parameter.getTimeout() > 0) socket.setSoTimeout(parameter.getTimeout()); String data = IOUtils.toString(parameter.getInputStream()); DatagramPacket packet = new DatagramPacket(data.getBytes(), data.length()); packet.setAddress(InetAddress.getByName(parameter.getHost())); if (parameter.getPort() > 0) packet.setPort(parameter.getPort()); socket.send(packet); socket.close(); return null; }
From source file:tubessister.SenderR.java
public void run() { System.out.println("Sender Running "); try {/*from w w w . j a va 2 s.c o m*/ InetAddress IPAddress = InetAddress.getByName(targetAddress); DatagramSocket datagramSocket = new DatagramSocket(); ReliableSender ReliableSender = new ReliableSender(datagramSocket); byte[] sendData = message.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, targetPort); ReliableSender.send(sendPacket); datagramSocket.close(); } catch (UnknownHostException ex) { System.out.println("Unknown Host Exception"); } catch (SocketException ex) { System.out.println("Socket Exception"); } catch (IOException ex) { System.out.println("IO Exception"); } System.out.println("Send finished"); }
From source file:tubessister.Sender.java
public void run() { //System.out.println("Sender Running " ); try {/*from w w w . j a v a 2 s .com*/ InetAddress IPAddress = InetAddress.getByName(targetAddress); DatagramSocket datagramSocket = new DatagramSocket(); UnreliableSender unreliableSender = new UnreliableSender(datagramSocket); byte[] sendData = message.getBytes(); DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, targetPort); unreliableSender.send(sendPacket); datagramSocket.close(); } catch (UnknownHostException ex) { System.out.println("Unknown Host Exception"); } catch (SocketException ex) { System.out.println("Socket Exception"); } catch (IOException ex) { System.out.println("IO Exception"); } System.out.println("Send finished"); }