42.网络编程
Posted luzhanshi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了42.网络编程相关的知识,希望对你有一定的参考价值。
1.网络编程入门
1.1.网络编程概述
1.2网络编程三要素
1.3 IP
IP地址常用命令:
ipconfig 查看本机IP地址
ping IP地址 检查网络是否连通
特殊IP地址:127.0.0.1:是回送地址,可以表示本机地址,一般用来测试使用
1.4 InetAddress
// InetAddress inetAddress = InetAddress.getByName("DESKTOP-2PVNKVI"); InetAddress inetAddress = InetAddress.getByName("192.168.0.111"); String hostAddress = inetAddress.getHostAddress(); String hostName = inetAddress.getHostName(); System.out.println(hostAddress+":"+hostName);
1.5端口
1.6协议
2.UDP通信程序
2.1 UDP通信原理
2.2 UDP发送数据
public static void main(String[] args) throws IOException { // 1.创建发送端Socket对象DatagramSocket DatagramSocket datagramSocket = new DatagramSocket(); // 2.创建数据,并打包数据 // DatagramPacket?(byte[] buf, int length, InetAddress address, int port) : // 构造一个数据包,发送长度为 length的数据包到指定主机上的指定端口号。 byte[] bytes = "hello,UDP,I`M COMMING!!!".getBytes(); DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length, InetAddress.getByName("192.168.0.111"),8088); // 3.发送数据 datagramSocket.send(datagramPacket); // 4.关闭发送端 datagramSocket.close(); }
2.3UDP接收数据
public static void main(String[] args) throws IOException { // 1.创建接收端的Socket对象,DataGramSocket // DatagramSocket?(int port) 构造数据报套接字并将其绑定到本地主机上的指定端口。 DatagramSocket datagramSocket = new DatagramSocket(8088); // 2.创建一个数据包用来接收数据 // DatagramPacket?(byte[] buf, int length) 构造一个 DatagramPacket用于接收长度为 length数据 byte[] bytes = new byte[1024]; DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length); // 3.接收数据 datagramSocket.receive(datagramPacket); // 4.解析数据,加工数据,使数据为实际数据长度 byte[] data = datagramPacket.getData(); String s = new String(data, 0, datgramPacket.getLength); // 5.展示数据 System.out.println("接收到数据:"+s); // 6.关闭接收端 datagramSocket.close(); }
2.4UDP练习
public class PracticeSend { public static void main(String[] args) throws IOException { BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in)); String line; while ((line=bufferedreader.readLine())!=null){ // 输入数据是886,终止发送 if (line.equals("886")){ break; } send(line); } } private static void send(String s) throws IOException { DatagramSocket datagramSocket = new DatagramSocket(); byte[] bytes = s.getBytes(); DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length, InetAddress.getByName("192.168.0.111"),8088); datagramSocket.send(datagramPacket); datagramSocket.close(); } }
public class PracticeRecive { public static void main(String[] args) throws IOException { DatagramSocket datagramSocket = new DatagramSocket(8088); while (true) {//因为不知道发送端什么时候停止发送,所以一直接收 recive(datagramSocket); } } private static void recive(DatagramSocket datagramSocket) throws IOException { byte[] bytes = new byte[1024]; DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length); datagramSocket.receive(datagramPacket); byte[] data = datagramPacket.getData(); System.out.println("接收到数据:" + new String(data, 0, datagramPacket.getLength())); } }
3.TCP通信
3.1TCP通信原理
3.2TCP发送数据
public static void main(String[] args) throws IOException { // 创建客户端Socket对象(Socket) // Socket?(InetAddress address, int port) 创建流套接字并将其连接到指定IP地址的指定端口号。 // Socket socket = new Socket(InetAddress.getByName("192.168.0.111"),8099); // Socket?(String host, int port) 创建流套接字并将其连接到指定主机上的指定端口号。 Socket st = new Socket("192.168.0.111", 8099); // 2.获取输出流写数据 OutputStream outputStream = st.getOutputStream(); outputStream.write("^_^,你好TCP!!!".getBytes()); // 释放资源 st.close(); }
3.3TCP接收数据
//1.创建服务器端Socket对象(ServerSocket) //ServerSocket?(int port) 创建绑定到指定端口的服务器套接字。 ServerSocket serverSocket = new ServerSocket(8099); // 2.监听客户连接并返回一个Socket对象:Socket accept?() 侦听要连接到此套接字并接受它。 Socket socket = serverSocket.accept(); // 3.获取输入流并读取数据进行展示 InputStream inputStream = socket.getInputStream(); byte[] bytes = new byte[1024]; int length=inputStream.read(bytes); String s = new String(bytes, 0, length); System.out.println("数据是:"+s); // 4.释放资源 inputStream.close(); serverSocket.close(); }
3.4TCP案例
字符流发送接收数据(注意:当发送视频音频图片时等不可使用字符流)
public class Client2 { public static void main(String[] args) throws IOException { Socket socket = new Socket("192.168.0.111", 8099); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); String line; while ((line=bufferedReader.readLine())!=null){ if ("886".equals(line)){ break; } bufferedWriter.write(line); bufferedWriter.flush(); bufferedWriter.newLine(); } socket.close(); } }
public class Server2 { public static void main(String[] args) throws IOException { ServerSocket serverSocket = new ServerSocket(8099); Socket socket = serverSocket.accept(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line; while ((line=bufferedReader.readLine())!=null){ System.out.println(line); } serverSocket.close(); } }
以上是关于42.网络编程的主要内容,如果未能解决你的问题,请参考以下文章
VSCode自定义代码片段14——Vue的axios网络请求封装