1、初识网络编程
2、网络编程三要素
3、三要素(IP)
4、IPV4的一些小细节
5、Inetaddress类的使用
package com.leitao.demo.network;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @Description: TODO
* @Author LeiTao
* @Date 2023/2/15
* @Version V1.0
**/
public class IntAddress {
public static void main(String[] args) throws UnknownHostException {
//1.获取InetAddress的对象
//IP的对象 一台电脑的对象
InetAddress address = InetAddress.getByName("DESKTOP-C280N7F");
System.out.println(address);
String hostName = address.getHostName();
System.out.println(hostName);
String ip = address.getHostAddress();
System.out.println(ip);
}
}
6、端口号
7、协议
8、UDP协议(发送数据)
package com.leitao.demo.network;
import java.io.IOException;
import java.net.*;
/**
* @Description: TODO
* @Author LeiTao
* @Date 2023/2/15
* @Version V1.0
**/
public class UDPDemo {
public static void main(String[] args) throws IOException {
//1.创建DatagramSocket对象(快递公司)
/**
* 细节:
* 绑定端口,以后我们就是通过这个端口往外发送
* 空参:所有可用的端口中随机一个进行使用
* 有参:指定端口号进行绑定
*/
DatagramSocket socket = new DatagramSocket();
//2.打包数据
String str = "woshinidie!";
byte[] bytes = str.getBytes();
InetAddress address = InetAddress.getByName("127.0.0.1");
int port = 10010;
DatagramPacket packet = new DatagramPacket(bytes, bytes.length, address, port);
//3.发送数据
socket.send(packet);
//4.释放数据
socket.close();
}
}
9、UDP协议(接收数据)
package com.leitao.demo.network;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* @Description: TODO
* @Author LeiTao
* @Date 2023/2/15
* @Version V1.0
**/
public class UDPReceiveDemo {
public static void main(String[] args) throws IOException {
//1.创建DatagramSocket对象(快递公司)
/**
* 细节:
* 在接收的时候,一定要绑定端口
* 而且绑定的端口,一定要跟发送的端口保持一致
*/
DatagramSocket datagramSocket = new DatagramSocket(10010);
//2.接收数据包
byte[] bytes = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);
/**
* receive()该方法是阻塞的
* 程序执行到这一步的时候,会在这里死等
* 等发送端发送消息
*/
System.out.println("1111111");
datagramSocket.receive(datagramPacket);
System.out.println("22222222");
//3.解析数据包
byte[] data = datagramPacket.getData();
int length = datagramPacket.getLength();
InetAddress address = datagramPacket.getAddress();
int port = datagramPacket.getPort();
System.out.println("接受数据到" + new String(data,0,length));
System.out.println("该数据是从" + address + "这台电脑中的" + port + "这个端口发出的");
//4.释放资源
datagramSocket.close();
}
}
10、UDP练习(聊天室)
发送端
package com.leitao.demo.network;
import java.io.IOException;
import java.net.*;
import java.util.Scanner;
/**
* @Description: TODO
* @Author LeiTao
* @Date 2023/2/15
* @Version V1.0
**/
public class SendMessageDemo {
public static void main(String[] args) throws IOException {
//1.创建对象DatagramSocket的对象
DatagramSocket ds = new DatagramSocket();
//2.打包数据
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入信息:");
String str = sc.nextLine();
if ("886".equals(str)){
break;
}
byte[] bytes = str.getBytes();
InetAddress address = InetAddress.getByName("127.0.0.1");
int port = 10010;
DatagramPacket dp = new DatagramPacket(bytes, bytes.length, address, port);
//3.发送数据
ds.send(dp);
}
//4.释放资源
ds.close();
}
}
接收端
package com.leitao.demo.network;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* @Description: TODO
* @Author LeiTao
* @Date 2023/2/15
* @Version V1.0
**/
public class ReceiveMessageDemo {
public static void main(String[] args) throws IOException {
//1.创建对象DatagramSocket的对象
DatagramSocket ds = new DatagramSocket(10010);
//2.接收数据包
byte[] bytes = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
while (true) {
ds.receive(dp);
//3.解析数据包
byte[] data = dp.getData();
int length = dp.getLength();
String ip = dp.getAddress().getHostAddress();
String name = dp.getAddress().getHostName();
//4.打印数据
System.out.println("ip为:" + ip + ",主机名为:" + name + "的人,发送了数据:" + new String(data,0,length));
}
}
}
11、单播、组播、广播
组播:创建MulticastSocket对象
12、TCP协议(发送和接收数据)
客户端
package com.leitao.demo.network;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
/**
* @Description: TODO
* @Author LeiTao
* @Date 2023/2/15
* @Version V1.0
**/
public class TCPSendMessageDemo {
public static void main(String[] args) throws IOException {
//TCP协议,发送数据
//1.创建socket对象
/**
* 细节:在创建对象的同事会连接服务端
* 如果连不上,代码会报错
*/
Socket socket = new Socket("127.0.0.1", 10010);
//2.可以从连接通道中获取输出流
OutputStream os = socket.getOutputStream();
//写出数据
os.write("123aaa".getBytes());
//3.释放资源
os.close();
socket.close();
}
}
服务端
package com.leitao.demo.network;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @Description: TODO
* @Author LeiTao
* @Date 2023/2/15
* @Version V1.0
**/
public class TCPReceiveMessageDemo {
public static void main(String[] args) throws IOException {
//TCP协议,接收数据
//1.创建对象ServerSocket
ServerSocket serverSocket = new ServerSocket(10010);
//2.监听客户端的连接
Socket accept = serverSocket.accept();
//3.从连接通道中获取输入流读取数据
InputStream inputStream = accept.getInputStream();
int b;
while ((b = inputStream.read()) != -1) {
System.out.println((char) b);
}
//4.释放资源
accept.close();
inputStream.close();
}
}
13、TCP协议(中文乱码问题)
将字节流转化为字符流即可
package com.leitao.demo.network;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @Description: TODO
* @Author LeiTao
* @Date 2023/2/15
* @Version V1.0
**/
public class TCPReceiveMessageDemo {
public static void main(String[] args) throws IOException {
//TCP协议,接收数据
//1.创建对象ServerSocket
ServerSocket serverSocket = new ServerSocket(10010);
//2.监听客户端的连接
Socket accept = serverSocket.accept();
//3.从连接通道中获取输入流读取数据
InputStream inputStream = accept.getInputStream();
InputStreamReader isr = new InputStreamReader(inputStream);
int b;
while ((b = isr.read()) != -1) {
System.out.println((char) b);
}
//4.释放资源
accept.close();
inputStream.close();
}
}
14、TCP协议(代码细节)
15、三次握手和四次挥手
16、综合练习01(多发多送)
Client 客户端
package com.leitao.demo.network.test1;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;
/**
* @Description: TODO
* @Author LeiTao
* @Date 2023/2/16
* @Version V1.0
**/
public class Client {
public static void main(String[] args) throws IOException {
//1.创建Socket对象并连接服务端
Socket socket = new Socket("127.0.0.1", 10010);
//2.写出数据
Scanner sc = new Scanner(System.in);
OutputStream os = socket.getOutputStream();
while (true) {
System.out.println("请输入您要发送的信息:");
String str = sc.nextLine();
if ("886".equals(str)) {
break;
}
os.write(str.getBytes());
}
//3.释放资源
socket.close();
}
}
Server 服务器
package com.leitao.demo.network.test1;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @Description: TODO
* @Author LeiTao
* @Date 2023/2/16
* @Version V1.0
**/
public class Server {
public static void main(String[] args) throws IOException {
//1.创建对象绑定10010端口
ServerSocket ss = new ServerSocket(10010);
//2.等待客户端来连接
Socket socket = ss.accept();
//3.读取数据
InputStreamReader isr = new InputStreamReader(socket.getInputStream());
int b;
while ((b = isr.read()) != -1) {
System.out.print((char) b);
}
//4.释放资源
socket.close();
ss.close();
}
}
结果
17、综合练习02(接收并反馈)
代码分为两大部分,客服端往服务器发送数据吗,数据的流向要清楚;回写的数据流向也要明白
还需要客户端写一个结束标记,不然客户端read方法会一直等待接收数据
客户端
package com.leitao.demo.network.test2;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
/**
* @Description: TODO
* @Author LeiTao
* @Date 2023/2/20
* @Version V1.0
**/
public class Client {
public static void main(String[] args) throws IOException {
//1.创建Socket对象并连接服务器
Socket socket = new Socket("127.0.0.1",10010);
//2.写出数据
String str = "你好,我是帅哥";
OutputStream os = socket.getOutputStream();
os.write(str.getBytes());
//写出一个结束标记
socket.shutdownOutput();
//3.接收服务端回写的数据
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int b;
while ((b = isr.read()) != -1) {
System.out.print((char)b);
}
//释放资源
socket.close();
}
}
服务器
package com.leitao.demo.network.test2;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @Description: TODO
* @Author LeiTao
* @Date 2023/2/20
* @Version V1.0
**/
public class Server {
public static void main(String[] args) throws IOException {
//1.创建对象并绑定端口
ServerSocket ss = new ServerSocket(10010);
//2.等待客户端连接
Socket socket = ss.accept();
//3.socket 中获取输入流读取数据
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
int b;
/**
* 细节:
* read方法会从连接通道中读取数据
* 但是,需要有一个结束标记,此处的循环才会停止
* 否则,程序就会一直停在read方法这里,等待读取接下来的数据
*/
while ((b = isr.read()) != -1) {
System.out.print((char)b);
}
//4.回写数据
String str = "很帅吗";
OutputStream os = socket.getOutputStream();
os.write(str.getBytes());
//释放资源
socket.close();
ss.close();
}
}
18、综合练习03(上传文件)
。。。。。。