1.1 OSI七层网络模型:
-
网络编程中两个主要问题
- 如何准确的定位到网络上的一台或多台主机
- 找到主机之后如何进行通信
-
网络编程中的要素
- IP和端口号
- 网络通信协议
-
万物皆对象
1.2 IP
IP地址:InetAddress
-
唯一定位一台网络上计算机
-
127.0.0.1:本机localhost
-
ip地址的分类
-
ipv4/ipv6
-
IPV4 127.0.01 ,4个字节组成,0-255,42亿~;
-
IPV6 128位。8个无符号整数!
2001:0bb2:aaaa:0115:0000:0000:laaa:1312
-
-
公网(互联网)-私网(局域网)
- A类B类C类D类
- 192.168.xx.xx 专门给组织内部使用的
-
-
域名:记忆IP问题!
1.3 端口
端口表示计算机上的一个程序的进程;
-
不同的进程有不同的端口号!用来区分软件
-
被规定0-65535
-
TCP,UDP
-
端口分类
-
公有端口 0-1023
- HTTP:80
- HTTPS:43
- FTP:21
- Telent:23
-
程序注册端口:1024-49151,分配用户或者程序
- Tomcat: 8080
- Mysql: 3306
- Ocrale:1521
-
动态、私有:49152-65535
netstart -ano #查看所有的端口 netstart -ano|findstr "5900" #查看指定的端口
-
1.4 通信协议
**网络通信协议:**速率,传输码率,代码结构,传输控制…
TCP/IP协议簇
重要:
- TCP: 用户传输协议
- UDP: 用户数据报协议
出名的协议:
- TCP:
- IP:网络网络互联协议
TCP UDP对比
TCP:打电话
-
连接稳定
-
三次握手,四次挥手
最少需要三次,保证稳定连接! A:你瞅啥? B:瞅你咋地 A:干一场! A:我要断开了 B:我知道你要断开了 B:你真的要断开了吗 A:我真的要断开了
-
客户端、服务端
-
传输完成,释放连接,效率低
UDP:发短信
- 不连接,不稳定
- 客户端、服务端:没有明确的界限
- 不管有没有准备好,都可以发给你
- 导弹
- DDOS:洪水攻击!饱和攻击
1.5 TCP
客户端
- 连接服务器Socket
- 获取服务器地址、端口号
- 创建一个Socket连接
- 发送消息
public class TcpClientDemo1 {
public static void main(String[] args) {
OutputStream os = null;
Socket socket = null;
//1.获取服务器地址、端口号
try {
InetAddress ServerIp = InetAddress.getByName("127.0.0.1");
int port = 9999;
//2.创建一个Socket连接
socket = new Socket(ServerIp, port);
//3.使用IO流输出消息
os = socket.getOutputStream();
os.write("你好".getBytes());
} catch (Exception e) {
e.printStackTrace();
}
finally {
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务端
- 建立服务的接口ServerSocket
- 等待用户的连接accept
- 接受用户的消息
public class TcpServerDemo1 {
public static void main(String[] args) {
ByteArrayOutputStream baos = null;
InputStream is = null;
Socket socket = null;
ServerSocket serverSocket = null;
//1.设置一个地址
try {
serverSocket = new ServerSocket(9999);
//2.等待服务器连接
while (true) {
socket = serverSocket.accept();
//3.接收消息
is = socket.getInputStream();
//管道流
baos = new ByteArrayOutputStream();
byte[] buffers = new byte[1024];
int len = 0;
while((len = is.read(buffers)) != -1){
baos.write(buffers,0,len);
}
System.out.println(baos.toString());
}
/* byte[] buffers = new byte[1024];
int len = 0;
while ((len = is.read(buffers)) != -1 ){
String msg = new String(buffers,0,len);
System.out.println(msg);
}*/
} catch (Exception e) {
e.printStackTrace();
}
finally {
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
文件上传
客户端
public class TcpClientDemo2 {
public static void main(String[] args) throws Exception{
//1.获取ip地址,端口号
InetAddress serverIp = InetAddress.getByName("127.0.0.1");
int port = 9000;
//2.创建socket连接
Socket socket = new Socket(serverIp,port);
//3.创建输出流
OutputStream os = socket.getOutputStream();
//4.读取文件
FileInputStream fis = new FileInputStream("miku.jpg");
//5.写出文件
byte[] buffer = new byte[1024];
int len = 0;
while((len = fis.read(buffer)) != -1){
os.write(buffer,0,len);
}
//通知服务器已经传输完了
socket.shutdownOutput();
//确认服务器接收完毕
InputStream is = socket.getInputStream();
//创建管道流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer1 = new byte[1024];
int len1;
while((len = is.read(buffer1)) != -1){
baos.write(buffer1,0,len);
}
System.out.println(baos.toString());
//6.关闭流
fis.close();
os.close();
socket.close();
}
}
服务端
public class TcpServerDemo2 {
public static void main(String[] args) throws IOException {
//1.创建一个地址
ServerSocket serversocket = new ServerSocket(9000);
//2.等待监听
Socket socket = serversocket.accept();
//3.创建输入流
InputStream is = socket.getInputStream();
//4.文件输出
FileOutputStream fos = new FileOutputStream("receive.jpg");
byte[] buffer = new byte[1024];
int len = 0;
while((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
//通知客户端已经接受完毕
OutputStream os = socket.getOutputStream();
os.write("接受完毕".getBytes());
fos.close();
is.close();
socket.close();
serversocket.close();
}
}
Tomcat
服务端
- 自定义S
- Tomcat服务器 S : Java后台
客户端
-
自定义C
-
浏览器B
1.6 UDP
发短信:不用连接,需要知道别人的地址
发送端
public class UdpClientDemo1 {
public static void main(String[] args) throws Exception {
//1.建立socket连接
DatagramSocket socket = new DatagramSocket();
//2.建包
byte[] buffer = new byte[1024];
String str = "你好服务端";
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
DatagramPacket datagramPacket = new DatagramPacket(str.getBytes(), 0, str.getBytes().length, localhost, port);
//3.发送包
socket.send(datagramPacket);
//4.关闭流
socket.close();
}
}
接收端
public class UdpServerDemo1 { public static void main(String[] args) throws Exception { //1.开端口 DatagramSocket socket =new DatagramSocket(9090); //2.接收包 byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length); socket.receive(packet); System.out.println(packet.getAddress().getHostAddress()); System.out.println(new String(packet.getData(),0,packet.getLength())); //3.关闭流 socket.close(); }}
1.7.URL
统一资源定位符:定位资源的,定位互联网上的某一个资源.
协议://ip地址:端口/项目名/资源