网络的七层
网络层:ip协议(IPV4,IPV6) 传输层:TCP协议和UDP协议 应用层: HTTP协议
TCP和UDP的区别
维度 | TCP | UDP |
---|
可靠性 | 可靠 | 不可靠 | 速度 | 稍慢 | 更快 | 通信方式 | 点对点 | 点对点、一对多、多对多 | 通信内容 | 字节码 | 数据报 | 应用场景 | 传输文件 | 聊天、视频、语音 |
三次握手和四次挥手
TCP(传输控制协议 Transfer Control Protocol)
建立TCP连接需要通过三次握手机制在服务端和客户端之间建立连接
三次握手(Three-way Handshake)其实就是指建立一个TCP连接时,需要客户端和服务器总共发送3个包。进行三次握手的主要作用就是为了确认双方的接收能力和发送能力是否正常、指定自己的初始化序列号为后面的可靠性传送做准备。实质上其实就是连接服务器指定端口,建立TCP连接,并同步连接双方的序列号和确认号,交换TCP窗口大小信息。
三次握手:
- 客户端发送信号量SYN=1 和序列号seq=J 给服务端,客户端进入SYN_SENT(信号发送状态),等待服务器回答
- 服务器收到客户端信号后,发送信号量SYN=1、ACK=1,应答值ack=J+1,新的序列号seq=K给客户端,服务器进入SYN_RCVD(收到信号)状态
- 客户端收到服务器的应答消息,发送ACK=1,和应答值ack=K+1给服务器,客户端和服务器都进入ESTABLISHED(连接建立)状态
在socket编程中,客户端执行connect()时,将触发三次握手。
客户端和服务端断开连接需要通过四次挥手
四次挥手:
- 客户端发送信号量FIN M给服务器端,进入FIN_WAIT1状态
- 服务端收到后,返回ack M+1给客户端,进入CLOSE_WAIT状态
- 服务端在发送FIN N信号给客户端
- 客户端收到后,返回应答信号ACK=1 ack=N+1给服务端,连接关闭
在socket编程中,任何一方执行close()操作即可产生挥手操作。
Socket编程
Socket(套接字)基于TCP/IP协议的网络编程机制
服务端
ServerSocket类 ,用于对本机的某个端口进行监听
- 创建
ServerSocket(int port)port是端口号 - 方法
close() 关闭连接 Socker accept() 接收客户端的Socket,会阻塞线程直到有客户端连接
客户端
Socket类,代表客户端向服务端发送连接,进行网络通知
-
创建 Socket(String ip,int port) 通过ip和端口连接服务端 -
方法 disconnect() 关闭 OutputStream getOutputStream() 获得输出流 InputStream getInputStream() 获得输入流 注意:IO流关闭后,socket会自动关闭
public class Server {
public static final int PORT = 8888;
public void start() {
System.out.println("服务器启动了!");
try(ServerSocket serverSocket = new ServerSocket(PORT)){
while(true) {
Socket socket = serverSocket.accept();
System.out.println("客户端连接了!" + socket.getInetAddress());
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
String str = in.readUTF();
System.out.println("接收消息:" + str);
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.writeUTF("服务器给你问好了!!");
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Server().start();
}
}
public class Client {
public void send(String ip,int port,String msg){
try(Socket socket = new Socket(ip,port)){
ObjectOutputStream out = new ObjectOutputStream(socket.getOutputStream());
out.writeUTF(msg);
out.flush();
ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
String s = in.readUTF();
System.out.println("服务器回应:" + s);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Client().send("192.168.1.113",9999,"Hello,今天学点什么??");
}
}
UDP编程
数据报协议,类似广播,属于不可靠的协议,无连接
服务端
DatagramSocket(int port) 指定端口获得发送的数据报
客户端
DatagramSocket() 作为客户端使用
主要方法
receive(DatagramPacket) 接收数据包
send(DatagramPacket) 发送数据报
DatagramPacket 包
DatagramPacket(String ip,int port) 发送前指定ip和端口
public class UDPServer {
public static final int PORT = 8888;
public void start(){
System.out.println("启动服务");
try {
DatagramSocket server = new DatagramSocket(PORT);
while(true) {
byte[] buff = new byte[1024];
DatagramPacket packet = new DatagramPacket(buff,0,buff.length);
server.receive(packet);
String msg = new String(packet.getData(),0,packet.getLength(),"UTF-8");
System.out.println("接收到:" + msg);
}
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new UDPServer().start();
}
}
public class UDPClient {
public void send(String ip,int port,String msg){
try {
DatagramSocket client = new DatagramSocket();
byte[] buff = msg.getBytes();
DatagramPacket packet = new DatagramPacket(buff,0,buff.length, InetAddress.getByName(ip),port);
client.send(packet);
} catch (SocketException | UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new UDPClient().send("127.0.0.1",8888,"Hello UDP!!");
}
}
例子
1、用TCP连接做一个简易版的聊天室
public class HandleWriteRunnable implements Runnable {
private Socket socket;
public HandleWriteRunnable(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("输入你想说的话");
String msg = scanner.next();
outputStream.writeUTF(msg);
outputStream.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class HandleReadRunnable implements Runnable {
private Socket socket;
public HandleReadRunnable(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
try {
ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
while (true) {
String msg = inputStream.readUTF();
System.out.println(socket.getInetAddress() + msg);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Client {
public void send(String ip, int port) throws IOException {
ExecutorService executorService = Executors.newFixedThreadPool(5);
Socket socket = new Socket(ip, port);
executorService.execute(new HandleReadRunnable(socket));
executorService.execute(new HandleWriteRunnable(socket));
}
public static void main(String[] args) {
try {
new Client().send("192.168.1.141",5000);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Server {
private final int PROT = 5000;
public void start(){
ExecutorService executorService = Executors.newFixedThreadPool(5);
System.out.println("服务器启动了");
try( ServerSocket serverSocket = new ServerSocket(PROT)) {
Scanner scanner = new Scanner(System.in);
while (true){
Socket socket = serverSocket.accept();
System.out.println("客户端连接了"+socket.getInetAddress());
executorService.execute(new HandleWriteRunnable(socket));
executorService.execute(new HandleReadRunnable(socket));
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Server().start();
}
}
2、写一个死锁案例(两个线程都持有对方需要的锁,又有需要对方持有的锁)
public class Demo1 {
Object lock1 = new Object();
Object lock2 = new Object();
public static void main(String[] args) throws InterruptedException {
new Demo1().test1();
}
public void test1() throws InterruptedException {
Thread a = new Thread(()->{
synchronized (lock1){
for (int i = 0; i <1000 ; i++) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
synchronized (lock2){
System.out.println("aaaaaaa");
}
}
});
Thread b = new Thread(()->{
synchronized (lock2){
for (int i = 0; i <1000 ; i++) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
synchronized (lock1){
System.out.println("bbbbbbb");
}
}
});
a.start();
b.start();
}
}
3、写一个线程交替输出案例(A线程循环输出A,B线程循环输出B,出现ABABABAB… )
package com.hopu.Demo;
public class Demo2 {
int num;
public static final Object lock = new Object();
public static void main(String[] args) {
Demo2 demo2 = new Demo2();
new Thread(() -> {
try {
demo2.test1(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "A").start();
new Thread(() -> {
try {
demo2.test1(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "B").start();
}
public void test1(int number) throws InterruptedException {
for (int i = 0; i < 10; i++) {
synchronized (lock) {
while (num % 2 != number) {
lock.wait();
}
num++;
System.out.println(Thread.currentThread().getName());
lock.notifyAll();
}
}
}
}
|