2021-1-29----------计算机网络
计算机网络
- 1.1概述
- 1.2网络通信的要素
- 1.3 、 IP
- 1.4端口
- 1.5通信协议
- 1.6、TCp
- 1.7文件上传
- 1.8 Tomcat
- 1.9UDP消息发送
- 2.0 UDP聊天实现
- 2.URL下载网络资源
1.1概述
计算机网络:地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。
网络编程的目的:
无线电台:传播交流信息,数据交换。通信
想要的效果:
- 如何定位到一台主机?端口,定位到这个计算机上的某个资源
- 找到这个主机,如何传输数据?
javaweb:网页编程 B/S
网络编程:TCP/IP C/S
1.2网络通信的要素
如何实现网络的通信?
1,通信的双方地址:
-
ip
-
端口号
-
192.168.16
规则:TCP/IP通信协议
2.网络编程有两个主要的问题
- 如何准确的定位到网络上的一台或者多台主机
- 找到主机后如何进行通信
3.网络编程中的要素
- IP和端口号 IP
- 网络通信协议 udp,tcp
4.主要讲的是传输层:
TCP,UDP
5.万物皆对象
1.3 、 IP
ip地址:InetAddress
-
唯一定位一台网络上计算机
-
127.0.0.1:本机localhost
-
ip地址的分类
1.ipv4/ipv6
ipv4 :127.0.0.1,4个字节组成,0~255,42亿,2011年就用尽了
ipv6:128位。8个无符号整数!abcde(十六位)
2.公网(互联网)-私网(局域网)
192.168.xx.xxx专门给组织内部使用
A类地址:10.0.0.0~10.255.255.255
B类地址:172.16.0.0~172.31.255.255
C类地址:192.168.0.0~192.168.255.255
import java.net.InetAddress;/**
没有构造函数* @author * @date 2021/1/16 - 12:20*/
//测试ip
public class TestInetAddress {public static void main(String[] args) {try {//获取本地ip地址InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");InetAddress inetAddress2 = InetAddress.getByName("localhost");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.getHostAddress());//ipSystem.out.println(inetAddress4.getCanonicalHostName());//规范的名字System.out.println(inetAddress4.getHostName());//域名,或者自己电脑的名字} catch(Exception e) {e.printStackTrace();}}
}
1.4端口
端口表示计算机上的一个程序的进程
-
不同的进程有不同的端口号:用来区分软件
-
被规定为0~65535
-
TCP/UDP 65535*2,单个协议下,端口号不能冲突
-
端口分类:
公有端口
Telent:23
Http:80
HTTPS:443
FTP21
程序注册端口:1024~49151,分配用户或者程序
Tomcat:8080
Mysql:3306
Oracle:1521
动态、私有:49152~65535
netstat -ano #查看所有的端口 netstat -ano | findstr "5900"#查看指定的端口 tasklist|findstr "8696"#查看指定端口
import java.net.InetSocketAddress;/*** @author pcy* @date 2021/1/16 - 15:15*/ public class TestInetSocketAddress {public static void main(String[] args) {InetSocketAddress inetSocketAddress1=new InetSocketAddress("127.0.0.1",8080);InetSocketAddress inetSocketAddress2=new InetSocketAddress("localhost",8080);System.out.println(inetSocketAddress1);System.out.println(inetSocketAddress2);//QICQSystem.out.println(inetSocketAddress1.getAddress());System.out.println(inetSocketAddress1.getHostName());System.out.println(inetSocketAddress1.getPort());//获取端口号} }
1.5通信协议
协议:约定
网络通信协议:速率,传输码率,代码结构,传输控制
TCP/IP协议簇:实际上是一组协议
重要:
TCP:用户传输协议
UDP:用户数据报协议
出名的协议:
TCP:
IP:网络互连协议
TCP和udp对比
TCp:打电话
-
连接、稳定
-
三次握手,四次挥手
双方是有回应的保持稳定性
-
客户端、服务端
-
传输完成,释放连接、效率低
udp:发短信
- 不连接、不稳定
- 客户端、服务端:没有明确的界限
- 不管有没有准确准备好,都可以发给你
- 导弹
- DDOS:洪水攻击!饱和攻击
1.6、TCp
实现聊天
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;/*** @author pcy* @date 2021/1/16 - 15:46*/
//服务端
public class TCPdemo1 {public static void main(String[] args) {//有一个地址ServerSocket serverSocket=null;Socket socket=null;InputStream is=null;ByteArrayOutputStream bais=null;{try {serverSocket = new ServerSocket(9999);while (true) {socket = serverSocket.accept();//读取客户端消息is = socket.getInputStream();//管道流bais = new ByteArrayOutputStream();byte[] bytes = new byte[1024];int len;while ((len = is.read(bytes)) != -1) {bais.write(bytes, 0, len);}System.out.println(bais.toString());}} catch (IOException e) {e.printStackTrace();}finally {if (bais!=null) {try {bais.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();}}}}}}
import com.sun.security.ntlm.Server;import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;/*** @author pcy* @date 2021/1/16 - 15:46*/
//客户端
public class TCPdemo {public static void main(String[] args) {Socket socket = null;OutputStream os = null;//要知道服务器的地址try {InetAddress inetAddress = InetAddress.getByName("localhost");int port = 9999;//创建一个Socket连接socket = new Socket(inetAddress, port);os = socket.getOutputStream();os.write("我爱你啊".getBytes());} catch (IOException 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();}}}}
}
1.7文件上传
服务器端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;/*** @author pcy* @date 2021/1/16 - 17:16*/
public class TcpServerDemo {public static void main(String[] args) throws IOException {//创建服务ServerSocket serverSocket = new ServerSocket(9998);Socket socket=serverSocket.accept();InputStream is = socket.getInputStream();FileOutputStream fops = new FileOutputStream(new File("receive.jpg"));byte[] bytes=new byte[1024];int len;if ((len=is.read(bytes))!=-1){fops.write(bytes,0,len);}//通知客户端器接收完毕OutputStream os = socket.getOutputStream();os.write("我接受完了".getBytes());os.close();fops.close();is.close();socket.close();serverSocket.close();}
}
客户端
import java.io.*;
import java.net.Socket;/*** @author pcy* @date 2021/1/16 - 17:07*/
public class TcpClientDemo1 {public static void main(String[] args) throws IOException {Socket socket=new Socket("127.0.0.1",9998);OutputStream os = socket.getOutputStream();FileInputStream fips = new FileInputStream(new File("3.jpg"));byte[] bytes=new byte[1024];int len;if ((len=fips.read(bytes))!=-1){os.write(bytes,0,len);}//通知服务器接受完毕socket.shutdownOutput();//确定服务器接受完毕,才断开InputStream is=socket.getInputStream();ByteArrayOutputStream baos=new ByteArrayOutputStream();byte[] bytes1=new byte[1024];int len1;if ((len1=is.read(bytes1))!=-1){baos.write(bytes1,0,len1);}System.out.println(baos.toString());baos.close();is.close();fips.close();os.close();socket.close();}
}
1.8 Tomcat
客户端
- 自定义 B
- 浏览器 B
服务端
-
自定义 S
-
Tomcat S
1.9UDP消息发送
发短信:不用连接,需要知道对方的地址
发送消息
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;/*** @author pcy* @date 2021/1/17 - 17:18*/
public class UdpClientDemo {public static void main(String[] args) throws Exception {//建立一个socketDatagramSocket socket = new DatagramSocket();String msg="我喜欢你啊";InetAddress address = InetAddress.getByName("localhost");int port=9090;//建立一个package//第一个是数据,从0开始,数据的长度,ip地址,端口号DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,address,port);//发送包socket.send(packet);//关闭流socket.close();}
}
接受消息
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;/*** @author pcy* @date 2021/1/17 - 17:29*/
//等待客户端的连接
public class UdpServerDemo {public static void main(String[] args) throws IOException {DatagramSocket socket = new DatagramSocket(9090);byte[] bytes=new byte[1024];DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);//接收包socket.receive(packet);System.out.println(packet.getAddress());System.out.println(new String(packet.getData(),0,packet.getLength()));socket.close();}
}
2.0 UDP聊天实现
发送消息
public class UdpSendDemo {public static void main(String[] args) throws IOException {DatagramSocket socket = new DatagramSocket(8081);//读取控制台BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));while (true) {String data = reader.readLine();byte[] bytes = data.getBytes();DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, new InetSocketAddress("localhost", 6060));socket.send(packet);if (data.equals("bye")){break;}}socket.close();}
}
接收消息
public class UdpReceiveDemo {public static void main(String[] args) throws IOException {DatagramSocket socket = new DatagramSocket(6060);while (true) {byte[] bytes = new byte[1024];DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);socket.receive(packet);//断开连接byte[] data = packet.getData();String receiveData = new String(data, 0, data.length);System.out.println(receiveData);if (receiveData.equals("bye")) {break;}}socket.lose();}
}
2.URL下载网络资源
统一资源定位符
DNS域名解析 将www.baidu.com解析成XXX.XXX.XXXX
协议:http等/ip地址,端口/项目,资源
public class UrlDemo {public static void main(String[] args) throws MalformedURLException {URL url = new URL("http://localhost:8080/helloWorld/index.jsp?username=123");System.out.println(url.getHost());//主机名System.out.println(url.getPort());//端口号System.out.println(url.getFile());//全路径System.out.println(url.getPath());//文件路径System.out.println(url.getQuery());//参数名System.out.println(url.getProtocol());//协议}
}
public class UrlDemo {public static void main(String[] args) throws IOException {URL url = new URL("http://localhost:8080/pcy/Security.txt");HttpURLConnection connection = (HttpURLConnection) url.openConnection();InputStream is = connection.getInputStream();FileOutputStream fs = new FileOutputStream("Security.txt");byte[] bytes = new byte[1024];int len;if ((len=is.read(bytes))!=-1){fs.write(bytes,0,len);//写出这个数据}fs.close();is.close();connection.disconnect();}
}
2021-1-29----------计算机网络
计算机网络
- 1.1概述
- 1.2网络通信的要素
- 1.3 、 IP
- 1.4端口
- 1.5通信协议
- 1.6、TCp
- 1.7文件上传
- 1.8 Tomcat
- 1.9UDP消息发送
- 2.0 UDP聊天实现
- 2.URL下载网络资源
1.1概述
计算机网络:地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。
网络编程的目的:
无线电台:传播交流信息,数据交换。通信
想要的效果:
- 如何定位到一台主机?端口,定位到这个计算机上的某个资源
- 找到这个主机,如何传输数据?
javaweb:网页编程 B/S
网络编程:TCP/IP C/S
1.2网络通信的要素
如何实现网络的通信?
1,通信的双方地址:
-
ip
-
端口号
-
192.168.16
规则:TCP/IP通信协议
2.网络编程有两个主要的问题
- 如何准确的定位到网络上的一台或者多台主机
- 找到主机后如何进行通信
3.网络编程中的要素
- IP和端口号 IP
- 网络通信协议 udp,tcp
4.主要讲的是传输层:
TCP,UDP
5.万物皆对象
1.3 、 IP
ip地址:InetAddress
-
唯一定位一台网络上计算机
-
127.0.0.1:本机localhost
-
ip地址的分类
1.ipv4/ipv6
ipv4 :127.0.0.1,4个字节组成,0~255,42亿,2011年就用尽了
ipv6:128位。8个无符号整数!abcde(十六位)
2.公网(互联网)-私网(局域网)
192.168.xx.xxx专门给组织内部使用
A类地址:10.0.0.0~10.255.255.255
B类地址:172.16.0.0~172.31.255.255
C类地址:192.168.0.0~192.168.255.255
import java.net.InetAddress;/**
没有构造函数* @author * @date 2021/1/16 - 12:20*/
//测试ip
public class TestInetAddress {public static void main(String[] args) {try {//获取本地ip地址InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");InetAddress inetAddress2 = InetAddress.getByName("localhost");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.getHostAddress());//ipSystem.out.println(inetAddress4.getCanonicalHostName());//规范的名字System.out.println(inetAddress4.getHostName());//域名,或者自己电脑的名字} catch(Exception e) {e.printStackTrace();}}
}
1.4端口
端口表示计算机上的一个程序的进程
-
不同的进程有不同的端口号:用来区分软件
-
被规定为0~65535
-
TCP/UDP 65535*2,单个协议下,端口号不能冲突
-
端口分类:
公有端口
Telent:23
Http:80
HTTPS:443
FTP21
程序注册端口:1024~49151,分配用户或者程序
Tomcat:8080
Mysql:3306
Oracle:1521
动态、私有:49152~65535
netstat -ano #查看所有的端口 netstat -ano | findstr "5900"#查看指定的端口 tasklist|findstr "8696"#查看指定端口
import java.net.InetSocketAddress;/*** @author pcy* @date 2021/1/16 - 15:15*/ public class TestInetSocketAddress {public static void main(String[] args) {InetSocketAddress inetSocketAddress1=new InetSocketAddress("127.0.0.1",8080);InetSocketAddress inetSocketAddress2=new InetSocketAddress("localhost",8080);System.out.println(inetSocketAddress1);System.out.println(inetSocketAddress2);//QICQSystem.out.println(inetSocketAddress1.getAddress());System.out.println(inetSocketAddress1.getHostName());System.out.println(inetSocketAddress1.getPort());//获取端口号} }
1.5通信协议
协议:约定
网络通信协议:速率,传输码率,代码结构,传输控制
TCP/IP协议簇:实际上是一组协议
重要:
TCP:用户传输协议
UDP:用户数据报协议
出名的协议:
TCP:
IP:网络互连协议
TCP和udp对比
TCp:打电话
-
连接、稳定
-
三次握手,四次挥手
双方是有回应的保持稳定性
-
客户端、服务端
-
传输完成,释放连接、效率低
udp:发短信
- 不连接、不稳定
- 客户端、服务端:没有明确的界限
- 不管有没有准确准备好,都可以发给你
- 导弹
- DDOS:洪水攻击!饱和攻击
1.6、TCp
实现聊天
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;/*** @author pcy* @date 2021/1/16 - 15:46*/
//服务端
public class TCPdemo1 {public static void main(String[] args) {//有一个地址ServerSocket serverSocket=null;Socket socket=null;InputStream is=null;ByteArrayOutputStream bais=null;{try {serverSocket = new ServerSocket(9999);while (true) {socket = serverSocket.accept();//读取客户端消息is = socket.getInputStream();//管道流bais = new ByteArrayOutputStream();byte[] bytes = new byte[1024];int len;while ((len = is.read(bytes)) != -1) {bais.write(bytes, 0, len);}System.out.println(bais.toString());}} catch (IOException e) {e.printStackTrace();}finally {if (bais!=null) {try {bais.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();}}}}}}
import com.sun.security.ntlm.Server;import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;/*** @author pcy* @date 2021/1/16 - 15:46*/
//客户端
public class TCPdemo {public static void main(String[] args) {Socket socket = null;OutputStream os = null;//要知道服务器的地址try {InetAddress inetAddress = InetAddress.getByName("localhost");int port = 9999;//创建一个Socket连接socket = new Socket(inetAddress, port);os = socket.getOutputStream();os.write("我爱你啊".getBytes());} catch (IOException 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();}}}}
}
1.7文件上传
服务器端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;/*** @author pcy* @date 2021/1/16 - 17:16*/
public class TcpServerDemo {public static void main(String[] args) throws IOException {//创建服务ServerSocket serverSocket = new ServerSocket(9998);Socket socket=serverSocket.accept();InputStream is = socket.getInputStream();FileOutputStream fops = new FileOutputStream(new File("receive.jpg"));byte[] bytes=new byte[1024];int len;if ((len=is.read(bytes))!=-1){fops.write(bytes,0,len);}//通知客户端器接收完毕OutputStream os = socket.getOutputStream();os.write("我接受完了".getBytes());os.close();fops.close();is.close();socket.close();serverSocket.close();}
}
客户端
import java.io.*;
import java.net.Socket;/*** @author pcy* @date 2021/1/16 - 17:07*/
public class TcpClientDemo1 {public static void main(String[] args) throws IOException {Socket socket=new Socket("127.0.0.1",9998);OutputStream os = socket.getOutputStream();FileInputStream fips = new FileInputStream(new File("3.jpg"));byte[] bytes=new byte[1024];int len;if ((len=fips.read(bytes))!=-1){os.write(bytes,0,len);}//通知服务器接受完毕socket.shutdownOutput();//确定服务器接受完毕,才断开InputStream is=socket.getInputStream();ByteArrayOutputStream baos=new ByteArrayOutputStream();byte[] bytes1=new byte[1024];int len1;if ((len1=is.read(bytes1))!=-1){baos.write(bytes1,0,len1);}System.out.println(baos.toString());baos.close();is.close();fips.close();os.close();socket.close();}
}
1.8 Tomcat
客户端
- 自定义 B
- 浏览器 B
服务端
-
自定义 S
-
Tomcat S
1.9UDP消息发送
发短信:不用连接,需要知道对方的地址
发送消息
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;/*** @author pcy* @date 2021/1/17 - 17:18*/
public class UdpClientDemo {public static void main(String[] args) throws Exception {//建立一个socketDatagramSocket socket = new DatagramSocket();String msg="我喜欢你啊";InetAddress address = InetAddress.getByName("localhost");int port=9090;//建立一个package//第一个是数据,从0开始,数据的长度,ip地址,端口号DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,address,port);//发送包socket.send(packet);//关闭流socket.close();}
}
接受消息
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;/*** @author pcy* @date 2021/1/17 - 17:29*/
//等待客户端的连接
public class UdpServerDemo {public static void main(String[] args) throws IOException {DatagramSocket socket = new DatagramSocket(9090);byte[] bytes=new byte[1024];DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);//接收包socket.receive(packet);System.out.println(packet.getAddress());System.out.println(new String(packet.getData(),0,packet.getLength()));socket.close();}
}
2.0 UDP聊天实现
发送消息
public class UdpSendDemo {public static void main(String[] args) throws IOException {DatagramSocket socket = new DatagramSocket(8081);//读取控制台BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));while (true) {String data = reader.readLine();byte[] bytes = data.getBytes();DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length, new InetSocketAddress("localhost", 6060));socket.send(packet);if (data.equals("bye")){break;}}socket.close();}
}
接收消息
public class UdpReceiveDemo {public static void main(String[] args) throws IOException {DatagramSocket socket = new DatagramSocket(6060);while (true) {byte[] bytes = new byte[1024];DatagramPacket packet = new DatagramPacket(bytes, 0, bytes.length);socket.receive(packet);//断开连接byte[] data = packet.getData();String receiveData = new String(data, 0, data.length);System.out.println(receiveData);if (receiveData.equals("bye")) {break;}}socket.lose();}
}
2.URL下载网络资源
统一资源定位符
DNS域名解析 将www.baidu.com解析成XXX.XXX.XXXX
协议:http等/ip地址,端口/项目,资源
public class UrlDemo {public static void main(String[] args) throws MalformedURLException {URL url = new URL("http://localhost:8080/helloWorld/index.jsp?username=123");System.out.println(url.getHost());//主机名System.out.println(url.getPort());//端口号System.out.println(url.getFile());//全路径System.out.println(url.getPath());//文件路径System.out.println(url.getQuery());//参数名System.out.println(url.getProtocol());//协议}
}
public class UrlDemo {public static void main(String[] args) throws IOException {URL url = new URL("http://localhost:8080/pcy/Security.txt");HttpURLConnection connection = (HttpURLConnection) url.openConnection();InputStream is = connection.getInputStream();FileOutputStream fs = new FileOutputStream("Security.txt");byte[] bytes = new byte[1024];int len;if ((len=is.read(bytes))!=-1){fs.write(bytes,0,len);//写出这个数据}fs.close();is.close();connection.disconnect();}
}
发布评论