关键三要素IP:设备在网络中的地址端口应用程序在设备中唯一的标识协议连接和数据在网络中传输的规则1.InetAddress的常用方法//获取本机Ip,以InetAddress对象返回 InetAddress localHost InetAddress.getLocalHost(); System.out.println(localHost); //根据IP地址返回InetAddress对象 System.out.println(InetAddress.getByName(localhost)); //获取IP地址对象对应的主机名 System.out.println(localHost.getHostName()); //指定在毫秒内判断主机与该IP对应的主机是否接通 System.out.println(localHost.isReachable(2345));2.UDP通信public class Server { public static void main(String[] args)throws Exception { System.out.println(欢迎使用服务端); //创建服务端 DatagramSocket socketnew DatagramSocket(6666); //创建数据包 byte[]buffernew byte[1024*60]; DatagramPacket packetnew DatagramPacket(buffer, buffer.length); //接收信息 while (true) { socket.receive(packet); //解码数据 int length packet.getLength(); String snew String(buffer,0,length); System.out.println(s); System.out.println(客户端IP地址: packet.getAddress().getHostAddress());//拿到客户端IP地址 System.out.println(客户端端口: packet.getPort());//拿到客户端端口 System.out.println(欢迎下次使用); } } } public class Client { public static void main(String[] args) throws Exception { //创建客户端 DatagramSocket socketnew DatagramSocket(); //创建数据包 Scanner scnew Scanner(System.in); while (true) { System.out.println(请说); String s sc.nextLine(); if (exit.equals(s)){ System.out.println(退出系统); socket.close(); break; } byte[] buffer s.getBytes(); //创建发出去的数据包对象 DatagramPacket packet new DatagramPacket(buffer, buffer.length, InetAddress.getLocalHost(), 6666); //发送数据包 socket.send(packet); System.out.println(客户端已发送信息); } } } //多发多收 //将接收发送的内容使用死循环服务端无需关闭 //创建多个客户端有循环的条件下注意事项先启动服务端再启动客户端3.TCP通信public class Server2 { public static void main(String[] args) throws Exception{ System.out.println(欢迎进入服务端); //创建服务端 ServerSocket socketnew ServerSocket(6666); //创建数据包连接数据 Socket accept socket.accept(); //创建字节输入流对象 InputStream inputStream accept.getInputStream(); DataInputStream disnew DataInputStream(inputStream); //接收数据 while (true) { try { String s dis.readUTF(); System.out.println(s); } catch (IOException e) { System.out.println(accept.getRemoteSocketAddress()离线); dis.close(); accept.close(); socket.close(); break; } } } } public class Client2 { public static void main(String[] args) throws Exception{ //创建客户端 System.out.println(InetAddress.getLocalHost()); Socket socketnew Socket(10.4.136.23,6666); //创建字节输出流对象 OutputStream outputStream socket.getOutputStream(); DataOutputStream dosnew DataOutputStream(outputStream); //发送内容 Scanner scnew Scanner(System.in); while (true) { System.out.println(请说); String s sc.nextLine(); if (exit.equals(s)){ System.out.println(退出系统); socket.close(); break; } dos.writeUTF(s); //刷新 dos.flush(); } } }注意事项当有多个客户端时只有一个客户端会和服务端连通4.UDP与TCP的区别UDP:无连接不可靠通信通信效率高------语音视频通话TCP:面向连接可靠通信通信效率低-----支付下载
UDP和TCP网络通信
关键三要素IP:设备在网络中的地址端口应用程序在设备中唯一的标识协议连接和数据在网络中传输的规则1.InetAddress的常用方法//获取本机Ip,以InetAddress对象返回 InetAddress localHost InetAddress.getLocalHost(); System.out.println(localHost); //根据IP地址返回InetAddress对象 System.out.println(InetAddress.getByName(localhost)); //获取IP地址对象对应的主机名 System.out.println(localHost.getHostName()); //指定在毫秒内判断主机与该IP对应的主机是否接通 System.out.println(localHost.isReachable(2345));2.UDP通信public class Server { public static void main(String[] args)throws Exception { System.out.println(欢迎使用服务端); //创建服务端 DatagramSocket socketnew DatagramSocket(6666); //创建数据包 byte[]buffernew byte[1024*60]; DatagramPacket packetnew DatagramPacket(buffer, buffer.length); //接收信息 while (true) { socket.receive(packet); //解码数据 int length packet.getLength(); String snew String(buffer,0,length); System.out.println(s); System.out.println(客户端IP地址: packet.getAddress().getHostAddress());//拿到客户端IP地址 System.out.println(客户端端口: packet.getPort());//拿到客户端端口 System.out.println(欢迎下次使用); } } } public class Client { public static void main(String[] args) throws Exception { //创建客户端 DatagramSocket socketnew DatagramSocket(); //创建数据包 Scanner scnew Scanner(System.in); while (true) { System.out.println(请说); String s sc.nextLine(); if (exit.equals(s)){ System.out.println(退出系统); socket.close(); break; } byte[] buffer s.getBytes(); //创建发出去的数据包对象 DatagramPacket packet new DatagramPacket(buffer, buffer.length, InetAddress.getLocalHost(), 6666); //发送数据包 socket.send(packet); System.out.println(客户端已发送信息); } } } //多发多收 //将接收发送的内容使用死循环服务端无需关闭 //创建多个客户端有循环的条件下注意事项先启动服务端再启动客户端3.TCP通信public class Server2 { public static void main(String[] args) throws Exception{ System.out.println(欢迎进入服务端); //创建服务端 ServerSocket socketnew ServerSocket(6666); //创建数据包连接数据 Socket accept socket.accept(); //创建字节输入流对象 InputStream inputStream accept.getInputStream(); DataInputStream disnew DataInputStream(inputStream); //接收数据 while (true) { try { String s dis.readUTF(); System.out.println(s); } catch (IOException e) { System.out.println(accept.getRemoteSocketAddress()离线); dis.close(); accept.close(); socket.close(); break; } } } } public class Client2 { public static void main(String[] args) throws Exception{ //创建客户端 System.out.println(InetAddress.getLocalHost()); Socket socketnew Socket(10.4.136.23,6666); //创建字节输出流对象 OutputStream outputStream socket.getOutputStream(); DataOutputStream dosnew DataOutputStream(outputStream); //发送内容 Scanner scnew Scanner(System.in); while (true) { System.out.println(请说); String s sc.nextLine(); if (exit.equals(s)){ System.out.println(退出系统); socket.close(); break; } dos.writeUTF(s); //刷新 dos.flush(); } } }注意事项当有多个客户端时只有一个客户端会和服务端连通4.UDP与TCP的区别UDP:无连接不可靠通信通信效率高------语音视频通话TCP:面向连接可靠通信通信效率低-----支付下载