实现网络通信的因素
双方通信的地址
- ip
- 端口号
规则协议
tcp/ip 四层参考模型
osi 七层参考模型
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2y53JbfC-1619421307729)(…/…/AppData/Roaming/Typora/typora-user-images/image-20210424171205372.png)]
网络标称针对TCP UDP
两个主要的问题
- 如何定位到网络上的一台主机或者多台主机
- 找到主机之后如何通信
网络编程中的要素
- IP 和端口号
- 网络通信协议
IP地址
ipconfig查看本机IP
ping 地址/域名 查看链接状态
inetAddress 方法工具包,没有构造器不能new
-
唯一定位一台网络上的计算机
-
127.0.0.1 :本机localhost
-
IP地址的分类
-
IP地址分类 / IPV4/ipv6
-
IPV4 类似于127.0.0.1 4个字节组成,0~255 ,有42亿个,。0亿在北美
-
IPV6 类似于fe80::1070:b69e:bac7 128位 ,8位无符号整数
-
2. 公网(互联网)- 私网(局域网)区别
- ABCD类地址 - 192.168.xx.xx 给组织内部使用
3.域名解决记忆问题
-
IneAddress
public class IpGetTest {
public static void main(String[] args) throws UnknownHostException {
InetAddress Address = InetAddress.getByName("localhost");
System.out.println(Address);
System.out.println(InetAddress.getByName("www.baidu.com"));
InetAddress address2 = InetAddress.getByName("www.baidu.com");
System.out.println(address2.getHostAddress());
System.out.println(address2.getCanonicalHostName());//获得规范名字
System.out.println(address2.getHostName());//获得电脑的名字或者域名
// System.out.println(address2.getAddress());
}
}
端口port
端口表示计算机上的程序的进程,想要通信与要找到对应的端口
规定0-65535 tcp udp 端口号不能冲突
公有端口0 - 1023 分配给程序的端口 1024-49151
Tomcat:8080 Mysql 3305 Oracle:1521
剩下的为私有端口 49152-65535 尽量不要用
netstat -nao查看所有端口,
网络通信协议
速率,传输码率等等
问题是非常复杂,解决方法就是分层,
TCP/UDP协议簇 实际上是一组协议
- TCP:用户传输协议
- UCP:用户数据报协议
- IP:网络协议簇
tcp像打电话,连接,稳定,三次握手,四次挥手,需要客户端和服务端
三次握手,四次挥手.
A发送链接消息
B回应
A发送断开消息
B回应
A确认断开
B断开
传输完成,释放链接,效率低
udp不稳定,不连接,客户端服务端没有明确界限
不需要准备好就可以发送,强制发送.比如DDOS,强制发送大量数据,造成端口堵塞,进行攻击.
tcp 应用
传递消息
服务端server
public class TcpServer {
//服务端,提供地址和端口
public static void main(String[] args) throws IOException {
//提供端口
ServerSocket socket = new ServerSocket(9999);
//接收socket,用侦听方法accept
Socket accept = socket.accept();
//读取客户端消息
InputStream stream = accept.getInputStream();
//读文件管道流
ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
byte[] buffer= new byte[1024];
int len;
while ((len= stream.read(buffer))!= -1){
stream1.write(buffer,0,len);
}
System.out.println(stream1.toString());
stream1.close();
socket.close();
}
}
客户端client
public class TcpClient {
public static void main(String[] args) {
OutputStream stream = null;//创建输出流
try {
//需要服务器地址
InetAddress address = InetAddress.getByName("127.0.0.1");
int port = 9999;
Socket socket = new Socket(address,port);//创建!socket链接
stream = socket.getOutputStream();
stream.write("刘越是煞笔".getBytes(StandardCharsets.UTF_8));//发送消息
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
传递文件或图片
基本上就是io操作 ,使用sockt通过地址和端口号进行操作
服务端
public static void main(String[] args) throws Exception {
ServerSocket socket = new ServerSocket(9999);//申请一个端口
Socket accept = socket.accept();//创建监听,这个监听器会一直等待客户端socket的动作
InputStream is = accept.getInputStream();//监听器获得输入流
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("wpx.jpg")));//创建文件流,写出输入的文件流
//写出的固定流程
byte[] buffer1 = new byte[1024];
int len2;
while ((len2 = is.read(buffer1))!= -1){
bos.write(buffer1,0,len2);
}
OutputStream os = accept.getOutputStream();
os.write("服务端接收完成".getBytes());
os.close();
bos.close();
is.close();
socket.close();
}
客户端
public static void main(String[] args) throws Exception {
InetAddress address = InetAddress.getByName("127.0.0.1");
int port = 9999;
Socket socket = new Socket(address, port);
OutputStream os = socket.getOutputStream();
BufferedInputStream br = new BufferedInputStream(new FileInputStream(new File("5.jpg")));
byte[] buffer = new byte[1024];
int len;
while ((len = br.read(buffer))!=-1){
os.write(buffer,0,len);
}
socket.shutdownOutput();
InputStream is = socket.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer1 = new byte[1024];
int len1;
while ((len1 =is.read(buffer1))!= -1){
bos.write(buffer1,0,len1);
}
System.out.println(bos.toString());
bos.close();
is.close();
br.close();
socket.close();
}
外部开启TomCat方法,安装目录的Bin下 ,start.bat 开启shoutdown关闭
UDP应用
UDP 是向固定的端口发送包,即使端口没有开放接受包,也能发送
发送文字信息
public class sendTest {
public static void main(String[] args) throws SocketException, Exception {
//建立包的socket,8086为自己指定的这个socket端口
DatagramSocket socket = new DatagramSocket(8086);
//建立包的内容
String msg = "hello ,server";//信息
InetAddress localhost = InetAddress.getByName("localhost");//传入的地址
int port = 9090;//端口号
socket.send(new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port));//发送
socket.close();//关闭socket
}
}
接受文字信息
public class reciveTest {
public static void main(String[] args) throws Exception{
//申请9090端口的Datasocket
DatagramSocket socket = new DatagramSocket(9090);
//新建一个包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
//接受包的内容
socket.receive(packet);
//输出包的信息
System.out.println(new String(packet.getData(),0,packet.getLength()));//传入的是byte类型,转换格式为String
System.out.println(packet.getAddress());
System.out.println(packet.getPort());
socket.close();
}
}
URL
https://www.baidu.com
统一定位符,定位互联网的某一个资源
DNS:将域名解析成IP地址
协议://IP地址:端口/项目名字/资源
不能多,只能少
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZSGDl6Jt-1619421307731)(…/…/AppData/Roaming/Typora/typora-user-images/image-20210426142833976.png)]
利用URL进行网上下载资源
public class UrlDown {
public static void main(String[] args) throws IOException {
//下载文件地址
URL url = new URL("");
//连接到网站
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//抓取输入流
InputStream is = connection.getInputStream();
//输出他,使用缓冲流
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("保存文件.mp3")));
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer))!= -1){
bos.write(buffer,0,len);
}
bos.close();
is.close();
connection.connect();
}