示例1:
public class IPDemo {
public static void main(String[] args) throws Exception {
InetAddress i = InetAddress.getLocalHost();
InetAddress ia = InetAddress.getByName("www.baidu.com");
sop(i);
sop(ia);
}
public static void sop(InetAddress i)
{
//获取主机地址
System.out.println(i.getHostAddress());
//获取主机名
System.out.println(i.getHostName());
}
}
示例2:
/*
* 通过udp传输方式,将一段文字数据发送出去
* 思路:
* 1.建立udpsocke服务
* 2.提供数据,并将数据封装到数据包中
* 3.通过socket服务的发送功能,将数据包发送出去
* 4.关闭资源
* */
//发送端
public class UdpDemo {
public static void main(String[] args) throws Exception {
//1.建立udp socke服务
DatagramSocket ds = new DatagramSocket();
//2.提供数据,并将数据封装到数据包中,指定发送到哪个端口
byte[] buf = "udp ge men lai le".getBytes();
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.0.100"),10000);
//3.通过socket服务的发送功能,将数据包发送出去
ds.send(dp);
//4.关闭资源
ds.close();
}
}
//接收端
class UdpRece
{
public static void main(String[] args) throws Exception
{
//1.创建udp socker,建立端点(监视的端口号)
DatagramSocket ds = new DatagramSocket(10000);
while(true)
{
//2.定义数据包,用于存储数据
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
//3.通过服务的receive方法将收到的数据存入数据包中
ds.receive(dp);//阻塞式方法
//4.通过数据包的方法获取其中的数据
//获取主机地址
String ip = dp.getAddress().getHostAddress();
//获取数据
String data = new String(dp.getData(),0,dp.getLength());
//获取端口号
int port = dp.getPort();
System.out.println(ip+data+port);
}
//5.关闭资源
//ds.close();
}
}
示例3:
class UdpSend2 {
public static void main(String[] args) throws Exception {
DatagramSocket ds = new DatagramSocket();
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
String line= null;
while((line=bufr.readLine())!=null)
{
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf, buf.length,InetAddress.getByName("192.168.0.100"),10001);
ds.send(dp);
}
ds.close();
}
}
class UdpRece2 {
public static void main(String[] args) throws Exception {
//接收端监视10001端口
DatagramSocket ds = new DatagramSocket(10001);
while(true)
{
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, buf.length);
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData(),0,dp.getLength());
System.out.println(ip+"--"+data);
}
}
}
示例4:
public class ChatDemo {
public static void main(String[] args) throws Exception {
//发送端可以不指定自己的端口号
DatagramSocket send_ds = new DatagramSocket();
//接收端必须指定自己的端口号
DatagramSocket rece_ds = new DatagramSocket(10004);
new Thread(new Send(send_ds)).start();
new Thread(new Send(rece_ds)).start();
}
}
//发送端
class Send implements Runnable
{
private DatagramSocket ds;
public Send(DatagramSocket ds)
{
this.ds = ds;
}
public void run() {
try {
BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line = bufr.readLine())!=null)
{
byte[] buf = line.getBytes();
DatagramPacket dp = new DatagramPacket(buf,buf.length,InetAddress.getByName("192.168.0.255"),10004);
ds.send(dp);
//ds.close();
}
} catch (Exception e) {
throw new RuntimeException("发送端出错");
}
}
}
//接收端
class Rece implements Runnable
{
private DatagramSocket ds;
public Rece(DatagramSocket ds)
{
this.ds = ds;
}
public void run() {
try {
while(true)
{
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf,buf.length);
ds.receive(dp);
String ip = dp.getAddress().getHostAddress();
String data = new String(dp.getData());
System.out.println(ip+"--"+data);
}
} catch (Exception e) {
throw new RuntimeException();
}
}
}