/** * @param hostName * @return ip address or hostName if UnknownHostException */ public static String getIpByHost(String hostName) { try { return InetAddress.getByName(hostName).getHostAddress(); } catch (UnknownHostException e) { return hostName; } }
@Override public void write(byte[] b, int off, int len) throws IOException { // Important not to cache InetAddress and let the JVM/OS to handle DNS caching. datagramSocket.send(new DatagramPacket(b, off, len, InetAddress.getByName(host), port)); }
InetSocketAddress createPeerAddress() { if (remoteAddress == null) { return null; } int port = remotePort > 0 ? remotePort : 0; try { InetAddress address = InetAddress.getByName(remoteAddress); return new InetSocketAddress(address, port); } catch (UnknownHostException e) { return null; } }
try { DatagramSocket socket = new DatagramSocket(); Log.d("VS", "Socket Created"); DatagramPacket packet; final InetAddress destination = InetAddress.getByName("192.168.1.5"); Log.d("VS", "Address retrieved"); packet = new DatagramPacket (buffer,buffer.length,destination,port); socket.send(packet); System.out.println("MinBufferSize: " +minBufSize);
public void start() throws Exception { StringBuilder sb=new StringBuilder(); sb.append("\n\n----------------------- MPerf -----------------------\n"); sb.append("Date: ").append(new Date()).append('\n'); sb.append("Run by: ").append(System.getProperty("user.name")).append("\n"); System.out.println(sb); mcast_sock=new MulticastSocket(7500); sock_addr=new InetSocketAddress(InetAddress.getByName("232.5.5.5"), 7500); mcast_sock.joinGroup(InetAddress.getByName("232.5.5.5")); mcast_sock.setReceiveBufferSize(10 * 1000 * 1000); mcast_sock.setSendBufferSize(5 * 1000 * 1000); mcast_sock.setTrafficClass(8); receiver=new Receiver(); receiver.start(); }
InetAddress address = InetAddress.getByName("www.example.com"); System.out.println(address.getHostAddress());
public static void main(String args[]) { if (2 != args.length) { System.err.println(MSG_USAGE); System.exit(E_USAGE); } try { Socket s = new Socket( InetAddress.getByName(args[0]), Integer.valueOf(args[1]).intValue()); s.close(); System.exit(0); } catch (Throwable t) { System.err.println(t.toString()); System.exit(E_FAILED); } } }
this.num=num; this.size=size; this.local_addr=InetAddress.getByName(local_addr); this.local_port=local_port; this.remote_addr=InetAddress.getByName(remote_addr); this.remote_port=remote_port; System.out.println("-- creating socket to " + this.remote_addr + ":" + this.remote_port); Socket sock=new Socket(this.remote_addr, remote_port); sock.setReceiveBufferSize(receivebuf); sock.setSendBufferSize(sendbuf); DataOutputStream out=new DataOutputStream(new BufferedOutputStream(sock.getOutputStream())); byte[] buf=new byte[size]; int cnt=1; System.out.println("-- sending " + num + " messages"); for(int i=0; i < num; i++) { out.write(buf, 0, buf.length); out.flush(); if(cnt % incr == 0) System.out.println("-- sent " + cnt + " messages"); cnt++;
try { ms = new MulticastSocket(port); dp = new DatagramPacket(buf, length); InetAddress group = InetAddress.getByName(host); ms.joinGroup(new InetSocketAddress("239.239.239.88", 9999), Utils.getLocalNetworkInterface()); System.out.println("监听多播端口打开:"); DatagramPacket dp2 = new DatagramPacket(message.getBytes(), message.length(), group, port); ms.send(dp2); ms.receive(dp); System.out.println("收到多播消息:" + sbuf.toString());
private void shutdownMoco(final int shutdownPort, final String shutdownMocoKey) throws IOException { Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), shutdownPort); socket.setSoLinger(false, 0); OutputStream outputStream = socket.getOutputStream(); outputStream.write((shutdownMocoKey + "\r\n").getBytes()); outputStream.flush(); socket.close(); }
private static boolean isPortOpen(String host, Integer port) { try (Socket socket = new Socket()) { socket.connect(new InetSocketAddress(InetAddress.getByName(host), port), 1000); return true; } catch (ConnectException | SocketTimeoutException e) { return false; } catch (IOException e) { throw new UncheckedIOException(e); } } }
LOG.info("connecting to {} {}", host, port); Socket sock; InetSocketAddress hostaddress= host != null ? new InetSocketAddress(host, port) : new InetSocketAddress(InetAddress.getByName(null), port); if (secure) { LOG.info("using secure socket"); sock = new Socket(); sock.connect(hostaddress, timeout); BufferedReader reader = null; try { OutputStream outstream = sock.getOutputStream(); outstream.write(cmd.getBytes()); outstream.flush();
@Override OutputStream openNewOutputStream() throws IOException { final Socket socket = socketFactory.createSocket(); // Prevent automatic closing of the connection during periods of inactivity. socket.setKeepAlive(true); // Important not to cache `InetAddress` in case the host moved to a new IP address. socket.connect(new InetSocketAddress(InetAddress.getByName(host), port), connectionTimeoutMs); return new BufferedOutputStream(socket.getOutputStream(), sendBufferSize); } }
private void unicast(String msg, String host) { if (logger.isInfoEnabled()) { logger.info("Send unicast message: " + msg + " to " + host + ":" + multicastPort); } try { byte[] data = (msg + "\n").getBytes(); DatagramPacket hi = new DatagramPacket(data, data.length, InetAddress.getByName(host), multicastPort); multicastSocket.send(hi); } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } }
@Test public void testSend() throws Exception { String host = "239.239.239.99";//多播地址 int port = 9998; String message = "test-multicastSocket"; try { InetAddress group = InetAddress.getByName(host); MulticastSocket s = new MulticastSocket(); //加入多播组 s.joinGroup(new InetSocketAddress(host, port), Utils.getLocalNetworkInterface()); DatagramPacket dp = new DatagramPacket(message.getBytes(), message.length(), group, port); s.send(dp); s.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
boolean entered = BTraceRuntime.enter(); try { ds = new DatagramSocket(); DatagramPacket dp = new DatagramPacket(new byte[0], 0); try { dp.setAddress(InetAddress.getByName(SharedSettings.GLOBAL.getStatsdHost())); } catch (UnknownHostException e) { System.err.println("[statsd] invalid host defined: " + SharedSettings.GLOBAL.getStatsdHost()); dp.setAddress(InetAddress.getLoopbackAddress()); } catch (SecurityException e) { } else { dp.setData(sb.toString().getBytes(CHARSET)); ds.send(dp); sb.setLength(0); ds.send(dp);
@Test public void TestServer() throws Exception { //接受组播和发送组播的数据报服务都要把组播地址添加进来 String host = "239.239.239.88";//多播地址 int port = 9998; InetAddress group = InetAddress.getByName(host); DatagramChannel channel = DatagramChannel.open(StandardProtocolFamily.INET); channel.bind(new InetSocketAddress(port)); channel.join(group, Utils.getLocalNetworkInterface()); ByteBuffer buffer = ByteBuffer.allocate(1024); SocketAddress sender = channel.receive(buffer); buffer.flip(); byte[] data = new byte[buffer.remaining()]; buffer.get(data); System.out.println(new String(data)); }