知识:
InetAddresss:getByName,getHostName,getHostAddress方法
UDP通信程序:单播,组播,广播代码实现
一.InetAddress 的使用
1.static InetAddress getByName(String host)
确定主机名称的IP地址。主机名称可以是机器名称,也可以是IP地址
2.String getHostName()
获取此IP地址的主机名
3.String getHostAddress()
返回文本显示中的IP地址字符串
4.InetAddress代码入门:
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetadressDemo1 {
public static void main(String[] args) throws UnknownHostException {
//1.static InetAddress getByName(String host)[确定主机名称的IP地址。主机名称可以是机器名称,也可以是IP地址]
InetAddress address = InetAddress.getByName("沉迷代码");
//2.String getHostName() 获取此IP地址的主机名
String hostName = address.getHostName();
System.out.println("主机名为" + hostName);
//3.String getHostAddress() 返回文本显示中的IP地址字符串
String ip = address.getHostAddress();
System.out.println("IP为" + ip);
}
}
二.UDP通信程序
1.UDP发送数据
(1)UDP发送数据步骤:
步骤1:找码头[创建发送端的DatagramSocket对象]
步骤2:打包礼物[创建数据,并把数据打包(DatagramPacket)]
步骤3:由码头发送包裹[调用DatagramSocket对象的方法发送数据]
步骤4:付钱走羊[释放资源]
(2)UDP发送数据具体实现:
import java.io.IOException;
import java.net.*;
public class ClientDemo {
public static void main(String[] args) throws IOException {
//1.创建发送端的DatagramSocket对象
DatagramSocket ds = new DatagramSocket();
//2.创建数据,并把数据打包(DatagramPacket)
//DatagramPacket?(byte[] buf, int length, InetAddress address, int port)
String s = "送给村长老丈人的礼物";
byte[] bytes = s.getBytes();
InetAddress address = InetAddress.getByName("127.0.0.1");
int port = 10000;
DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,port);
//3.调用DatagramSocket对象的方法发送数据
ds.send(dp);
//4.释放资源
ds.close();
}
}
2.UDP接受数据
(1)UDP接受数据步骤:
步骤1:找码头[创建发送端的DatagramSocket对象]
步骤2:找新箱子[创建一个箱子,用于接收数据]
步骤3:码头接收礼物并放入新箱子中[调用DatagramSocket的方法接收数据并将数据放入箱子中]
步骤4:村长从箱子里获取礼物[解析数据包,并把数据在控制台显示]
步骤5:付钱走羊[释放资源]
(2)UDP接受数据具体实现:
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class ServerDemo {
//注意点:
//1.要先运行接收端,再运行发送端
//2.如果接收端再启动之后,没有接收到数据,那么会死等(阻塞).
//3.在接收数据的时候,需要调用一个getLength方法,表示接收到了多少字节
public static void main(String[] args) throws IOException {
//1.找码头 ---- 表示接收端从10000端口接收数据的.
DatagramSocket ds = new DatagramSocket(10000);
//2,创建一个新的箱子
byte [] bytes = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
//3.接收礼物,把礼物放到新的箱子中
System.out.println("-----------接收前----------");
ds.receive(dp);
System.out.println("------------接收后---------");
//4.从新的箱子里面获取礼物
//byte[] data = dp.getData();和bytes数组中数据等同
int length = dp.getLength();
System.out.println(new String(bytes,0,length));
//5.拿完走羊
ds.close();
}
}
二.UDP通信程序单播
1.UDP发送数据:数据来自于键盘录入,直到输入的数据是886,发送数据结束
import java.io.IOException;
import java.net.*;
import java.util.Scanner;
public class ClientDemo {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
DatagramSocket ds = new DatagramSocket();
while (true) {
String s = sc.nextLine();
if("886".equals(s)){
break;
}
byte[] bytes = s.getBytes();
InetAddress address = InetAddress.getByName("127.0.0.1");
int port = 10000;
DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,port);
ds.send(dp);
}
ds.close();
}
}
2.UDP接收数据:因为接收端不知道发送端什么时候停止发送,故采用死循环接收。
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
public class ServerDemo {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(10000);
while (true) {
byte [] bytes = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
ds.receive(dp);
byte[] data = dp.getData();
int length = dp.getLength();
System.out.println(new String(data,0,length));
}
}
}
三.UDP通信程序组播
组播地址:224.0.0.0 ~ 239.255.255.255
其中224.0.0.0 ~ 224.0.0.255 为预留的组播地址
//1.组播发送端
import java.io.IOException;
import java.net.*;
public class ClinetDemo {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket();
String s = "hello 组播";
byte[] bytes = s.getBytes();
InetAddress address = InetAddress.getByName("224.0.1.0");
int port = 10000;
DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,port);
ds.send(dp);
ds.close();
}
}
//2.组播接受端
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
MulticastSocket ms = new MulticastSocket(10000);
DatagramPacket dp = new DatagramPacket(new byte[1024],1024);
//把当前计算机绑定一个组播地址,表示添加到这一组中.
ms.joinGroup(InetAddress.getByName("224.0.1.0"));
ms.receive(dp);
byte[] data = dp.getData();
int length = dp.getLength();
System.out.println(new String(data,0,length));
ms.close();
}
}
四.UDP通信程序广播
广播地址:255.255.255.255
//1.广播发送端
import java.io.IOException;
import java.net.*;
public class ClientDemo {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket();
String s = "广播 hello";
byte[] bytes = s.getBytes();
//255.255.255.255广播地址,路由器看到,就会连接到所有能接受地址然后进行数据发送
InetAddress address = InetAddress.getByName("255.255.255.255");
int port = 10000;
DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,port);
ds.send(dp);
ds.close();
}
}
//2.广播接受端
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class ServerDemo {
public static void main(String[] args) throws IOException {
DatagramSocket ds = new DatagramSocket(10000);
DatagramPacket dp = new DatagramPacket(new byte[1024],1024);
ds.receive(dp);
byte[] data = dp.getData();
int length = dp.getLength();
System.out.println(new String(data,0,length));
ds.close();
}
}