计算机网络
-
将地理位置不同的具有独立功能的多台计算机及其设备, 通过通信线路连接起来, 在网络操作系统, 网络管理软件及网络通信协议的管理和协调下, 实现资源共享和信息传递的计算机系统
网络版的目的
-
传播交流信息
-
数据交换
-
通信
如何定位到主机
-
IP+端口号+资源地址
-
传输介质:
通信的要素
-
双方ip和端口号
-
通信协议: udp, tcp
主要问题
-
准确定位主机地址
-
如何通信
IP
-
inetAddress
分类
-
ipv4 / ipv6
-
ipv4: 127.0.0..1 4个字节组成 0~255
-
ipv6 128位, 8个无符号整数
2001: 0bb2: aaaa: 0015:0000:0000:1aaa:1312
-
-
公网(互联网)-私网(局域网)
域名
-
记忆ip
import java.net.InetAddress;
import java.net.UnknownHostException;
public class MyTestIp
{
public static void main(String[] args) {
try {
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
// /127.0.0.1
InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress2);
// www.baidu.com/180.101.49.12
InetAddress inetAddress3 = InetAddress.getLocalHost();
System.out.println(inetAddress3);
// 陈耀泽/192.168.211.110
//常用方法
System.out.println(inetAddress2.getAddress());// [B@1b6d3586
System.out.println(inetAddress2.getCanonicalHostName());// 180.101.49.11
System.out.println(inetAddress2.getHostAddress());// 180.101.49.11
System.out.println(inetAddress2.getHostName());// www.baidu.com
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
端口
-
一个端口对应一个程序的进程
-
0 -65535
-
tcp,udp 65535*2 单个协议上端口上不能重复
分类
公有端口0-1023
-
http:80
-
https:443
-
ftp: 21
-
telent: 23
程序注册端口:1024-49151 分配用户或程序
-
tomcat: 8080
-
mysql: 3306
-
oracle: 1521
动态, 私有 49152-65535
-
netstat -ano 查看所有端口
-
netstat -ano | findstr "5900" 查看指定端口
-
tasklist | findstr "8696" 查看端口对应进程
import java.net.InetSocketAddress;
public class TestSocket {
public static void main(String[] args) {
InetSocketAddress socketAdress = new InetSocketAddress("127.0.0.1", 8080);
System.out.println(socketAdress);
System.out.println(socketAdress.getAddress());
System.out.println(socketAdress.getHostName());
System.out.println(socketAdress.getPort());
/*
/127.0.0.1:8080
/127.0.0.1
www.sublimetext.com
8080
*/
}
}
通信协议
-
约定
-
速率, 传输码率, 代码结构, 传输控制....
-
TCP/IP协议
-
TCP: 用户传输协议
-
UDP:用户数据报协议
-
IP: 网络互连协议
-
TCP: 打电话
-
连接, 稳定
-
三次握手, 四次挥手
-
三次握手
-
最少需要三次, 保证稳定的连接
A: 你愁啥?
B: 瞅你咋地?
A: 干一场!
-
-
四次挥手
-
A: 我要走了!
B: 你真的要走了吗?
B: 你真的真的要走了吗?
A: 我真的要走了!
-
-
-
客户端, 服务端
-
传输完成, 释放连接, 效率低
UDP: 发短信
-
不连接, 不稳定
-
客户端, 服务端 没有明确的接线
-
不管有没有准备好, 都可以发给你
-
导弹
-
DDOS: 洪水攻击 (饱和攻击)
TCP聊天室
客户端
-
连接服务器 Socket
-
发送消息
package com.cyz;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TcpClientDemo01 {
public static void main(String[] args) {
Socket socket = null;
OutputStream os =null;
try {
// 要知道服务器地址
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
// 创建连接
socket = new Socket(serverIP,port);
// 发送消息 IO流
os = socket.getOutputStream();
os.write("您好啊!!!".getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务端
-
建立服务的端口 ServerSocket
-
等待用户的连接 accept
-
接收用的消息7
package com.cyz;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpServerDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
// 有一个地址
serverSocket = new ServerSocket(9999);
//等待客户连接
while (true) {
socket = serverSocket.accept();
//读取信息
is = socket.getInputStream();
// 管道流
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
System.out.println(baos.toString());
}
} catch (IOException 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();
}
}
}
}
}
文件上传
package com.cyz;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClientDemo02 {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
FileInputStream fis = null;
InputStream inputStream = null;
ByteArrayOutputStream baos = null;
try {
// 建立连接
socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
// 创建一个输出流
os = socket.getOutputStream();
// 读取文件
fis = new FileInputStream(new File("timg.jpg"));
// 写出文件
byte[] buffer = new byte[1024];
int len;
while((len=fis.read(buffer))!=-1){
os.write(buffer,0,len);
}
// 通知服务器接收结束
socket.shutdownOutput();//传输完成
// 确定 服务端就收完毕
inputStream = socket.getInputStream();
baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2;
while((len2=inputStream.read(buffer2))!=-1){
baos.write(buffer2,0,len2);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
}finally {
if(baos!=null){
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os!=null){