List of usage examples for java.net InetAddress getByName
public static InetAddress getByName(String host) throws UnknownHostException
From source file:Main.java
public static void main(String[] args) throws Exception { int mcPort = 12345; String mcIPStr = "230.1.1.1"; MulticastSocket mcSocket = null; InetAddress mcIPAddress = null; mcIPAddress = InetAddress.getByName(mcIPStr); mcSocket = new MulticastSocket(mcPort); System.out.println("Multicast Receiver running at:" + mcSocket.getLocalSocketAddress()); mcSocket.joinGroup(mcIPAddress);// w w w. j av a 2 s. c o m DatagramPacket packet = new DatagramPacket(new byte[1024], 1024); System.out.println("Waiting for a multicast message..."); mcSocket.receive(packet); String msg = new String(packet.getData(), packet.getOffset(), packet.getLength()); System.out.println("[Multicast Receiver] Received:" + msg); mcSocket.leaveGroup(mcIPAddress); mcSocket.close(); }
From source file:Main.java
public static void main(String args[]) throws IOException { String host = args[0];/*from www . j ava 2s . c om*/ byte message[] = new byte[256]; InetAddress address = InetAddress.getByName(host); System.out.println("Checking at: " + address); DatagramPacket packet = new DatagramPacket(message, message.length, address, DAYTIME_PORT); DatagramSocket socket = new DatagramSocket(); socket.send(packet); packet = new DatagramPacket(message, message.length); socket.receive(packet); String time = new String(packet.getData()); System.out.println(time); socket.close(); }
From source file:UDPSend.java
public static void main(String args[]) { try {/*from www . ja v a2s . c o m*/ String host = "www.java2s.com"; int port = 90; byte[] message = "Java Source and Support".getBytes(); // 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); } }
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 w w w . ja va2 s . co 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 { ServerSocket serverSocket = new ServerSocket(12900, 100, InetAddress.getByName("localhost")); System.out.println("Server started at: " + serverSocket); while (true) { System.out.println("Waiting for a connection..."); final Socket activeSocket = serverSocket.accept(); System.out.println("Received a connection from " + activeSocket); Runnable runnable = () -> handleClientRequest(activeSocket); new Thread(runnable).start(); // start a new thread }/*from w ww .j av a2s.c om*/ }
From source file:lookForPorts3.java
public static void main(String[] args) { Socket theSocket;/* w w w . j a v a 2 s .c o m*/ String host = "localhost"; if (args.length > 0) { host = args[0]; } try { InetAddress theAddress = InetAddress.getByName(host); for (int i = 1; i <= 65535; i++) { try { theSocket = new Socket(host, i); System.out.println("There is a server on port " + i + " of " + host); theSocket.close(); } catch (IOException e) { // must not be a server on this port } } } catch (UnknownHostException e) { System.err.println(e); } }
From source file:Main.java
public static void main(String[] args) throws Exception { int port = 0; byte ttl = (byte) 1; byte[] data = "Here's some multicast data\r\n".getBytes(); DatagramPacket dp = new DatagramPacket(data, data.length, InetAddress.getByName("google.com"), port); MulticastSocket ms = new MulticastSocket(InetSocketAddress.createUnresolved("google.com", 8080)); ms.joinGroup(InetSocketAddress.createUnresolved("127.0.0.1", 8080), NetworkInterface.getByIndex(0)); for (int i = 1; i < 10; i++) { ms.send(dp, ttl);/*from ww w .ja v a 2 s. c o m*/ } ms.leaveGroup(InetAddress.getByName("google.com")); ms.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { int port = 0; byte ttl = (byte) 1; byte[] data = "Here's some multicast data\r\n".getBytes(); DatagramPacket dp = new DatagramPacket(data, data.length, InetAddress.getByName("google.com"), port); MulticastSocket ms = new MulticastSocket(InetSocketAddress.createUnresolved("google.com", 8080)); ms.joinGroup(InetSocketAddress.createUnresolved("127.0.0.1", 8080), NetworkInterface.getByIndex(0)); for (int i = 1; i < 10; i++) { ms.send(dp, ttl);//from ww w . j a v a2 s . c o m } ms.setLoopbackMode(true); ms.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { int port = 0; byte ttl = (byte) 1; byte[] data = "Here's some multicast data\r\n".getBytes(); DatagramPacket dp = new DatagramPacket(data, data.length, InetAddress.getByName("google.com"), port); MulticastSocket ms = new MulticastSocket(InetSocketAddress.createUnresolved("google.com", 8080)); ms.joinGroup(InetSocketAddress.createUnresolved("127.0.0.1", 8080), NetworkInterface.getByIndex(0)); for (int i = 1; i < 10; i++) { ms.send(dp, ttl);// w w w. j ava 2s. c o m } ms.setNetworkInterface(NetworkInterface.getByIndex(0)); ms.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { int port = 0; byte ttl = (byte) 1; byte[] data = "Here's some multicast data\r\n".getBytes(); DatagramPacket dp = new DatagramPacket(data, data.length, InetAddress.getByName("google.com"), port); MulticastSocket ms = new MulticastSocket(InetSocketAddress.createUnresolved("google.com", 8080)); ms.joinGroup(InetSocketAddress.createUnresolved("127.0.0.1", 8080), NetworkInterface.getByIndex(0)); for (int i = 1; i < 10; i++) { ms.send(dp, ttl);//from w ww . j ava 2 s. com } ms.setTimeToLive(1000); ms.close(); }