网络编程
1.1、概述
信件:
计算机网络:
计算机网络是将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信电路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。
网络编程的目的:
无线电台,传播交流信息,数据交换,通信。
想要达到需要:
如何准确的定位网络上的一台主机? ip:192.168.16.124, 端口,定位到这个计算机上的某个资源
找到了主机,如何传输数据?
javaweb: 网页编程 B/S 网络编程:TCP/IP C/S
1.2、网络通信的要素
如何实现网络的通信?
通信双方的地址:
- ip 唯一
- 端口号
- 192.168.16.124:5900 IP加端口可以定位到计算机上具体的应用
规则:网络通信的协议
TCP/IP参考模型:
主要:
小结:
- 网络编程中有两个主要的问题
- 如何准确的定位到网络上的一台或者多台主机
- 找到主机之后如何进行通信
- 网络编程中的要素
- IP和端口号
- 网络通信协议
- 万物皆对象
- IP 类
- udp,tcp类
1.3、IP
ip地址:InetAddress
-
ip用途:唯一定位一台网络上的计算机
-
127.0.0.1 :本机 英文名:localhost
-
ip地址的分类
-
ipv4/ipv6
-
IPV4 127.0.0.1 , 4个字节组成。 每个字节长度 0~255
一共有42亿;30亿都在北美,亚洲4亿,2011年就用尽
-
IPV6 fe80::1960:84b5:30cd:6eb%16 128位。8个无符号整数
举个例子:2001:0bb2:aaaa:0015:0000:0000:1aaa:1312
-
-
公网(互联网使用)-私网(局域网)
-
ABCD类地址
- 192.168.xx.xx 局域网,专门给组织内部使用的
-
-
域名:解决记忆IP问题
- IP:www.vip.com 域名越短越贵
//ctrl+alt+t public class TestInetAddress { public static void main(String[] args) { try { //查询本机地址 InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1"); System.out.println(inetAddress1); InetAddress inetAddress2 = InetAddress.getByName("localhost"); System.out.println(inetAddress2); InetAddress inetAddress3 = InetAddress.getLocalHost(); System.out.println(inetAddress3); //查询网站ip地址 InetAddress inetAddress4 = InetAddress.getByName("www.baidu.com"); System.out.println(inetAddress4); //常用方法 //System.out.println(inetAddress4.getAddress()); 返回一组字节地址 System.out.println(inetAddress4.getCanonicalHostName()); //规范的名字 System.out.println(inetAddress4.getHostAddress()); //ip System.out.println(inetAddress4.getHostName()); //域名,或者自己电脑的名字 } catch (UnknownHostException e) { throw new RuntimeException(e); } } }
结果是
1.4、端口
端口表示计算机上的一个程序的进程 PID:进程id
-
不同的进程有不同的端口号!用来区分软件!
-
被规定 0~65535
-
TCP,UDP:两个协议各占65535个端口数 所以端口总数65535 * 2 tcp: 80,udp: 80不会冲突,但是一个协议有两个应用都想占用80就会冲突
-
端口分类
-
公有端口 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" #查看指定端口的进程
//Socket 套 接 字 也就是IP:端口 public class TestInetSocketAddress { public static void main(String[] args) { InetSocketAddress socketAddress = new InetSocketAddress("",8080); InetSocketAddress socketAddress2 = new InetSocketAddress(8080); System.out.println(socketAddress); System.out.println(socketAddress2); System.out.println(socketAddress.getAddress()); System.out.println(socketAddress.getHostName()); System.out.println(socketAddress.getPort()); } }
-
1.5、通信协议
协议:约定 可以交流 就好比我们说的都是普通话
**网络通信协议:**速率,传输码率,代码结构,传输控制。。。 问题:非常复杂 怎么处理? 分层
TCP/IP协议簇 不止两个协议
重要:
- TCP:用户传输协议
- UDP:用户数据报协议
出名的协议:
- TCP:
- IP:网络传输协议
TCP UDP对比
TCP:打电话
-
连接,稳定
-
三次握手 四次挥手
三次握手 至少需要三次,保证稳定连接 A:你瞅啥? B:瞅你咋地? A:干一场 四次挥手 A:我不发了(关闭发送) B:好,那我不收了(关闭接收) B:我也不发了(关闭发送) A:好,那我也不收了(关闭接收)
-
客户端、服务端连接
-
传输完成,释放连接,效率低
UDP:发短信
- 不连接,不稳定
- 客户端、服务端:没有明确的界限
- 不管有没有准备好,都可以发给你
- 导弹
- DDOS:洪水攻击!(饱和攻击)
1.6、TCP
客户端
- 连接服务器Socket
- 发送消息
服务器端
- 建立服务的端口 ServerSocket
- 等待用户的连接 accept
- 接收有用的消息
客户端代码
public class TcpClientDemo01 {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
//要知道服务器的IP 端口
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
//创建一个socket 连接
socket = new Socket(serverIP, port);
//发送消息 IO流
os = socket.getOutputStream();
os.write("你好".getBytes());
} catch (Exception e) {
throw new RuntimeException(e);
}finally {
if (os != null){
try {
os.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
};
if (socket != null){
try {
socket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
};
}
}
}
服务器端代码
public class TcpServerDemo01 {
public static void main(String[] args){
ByteArrayOutputStream baos = null;
InputStream is = null;
Socket socket = null;
ServerSocket serverSocket = null;
try {
//创建一个端口
serverSocket = new ServerSocket(9999);
//等待客户端连接
socket = serverSocket.accept();
//读取客户端的消息
is = socket.getInputStream();
/*
这种写法不好 如果传输过来中文子符超过了给定的大小 会出现乱码
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) != -1){
String s = new String(buffer, 0, lenth);
System.out.println(s);
}
* */
//管道流读取
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) != -1){
baos.write(buffer,0,length);
}
System.out.println(baos.toString());
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
if (baos != null){
try {
baos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (is != null){
try {
is.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (socket != null){
try {
socket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
if (serverSocket != null){
try {
serverSocket.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
}
先运行服务器在运行客户端,结果为
文件上传
客户端代码
public class TcpClientDemo02 {
public static void main(String[] args) throws Exception{
//1.创建一个Socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9000);
//2.创建一个输出流
OutputStream os = socket.getOutputStream();
//3.读取文件
FileInputStream fis = new FileInputStream("1.jpg");
//4.写出文件
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) != -1){
os.write(buffer,0,length);
}
//通知服务器,我已经结束了 如果没有通知不会结束
// 因为上面的代码输出完就执行下面的输入代码 有可能自己传给自己
socket.shutdownOutput();
//确定服务器接收完毕,才能够断开连接
InputStream is = socket.getInputStream();
//不确定收到多少字符 使用管道
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int length2;
while ((length2 = is.read(buffer)) != -1){
baos.write(buffer,0,length2);
}
System.out.println(baos.toString());
//关闭资源
fis.close();
os.close();
socket.close();
}
}
服务器端代码
public class TcpserverDemo02 {
public static void main(String[] args) throws Exception{
//1. 创建服务
ServerSocket serverSocket = new ServerSocket(9000);
//2.监听客户端的连接
Socket socket = serverSocket.accept();
//3.获取输入流
InputStream is = socket.getInputStream();
//文件输出 FileOutputStream的相对路径是从该项目文件夹开始的 不是当前类所在的路径
FileOutputStream fos = new FileOutputStream("2.jpg");
byte[] buffer = new byte[1024];
int lentgh;
while ((lentgh = is.read(buffer)) != -1){
fos.write(buffer,0,lentgh);
}
//通知客户端已经接受完毕
OutputStream os = socket.getOutputStream();
os.write("我已经接收完了".getBytes());
//关闭资源
os.close();
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
结果是
Tomcat
服务端
- 自定义 s
- Tomcat服务器 s
客户端
- 自定义 c
- 浏览器 b
1.7、UDP
发送消息
发送端代码
public class UdpClientDemo01 {
public static void main(String[] args) throws Exception {
//建立一个Socket
DatagramSocket socket = new DatagramSocket();
//2.建个包
String msg = "你好啊";
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
//数据,数据的长度起始,要发给谁
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length,localhost,port);
//3.发送包
socket.send(packet);
//4.关闭流
socket.close();
}
}
接收端代码
public class UdpServerDemo01 {
public static void main(String[] args) throws Exception {
//开放端口
DatagramSocket socket = new DatagramSocket(9090);
//接收数据包
byte[] buffer = new byte[9];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet); //阻塞接收
System.out.println(packet.getAddress().getHostName());
System.out.println(new String(packet.getData()));
//关闭连接
socket.close();
}
}
接收结果为
咨询
循环发送消息代码
public class UdpSendDemo01 {
public static void main(String[] args) throws Exception{
DatagramSocket socket = new DatagramSocket(8080);
//准备数据
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
String data = reader.readLine();
byte[] receive = data.getBytes();
//建立一个包
InetAddress localhost = InetAddress.getByName("localhost");
int port = 6666;
DatagramPacket packet = new DatagramPacket(receive, 0, receive.length, localhost, port);
//发送包
socket.send(packet);
if (data.equals("bye")){
break;
}
}
//关闭
socket.close();
}
}
循环接受
public class UdpReceiveDemo01 {
public static void main(String[] args) throws Exception{
DatagramSocket socket = new DatagramSocket(6666);
while (true) {
//接收包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet); //阻塞时接受包裹
byte[] data = packet.getData();
String receiveData = new String(data, 0, packet.getLength());
System.out.println(receiveData);
if (receiveData.equals("bye")){
break;
}
}
socket.close();
}
}
在线咨询:两个人都是发送方也都可以是接收方(多线程)
工具代码
发送
public class TalkSend implements Runnable{
DatagramSocket socket = null;
BufferedReader reader = null;
private int toPort;
private String toIP;
private int fromIP;
public TalkSend(int fromIP,int toPort,String toIP) {
this.fromIP = fromIP;
this.toPort = toPort;
this.toIP = toIP;
try {
socket = new DatagramSocket(fromIP);
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void run() {
try {
while (true) {
String data = reader.readLine();
byte[] receive = data.getBytes();
InetAddress localhost = InetAddress.getByName(this.toIP);
DatagramPacket packet = new DatagramPacket(receive, 0, receive.length, new InetSocketAddress(this.toIP,this.toPort));
socket.send(packet);
if (data.equals("bye")){
break;
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
socket.close();
}
}
接收工具
public class TalkReceive implements Runnable{
DatagramSocket socket = null;
private String msgFrom;
private int port;
public TalkReceive(int port,String msgFrom) {
this.port = port;
this.msgFrom = msgFrom;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
throw new RuntimeException(e);
}
}
@Override
public void run() {
while (true) {
try {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
byte[] data = packet.getData();
String receiveData = new String(data, 0, packet.getLength());
System.out.println(this.msgFrom + ":" + receiveData);
if (receiveData.equals("bye")){
break;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
socket.close();
}
}
执行类老师和学生交流
学生
public class TalkStudent {
public static void main(String[] args) {
//开启两个线程
new Thread(new TalkSend(7777,9999,"localhost")).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
}
老师
public class TalkTeacher {
public static void main(String[] args) {
//开启两个线程
new Thread(new TalkSend(5555,8888,"localhost")).start();
new Thread(new TalkReceive(9999,"学生")).start();
}
}
输出结果
1.8、URL
https://www.baidu.com/
统一资源定位符:定位资源的,定位互联网上的某一个资源
DNS 域名解析 将域名www.baidu.com 解析成 IP xxx.xxx.xxx.xxx
协议://ip地址:端口/项目名/资源 //可以少 不能多
public class URLDemo01 {
public static void main(String[] args) throws Exception{
URL url = new URL("https://localhost:8080/helloWorld/index.jsp?username=xxx&password=123");
System.out.println(url.getProtocol()); //获取协议
System.out.println(url.getHost()); //获取主机名
System.out.println(url.getPort()); //获取接口
System.out.println(url.getPath()); //获取文件
System.out.println(url.getFile()); //获取全路径
System.out.println(url.getQuery()); //获取参数
}
}
输出为