标签:out 网卡 通信 压缩 示例 回话 七层 system sde
public static InetAddress[] getAllByName(String host) throws UnknownHostException
public static InetAddress getByName(String host) throws UnknownHostException
public String getHostAddress()
public String getHostName()
package java21; import java.net.InetAddress; import java.net.UnknownHostException; /** * 2017/10/16 * 说明: */ public class InetAddressDemo { public static void main(String[] args) throws UnknownHostException { InetAddress ip = InetAddress.getByName("www.baidu.com"); System.out.print(ip.getHostName() +"\n"+ip.getHostAddress()); } }
public DatagramSocket(int port) throws SocketException
public void send(DatagramPacket p) throws IOException
public void receive(DatagramPacket p) throws IOException
public DatagramPacket(byte[] buf, int length, InetAddress address,int port)
public DatagramPacket(byte[] buf, int length)
public InetAddress getAddress()
public byte[] getData()
public int getLength()
package java20; import java.io.IOException; import java.net.*; /** * 2017/10/17 * 说明: */ public class UDPSendDemo { public static void main(String[] args) throws IOException { DatagramSocket ds = new DatagramSocket(); String str = "hello"; DatagramPacket dp = new DatagramPacket(str.getBytes(),str.length(), InetAddress.getByName("127.0.0.1"),12345); ds.send(dp); ds.close(); } }
package java20; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.SocketException; /** * 2017/10/17 * 说明: */ public class UDPReceiveDemo { public static void main(String[] args) throws IOException { DatagramSocket ds = new DatagramSocket(12345); byte[] buffer = new byte[1024]; DatagramPacket dp = new DatagramPacket(buffer,buffer.length); ds.receive(dp); String ip = dp.getAddress().getHostAddress(); String str = new String(dp.getData(),0,dp.getLength()); int port = dp.getPort(); System.out.println("ip地址:"+ip); System.out.println("接收的内容:"+str); System.out.println("端口号:"+port); ds.close(); } }
标签:out 网卡 通信 压缩 示例 回话 七层 system sde
原文地址:http://www.cnblogs.com/xuweiweiwoaini/p/7674661.html