InetAddress:
代表IP地址
public static InetAddress getLocalHost() throws UnknownHostException
Returns the address of the local host.
获取本机IP,会以一个inetAddress的对象返回
public static InetAddress getByName(String host) throws UnknownHostException
Determines the IP address of a host, given the host's name.
根据ip地址或者域名,返回一个inetAddress对象
public String getHostName()
Gets the host name for this IP address.
获取ip地址对象对应的主机名
public String getHostAddress()
Returns the IP address string in textual presentation.
获取ip地址对象中的ip地址信息
public boolean isReachable(int timeout) throws IOException
在指定毫秒内,判断主机与该ip对应的主机是否能连通
public class test1 {
public static void main(String[] args) throws IOException {
//1:获取本机IP地址对象
InetAddress addr=InetAddress.getLocalHost();
System.out.println(addr.getHostName());
System.out.println(addr.getHostAddress());
//2:获取指定IP或者域名的IP地址对象
InetAddress addr2=InetAddress.getByName("www.baidu.com");
System.out.println(addr2.getHostName());
System.out.println(addr2.getHostAddress());
System.out.println(addr2.isReachable(5000));//判断本机是否能和addr2(百度IP)在5s内连通
//相当于 ping www.baidu.com
}
}
端口
用于标记正在计算机设备上运行的应用程序
分类:
周知端口:0~1023,被预先定义的知名应用占用
注册端口:1024~49151,分配给用户进程或某些应用程序
动态端口:49152~65535,因为它一般不固定分配某种进程,而是动态分配
注意:我们自己开发的程序一般选择注册端口,且一个设备中不能出现两个程序的端口号一致,否则出错
DatagramSocket:用与创建UDP客户端,服务端
public DatagramSocket() throws SocketException
Constructs a datagram socket and binds it to any available port on the local host machine. The socket will be bound to the
wildcard
address.创建 客户端的Socket,并随机分配一个端口号
public DatagramSocket(int port) throws SocketException
Constructs a datagram socket and binds it to the specified port on the local host machine. The socket will be bound to the
wildcard
address.创建 服务端的Socket,并指定一个端口号
public void send(DatagramPacket p) throws IOException
Sends a datagram packet from this socket. The
DatagramPacket
includes information indicating the data to be sent, its length, the IP address of the remote host, and the port number on the remote host.发送数据包
public void receive(DatagramPacket p) throws IOException
Receives a datagram packet from this socket. This method blocks(阻塞) until a datagram is received. When this method returns, the
DatagramPacket
's buffer is filled with the data received. The datagram packet also contains the sender's IP address, and the port number on the sender's machine. Thelength
field of the datagram packet object contains the length of the received message. If the message is longer than the packet's length, the message is truncated(切断).接收数据
DatagramPacket:创建数据包
public DatagramPacket(byte[] buf, int length)
Constructs a
DatagramPacket
for receiving packets of lengthlength
.创建发出去的数据包对象
public DatagramPacket(byte[] buf, int length, InetAddress address, int port)
Constructs a datagram packet for sending packets of length
length
to the specified port number on the specified host.创建发出去的数据包对象
public int getLength()
获取数据包,实际接收的字节个数
客户端:
public class client {
public static void main(String[] args)throws Exception {
//创建一个sokcet对象,用于发送数据
DatagramSocket cli=new DatagramSocket();
//创建数据包
Scanner sa=new Scanner(System.in);
String s=sa.nextLine();
byte[] bytes = s.getBytes();
InetAddress addr=InetAddress.getLocalHost();//获取服务端地址
DatagramPacket p=new DatagramPacket(bytes,bytes.length,addr,8888);
//发送数据
cli.send(p);
cli.close();//关闭
}
}
服务端:
public class server {
public static void main(String[] args) throws Exception{
//创建一个服务端socket对象
DatagramSocket ser = new DatagramSocket(8888);
//创建一个空数据包,接收数据
byte[]bytes=new byte[1024];
DatagramPacket p=new DatagramPacket(bytes,bytes.length);
//接收数据
ser.receive(p);
//从字节数组中,获取数据
//接受多少,就倒出多少
int len=p.getLength();
String s=new String(bytes,0,len);
System.out.println("客户端发来的数据是"+s);
System.out.println("客户端的ip地址是:"+p.getAddress().getHostAddress());
ser.close();
}
}
以上只能实现一收数据,接下来实现多发多收数据:
客户端:
public class client {
public static void main(String[] args)throws Exception {
//创建一个sokcet对象,用于发送数据
DatagramSocket cli=new DatagramSocket();
//创建数据包
Scanner sa=new Scanner(System.in);
while (true) {
String s=sa.nextLine();
byte[] bytes = s.getBytes();
if(Objects.equals("exit",s))
{
cli.close();
break;
}
InetAddress addr=InetAddress.getLocalHost();//获取服务端地址
DatagramPacket p=new DatagramPacket(bytes,bytes.length,addr,8888);
//发送数据
cli.send(p);
}
}
}
服务端:
public class server {
public static void main(String[] args) throws Exception{
//创建一个服务端socket对象
DatagramSocket ser = new DatagramSocket(8888);
//创建一个空数据包,接收数据
byte[]bytes=new byte[1024];
DatagramPacket p=new DatagramPacket(bytes,bytes.length);
//接收数据
while (true) {
ser.receive(p);
//从字节数组中,获取数据
//接受多少,就倒出多少
int len=p.getLength();
String s=new String(bytes,0,len);
System.out.println("客户端发来的数据是"+s);
System.out.println("客户端的ip地址是:"+p.getAddress().getHostAddress());
}
}
}
UDP服务端可以同时接受多个客户端发来的信息