计算机网络
计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通讯协议的管理和协调下,实现资源共享和信息传递的计算机系统。
javaweb:网页编程 B/S
网络编程: TCP/IP C/S
1.2网络通信的要素
如何实现网络的通信?
通信双方地址
- ip
- 端口号
规则:网络通信的协议
TCP/IP参考模型:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-V4gBstN9-1611078500579)(C:\Users\15512\AppData\Roaming\Typora\typora-user-images\image-20210116233320797.png)]
小结:
- 网络编程中有两个主要问题
-
- 如何准确的定位带网络上的一台或者多台主机
- 找到主机之后如何进行通信
- 网络编程中的要素
-
- IP和端口号 IP
- 网络通信协议:udp和tcp
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LIwlXWck-1611078500583)(C:\Users\15512\AppData\Roaming\Typora\typora-user-images\image-20210116233616766.png)]
1.3、IP
ip地址:InetAddress
-
唯一定位一台网络上的计算机
-
127.0.0.1:本机localhost
-
ip地址的分类
-
-
ipv4/ipv6
-
-
ipv4:127.0.0.1 ,四个字节组成,0~255
-
ipv6:128位。8个无符号整数!
2001: 0bb2:aaaa:0015:0000:0000:1aaa:1312!
-
-
公网(互联网)-私网(局域网)
- ABCD类地址
- 192.168.xx.xx,专门给组织内部使用的(0~255 折断 )
-
-
域名:记忆IP的问题!
package cn.org.mubai.ip01;
import java.net.InetAddress;
import java.net.UnknownHostException;
//测试IP
public class TestIp01 {
public static void main(String[] args) {
try {
InetAddress i2=InetAddress.getByName("localhost");
System.out.println(i2);
InetAddress i3=InetAddress.getLocalHost();//返回本机当前ip
System.out.println(i3);
//查询网站IP地址
InetAddress i1=InetAddress.getByName("www.mubai.org.cn");
System.out.println(i1);
System.out.println(i1.getHostAddress());//ip
System.out.println(i1.getCanonicalHostName());//规范的名字
System.out.println(i1.getHostName());//域名或者自己电脑的名字
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
/*
localhost/127.0.0.1
LAPTOP-QCLG1LR1/192.168.184.1
www.mubai.org.cn/121.196.155.165
121.196.155.165
mubai
www.mubai.org.cn*/
1.4、端口
端口表示计算机上的一个程序的进程;
-
不同的进程有不同的端口号!用来区分软件
-
被规定0~65536
-
TCP,UTP: 65536*2 ,单个协议下,端口号不能冲突
-
端口分类
-
公有端口0~1023
- HTTP:百度80
-
程序注册端口:1024~49151,分配用户或者程序
-
动态,私有:49152~65535
netstat -ano#查看所有的端口 netstat -ano|findstr "8080" #查看指定的端口 tasklist|findstar "8080" #查看指定端口的进程
package cn.org.mubai.ip01; import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput; import java.net.InetSocketAddress; public class TestIp02 { public static void main(String[] args) { InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8080); InetSocketAddress socketAddress2 = new InetSocketAddress("localhost", 8080); System.out.println(socketAddress); System.out.println(socketAddress2); System.out.println(socketAddress.getAddress()); System.out.println(socketAddress.getHostName());//地址 System.out.println(socketAddress.getPort());//端口 } } /127.0.0.1:8080 localhost/127.0.0.1:8080 /127.0.0.1 127.0.0.1 8080
-
1、5通信协议
协议: 约定
**网络通信协议:**速率,传输码率,代码结构,传输控制。。。。
**TCP/IP协议簇:**实际是一组协议
重要:
- TCP:用户传输协议
- UDP:用户数据报协议
- IP:网络互连协议
TCP UDP 对比
TCP:打电话
-
连接,稳定
-
三次握手四次挥手 -
A:你瞅啥? B:瞅你咋地 A:干一架 A:我要走了 B:你要走了吗? B:你真的真的要走了吗 A:我真的要走了
-
客户端、服务端
-
传输完成,释放连接,效率低
UDP:发短信
- 不连接,不稳定
- 客户端、服务端:没有明显的界限、
- 不管有没有准备好,都发给你。。。
- DDOS:洪水攻击!
1、6TCP
客户端
1连接服务器Socket
2发送消息
package cn.org.mubai.ip01;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TestTcpClient01 {
//1.要知道服务器地址
public static void main(String[] args) {
Socket socket=null;
OutputStream os=null;
try {
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
//2端口号
int port=9999;
//创建一个socket连接
socket = new Socket(serverIP,port);//将ip地址和端口号
// 3.发送消息 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();
}
}
}
}
}
服务器
1建立服务端口serverSocket
2等待用户的连接accept
3接收消息
package cn.org.mubai.ip01;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TestTcpServer01 {
// 我得有一个地址
public static void main(String[] args) {
ServerSocket serverSocket=null;
Socket socket=null;
InputStream is=null;
ByteArrayOutputStream bos=null;
try {
serverSocket = new ServerSocket(9999);
// 等待客户端连接过来
socket=serverSocket.accept();
// 读取客户端的消息
is = socket.getInputStream();
// 管道流
bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while((len=is.read(buffer))!=-1){
bos.write(buffer,0,len);
}
System.out.println(bos.toString());
} catch (IOException e) {
e.printStackTrace();
}finally {
if (bos!=null) {
try {
bos.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 cn.org.mubai.ip01;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TestClientIp02 {
public static void main(String[] args) throws Exception {
//创建一个Socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
//创建一个输出流
OutputStream os = socket.getOutputStream();
//文件流读取文件
FileInputStream fil = new FileInputStream(new File("C:\\Users\\15512\\Desktop\\mubai.JPG"));
// 写出文件
// 管道流
byte[] buffer=new byte[1024];
int len;
while((len=fil.read(buffer))!=-1){
os.write(buffer,0,len);
}
// 通知服务器结束
socket.shutdownOutput();
// 确定服务器接收完毕才能断开连接
InputStream inputStream = socket.getInputStream();
// String byte[]
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len2;
while((len2=inputStream.read(bytes))!=-1){
byteArrayOutputStream.write(bytes,0,len2);
}
System.out.println(byteArrayOutputStream.toString());
byteArrayOutputStream.close();
inputStream.close();
fil.close();
os.close();
socket.close();
}
}
package cn.org.mubai.ip01;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TestServerIp2 {
public static void main(String[] args) throws Exception {
// 创建服务
ServerSocket serverSocket = new ServerSocket(9000);
// 监听客户端的连接
Socket accept = serverSocket.accept();//阻塞式监听,会一直等待客户端连接
// 获取输入流
InputStream is = accept.getInputStream();
// 文件输出
FileOutputStream fos=new FileOutputStream("C:\\Users\\15512\\Desktop\\mubai-org1.JPG");
byte[] bytes = new byte[1024];
int len;
while((len=is.read(bytes))!=-1){
fos.write(bytes,0,len);
}
// 通知客户端我接收完毕
OutputStream os = accept.getOutputStream();
os.write("ok".getBytes());
// 关闭资源
fos.close();
is.close();
accept.close();
serverSocket.close();
}
}
UDP
发短信,不用连接
package cn.org.mubai.ip01;
import java.net.*;
public class TestUdpClient{
public static void main(String[] args) throws Exception {
// 建立一个socket
DatagramSocket socket = new DatagramSocket();
// 建立一个包
String mgs="hello!mubai-org";
InetAddress localhost = InetAddress.getByName("localhost");
int prot=9090;
// 数据,数据的长度起始。要发送给谁
DatagramPacket packet = new DatagramPacket(mgs.getBytes(), 0, mgs.getBytes().length, localhost, prot);
// 发送包
socket.send(packet);
socket.close();
}
}
package cn.org.mubai.ip01;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TestUdpServer {
public static void main(String[] args) throws Exception {
// 开放端口
DatagramSocket socket = new DatagramSocket(9090);
// 接收数据包
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);
socket.receive(packet);//阻塞接收
System.out.println(new String(packet.getData(),0,packet.getData().length));
socket.close();
}
}
多线程实现聊天
package cn.org.mubai.ip01;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class TestClientThread implements Runnable{
DatagramSocket socket=null;
BufferedReader reader=null;
private int myProt;
private String ToIp="localhost";
private int Toprot;
public TestClientThread(int myProt, String toIp, int toprot) {
this.myProt = myProt;
ToIp = toIp;
Toprot = toprot;
try{
socket = new DatagramSocket(this.myProt);
reader = new BufferedReader(new InputStreamReader(System.in));
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void run() {
// 建立一个包
boolean s=true;
while(s){
try {
String mgs=reader.readLine();
InetAddress localhost = InetAddress.getByName(ToIp);
int prot=Toprot;
// 数据,数据的长度起始。要发送给谁
DatagramPacket packet = new DatagramPacket(mgs.getBytes(), 0, mgs.getBytes().length, localhost, prot);
// 发送包
socket.send(packet);
if (mgs.equals("ok")){
s=!s;
}
}catch (Exception e){
e.printStackTrace();
}
}
socket.close();
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package cn.org.mubai.ip01;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class TestServerThread implements Runnable{
DatagramSocket socket=null;
private String mgs=null;
private int port;
public TestServerThread(int port,String mgs) {
this.port=port;
this.mgs=mgs;
try{
socket = new DatagramSocket(this.port);
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public void run() {
boolean s=true;
while(s){
try{
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);
socket.receive(packet);//阻塞接收
System.out.println(mgs+":"+new String(packet.getData(), 0, packet.getData().length));
if ("ok".equals(new String(packet.getData()))) {
s=!s;
}
}catch (Exception e){
e.printStackTrace();
}
}
socket.close();
}
}
package cn.org.mubai.ip01;
public class TestIpThreadTeacher {
public static void main(String[] args) {
Thread thread1 = new Thread(new TestClientThread(9999,"localhost",2020));
Thread thread2 = new Thread(new TestServerThread(9090,"老师"));
thread1.start();
thread2.start();
}
}
---------------------------------------------------------
package cn.org.mubai.ip01;
public class TestIpThreadStudent {
public static void main(String[] args) {
Thread thread1 = new Thread(new TestClientThread(2222,"localhost",9090));
Thread thread2 = new Thread(new TestServerThread(2020,"学生"));
thread1.start();
thread2.start();
}
}
两个人可以是接收方也可以是发送方
url
统一资源定位符:
DNS域名解析
package cn.org.mubai.ip01;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class TestURL {
public static void main(String[] args) throws IOException {
URL url = new URL("https://miao.baidu.com/abdr?data=eyJkYXRhIjoiNDk2NjI0YTE0NjFlNTQ3ZTE2OTA1MDNiNWQ1MWY5NTU3OWU5NjZmNDdhMjljMzU4NzhhOTZlNTdjYzE3M2ZhM2NlNmM3MWVmNGMwMTA5YThhMDU2N2MxNmYwNGYyZGYwMDYyMDU0OTRiMDAwNTVhMTM4NThjNjY0YjI3NDc3Y2Q5NjkwNTUxZDY0MDA2NjRjMDJhOWUxYTk0NjY1ZDg0OGE0YzZmMWYyMjhmZTc0Y2FlN2VmMDBiYzNiYjEwNmFkMGJkZjRmZjA4ZDk3NTYxOTI2OTE2ZWY2YTZmZjY1MjQ2ZGFjMDQ2Y2I4OTE1MjY1ZGZmZjllOTJhNDI1YmU0YWI3MTk1MTE0Mjg1NWI5NDFlY2QzMGJmOGM0NDg1NDFhOWMzMjgyZGVkOGE1NWIyZjQ2MjdhZjFmOGM4NTYzMTQ3MTc0MWU4YzRlZGE3NmE1NWQ5ZmVkNTBmODBiMmQ5MmJkMjZlYjNlNjI5Yzc2ZGY1ODEzZWMzMjBhMjc4YmNlNmZkNzJhYTQzZTk4OGY5YmEzZjc5MGIxZTg2MzFmOTk2Y2RhYWY0Y2IyMjUwNzZlMjk0ZGQ1N2QzNTI5ZDMzOWQyNmJjMzdmYWUwNmJiOWRjM2RlODU0MDU5ZDZlMzM3MDQyODg5ZTQzMmUxNzgxNGE1Y2IxNTQ1NTZkMTUwNjJhNzIwYWVlZmYwMmJiYTBiYTVjNDhjZGVhM2U4MGNhY2MwZWU5MGUyNWQxMWFmYmE2NTkyOTU3M2IzNWUxNjIxMDM0ODI2YmFhYmM4OGE4ZmNkNzgwMzhjNTU0NjMyOTYzYmYzNDNlYWQxNDFjYmU3M2RmZThmYjRkYjhhN2EzNmNjOTA3ODIzYWZjNjMxNzE1M2FiMGFhZWRmNTQ4ZjAxMTdhMjg1NDE5MWFiMjVlZTgyNTI1NmMzYTY1N2JiMWNmODMxY2RjZDc2MDI3NWU5NjI5MWVlMmY4Y2ZjOTI4ZjdjNDY1MjMyNDIwOTk4YTBhOTQwMWVkMzU2NTMxZmYzY2RmZmNkMDE1YzliZTIyOWU3ZjdiODljZjM1MWQ3ZTRhMzIwMzc5NjQ4NDg3ZTM1MTg4Yjg1MDViNTk3MWRlMjBjZmZmYmEwMGIwZDVlNWUwN2U4ZTQzMDI0NzI3YTkxZTgyMDJkNzI4OWM4NTNiYmU5MDA0YTk3OGRhMzNhYzFjMGFjNGE2ZDU0OGQ4YzdjYTEwZWVjMTI5MGM0ZGMyNjg3MTJlNmUxY2NjY2Y3MjliNzM2NzI2MmM1MDEwZTg4NmI0YjJkZTQ3ZGEzODI5MzI4ZGRjNDJlMGY0NjdjOWMiLCJrZXlfaWQiOjIwfQ%3D%3D&_=0.2030399449036786");
// 连接这个HTTP资源
HttpURLConnection openConnection= (HttpURLConnection) url.openConnection();
InputStream inputStream = openConnection.getInputStream();
FileOutputStream out=new FileOutputStream("C:\\Users\\15512\\Desktop\\mu.txt");
byte[] bytes = new byte[1024];
int len;
while((len=inputStream.read(bytes))!=-1){
out.write(bytes,0,len);
}
out.close();
inputStream.close();
openConnection.disconnect();
}
}