Java的网络编程 Li.048
网络通信 ip 电脑地址 UDP TCP 端口号 socket 插座, 数据流间的插座, 用来联通 类似电脑是港口ip, socket是航线用来发货, 端口号是港口停放船的泊位, UDP TCP是小船还是大船来运送数据箱子 DatagramSocket 与 DatagramPacket DatagramSocket 创建一条航线 DatagramPacket 指定电脑ip(港口), 端口号(泊位), 运送的数据(集装箱) import java.io.IOException; import java.net.*; import java.nio.charset.StandardCharsets; public class Demo1_Send { public static void main(String[] args) throws IOException { String msg = "中午一起吃饭"; DatagramSocket datagramSocket = new DatagramSocket(); DatagramPacket datagramPacket = new DatagramPacket (msg.getBytes(StandardCharsets.UTF_8),msg.getBytes().length, InetAddress.getByName("127.0.0.1"),2000); datagramSocket.send(datagramPacket); datagramSocket.close(); } } import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; public class Demo2_Receive { public static void main(String[] args) throws IOException { DatagramSocket socket = new DatagramSocket(2000); DatagramPacket packet = new DatagramPacket(new byte[1024], 1024); socket....