目录:
知识架构:
网络模型:
Tcp/IP系列协议
tcp的3次握手协议
Tcp的三次握手的漏洞:
Tcp的4次挥手协议
抓包工具:
三次握手:
TCP的通讯原理:
http协议:
http协议的报文组成
UDP协议:
Tcp通讯代码实战:
package io.renren.modules.tcp;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class IcpServer {
public static void main(String[] args) {
//创建一个serverSocker监听一个端口:8888
try {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("Tcp服务器已经启动,端口8888");
//无限循环
while(true) {
//等待客户端的请求,如果有一个请求分配一个socket
Socket socket = serverSocket.accept();
//根据标准输入构造一个bufferedReader对象
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String buffer = null;
//循环读取输入的每一行数据
while((buffer = reader.readLine()) != null && !"".equals(buffer)) {
System.out.println(buffer);//输出每一行
}
//通过soket对象得到输出流,构造bufferedWrite对象
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
//模拟了http的请求头信息
writer.write("HTTP/1.1 200 ok \r\n Content-Type:text/html \r\n charset=UTF-8 \r\n\r\n ");
//写一些html的体
writer.write("<html><head><title>http请求</title></head><body><h1>这是一个http请求!</h1></body></html>");
//刷新输出流,使数据立马发送
writer.flush();
//关闭
reader.close();
writer.close();
socket.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package io.renren.modules.tcp;
import java.io.PrintWriter;
import java.net.Socket;
public class TcpClient {
public static void main(String[] args) throws Exception{
String msg = "hello 13号技师";
//创建一个socket,跟本机的8888端口进行连接
Socket socket = new Socket("127.0.0.1",8888);
//使用socket创建一个printWriter进行写数据
PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
//发送数据
printWriter.println(msg);
//刷新一下,使得服务器立马收到请求信息
printWriter.flush();
//关闭资源
printWriter.close();
socket.close();
}
}
Tcp效率高,http传输信息多
接收端:
package io.renren.modules.udp;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**Udp接收端
* @author Administrator
*
*/
public class UdpReciveDemo {
public static void main(String[] args) throws Exception{
//创建一个DatafarmSocket实例,并且把实例绑定到本机的地址,端口10005
DatagramSocket datagramSocket = new DatagramSocket(10005);
byte bytes[] = new byte[1024];
//以一个空数组来创建datagrampacket,这个对象作用是接受datagramSoket中的数据
DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);
while(true) {//无限循环,因为不知道数据何时来
//接收到的数据包
datagramSocket.receive(datagramPacket);
//获取接受的数据
byte[] data = datagramPacket.getData();
//把数据转成字符串
String str = new String(data,0,datagramPacket.getLength());
//如果数据包中的数据是88的信息,则跳出并且关闭
if("88".equals(str)) {
break;
}
//打印数据
System.out.println("接收到的数据为:"+str);
}
//关闭资源
datagramSocket.close();
}
}
发送端:
package io.renren.modules.udp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* @author Administrator
* UDP发送端
*/
public class UdpSendDemo {
public static void main(String[] args) throws Exception{
//创建一个datagramSocket实例
DatagramSocket datagramSocket = new DatagramSocket();
//使用键盘输入构建一个bufferedReader
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String line = null;
while((line = bufferedReader.readLine()) != null) {
//转成byte
byte[] bytes = line.getBytes();
//创建一个用于发送datagrampacket对象
DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length, InetAddress.getByName("127.0.0.1"),10005);
//发送数据包
datagramSocket.send(datagramPacket);
if("88".equals(line)) {//当输入88时结束发送
break;
}
}
//关闭
datagramSocket.close();
}
}
总结;
来源:CSDN
作者:FUSIPING
链接:https://blog.csdn.net/FUSIPING/article/details/104044515