Java 网络编程
https://www.cnblogs.com/swordfall/p/10781281.html
https://www.runoob.com/w3cnote/summary-of-network.html
1 概述
计算机网络是通过传输介质、通信设施和网络通信协议,把分散在不同地点的计算机设备互连起来的,实现资源共享和数据传输的系统。网络编程就是编写程序使互联网的两个(或多个)设备(如计算机)之间进行数据传输。Java语言对网络编程提供了良好的支持。通过其提供的接口我们可以很方便地进行网络编程。
信件:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qU4R1O0w-1631548808673)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210906104548022.png)]
计算机网络
度娘解释:计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。
网络编程的目的
通信 数据交换 无限电台 --传播交流信息
想要达到这个效果需要
- 如何准确定位网络中的一台电脑 ip地址 端口号 定位到计算机上的某个资源
- 找到这个主机之后,如何传输数据
javaweb 网页编程 B/S架构
网络编程是通过协议进行通信,如TCP/IP,UDP等 C/S架构
1.2 网络通信的要素
双方的通信地址
协议
TCP/IP 参考模型
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7XEXI96t-1631548808681)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210906152038984.png)]
参考博客:https://www.cnblogs.com/gdayq/p/5797645.html
小结:
- 网络编程由两个主要的问题
- 如何准确定位到网络上的一台或者多台主机
- 找到主机之后如何进行通信
- 网络编程中的要素
- IP 地址和端口号
- 网络通信协议 UDP TCP IP
- Java中万物皆对象
1.3 IP
public class InetAddress extends Object implements Serializable
此类表示Internet协议(IP)地址
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestInetAddress01 {
public static void main(String[] args) {
try {
InetAddress name = InetAddress.getByName("127.0.0.1");
System.out.println(name);
InetAddress name3 = InetAddress.getByName("localhost");
System.out.println(name3);
InetAddress name4 = InetAddress.getLocalHost();
System.out.println(name4);
InetAddress name2 = InetAddress.getByName("www.baidu.com");
System.out.println(name2);
System.out.println(name2.getAddress());
System.out.println(name2.getHostName());
System.out.println(name2.isAnyLocalAddress());
System.out.println(name2.isLoopbackAddress());
System.out.println(name2.getCanonicalHostName());
System.out.println(name2.hashCode());
System.out.println(name2.isLinkLocalAddress());
System.out.println(name2.isMCGlobal());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
1.4 端口
此处端口指TCP/IP协议中的端口,一般用来区分计算机网络中不同的进程(IP地址用来区分网络中不同的计算机)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LMecvzXq-1631548808683)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210906200337772.png)]
1.5 通信协议
协议:即约定,比如我们交谈用到的普通话
网络通信协议:传输速率 传输码率 代码结构 传输控制 RB,Rb…
问题:非常复杂
大事化小,,分层
TCP/IP协议簇
最主要的基于传输层的协议
- TCP:传输控制协议(Transmission Control Protocol)是一种面向连接的、可靠的、基于字节流的传输层通信协议,由IETF的RFC 793 定义。
- UDP: 用户数据报协议(User Datagram Protocol)。UDP 为应用程序提供了一种无需建立连接就可以发送封装的 IP 数据包的方法。RFC 768 描述了 UDP。
TCP UDP区别
-
TCP 打电话
-
连接、稳定 -
三次握手,四次挥手 最少需要三次,保持稳定连接
A:你愁啥?
B:愁你咋地?
A:干一场
A:我要走了
B:你真的要走了吗?
B:你真的真的要走了吗?
A:我真的要走了
-
客户端 服务端 -
传输完成,释放连接,效率低 -
UDP 发短信
- 不连接、不稳定
- 客户端 服务端 没有明确的界限
- 不管服务端有没有准备好,都可以进行发送
- DDOS 洪水攻击(饱和攻击)
1.6 TCP
1.6.1 实现聊天
IO流
客户端
- 连接服务器 Socket
- 发送消息
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TestClientDemo01 {
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);
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
- 接收用的消息
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class TestServerDemo01 {
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();
}
}
}
}
}
1.6.2 TCP上传文件
P7,IO流出问题\\和 / 的作用相同。\\是对\的转义,都可以在window平台中使用,但是因为java有时需要跨平台,linux只能识别 / ,直接用 / 比较好
src下的绝对路径可以,相对路径报错
客户端
package com.raylene.net.tcpfileupload;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class TestClientDemo02 {
public static void main(String[] args) throws IOException {
Socket socket = null;
OutputStream os = null;
FileInputStream fis = null;
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9009;
socket = new Socket(serverIP,port);
os = socket.getOutputStream();
fis = new FileInputStream(new File("G:/XJTU/studynote/javacode/JAVASE/src/mmm.jpg"));
byte[] buffer = new byte[1024];
int len;
while((len=fis.read(buffer)) != -1){
os.write(buffer,0,len);
}
socket.shutdownOutput();
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer1 = new byte[1024];
int len1;
while((len1 = is.read(buffer1)) != -1) {
baos.write(buffer1, 0, len1);
}
System.out.println(baos.toString());
baos.close();
is.close();
fis.close();
os.close();
socket.close();
}
}
服务器端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TestServerDemo02 {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(9009);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream("G:/XJTU/studynote/javacode/JAVASE/src/receive.jpg");
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
OutputStream os = socket.getOutputStream();
os.write("我已经接收完毕啦".getBytes());
os.close();
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
1.6.3 Tomcat
服务器端
- 自定义 S
- Tomcat 服务器 S 重点:Java后台开发用别人的服务器
客户端
1.7 UDP
消息发送
服务端
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UdpServerDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(9090);
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
System.out.println(packet.getAddress());
System.out.println(new String(packet.getData(),0,packet.getLength()));
System.out.println(packet.getSocketAddress());
System.out.println(packet.getPort());
socket.close();
}
}
客户端
import java.net.*;
import java.nio.charset.StandardCharsets;
public class UdpClientDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket();
String msg = "你好,服务器!";
DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,InetAddress.getByName("localhost"),9090);
socket.send(packet);
socket.close();
}
}
聊天实现
发送端(客户端)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class UdpSendDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(8088);
while (true){
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost",6868));
socket.send(packet);
if(data.equals("bye")){
break;
}
}
socket.close();
}
}
接收端(服务端)
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpReceiveDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(6868);
while(true){
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container,0,container.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();
}
}
多线程在线咨询
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class TalkSend implements Runnable{
DatagramSocket socket = null;
BufferedReader reader = null;
private int fromPort;
private String toIP;
private int toPort;
public TalkSend(int fromPort, String toIP, int toPort){
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;
try {
socket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress(this.toIP,this.toPort));
socket.send(packet);
if(data.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TalkReceive implements Runnable{
DatagramSocket socket = null;
private int port;
private String msgfrom;
public TalkReceive(int port,String msgfrom) {
this.port = port;
this.msgfrom = msgfrom;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while(true){
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container,0,container.length);
try {
socket.receive(packet);
} catch (IOException e) {
e.printStackTrace();
}
byte[] data = packet.getData();
String receivedata = new String(data, 0, packet.getLength());
System.out.println(msgfrom+": "+receivedata);
if(receivedata.equals("bye")){
break;
}
}
socket.close();
}
}
public class TalkStudent {
public static void main(String[] args) {
new Thread(new TalkSend(7777,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"老师")).start();
}
}
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend(6666,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"学生")).start();
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-L0awupw7-1631548808690)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210913205917768.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Urq5oD5d-1631548808691)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\image-20210913205937417.png)]
1.8 URL
Uniform Resource Locator,统一资源定位器:定义互联网上的某个资源
域名系统(英文:Domain Name System,缩写DNS)是互联网的一项服务。它作为将域名和IP地址相互映射的一个分布式数据库,能够使人更方便地访问互联网。
1. 协议: // ip地址:端口号/项目名/资源
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class TestDemo01 {
public static void main(String[] args) throws IOException {
URL url = new URL("https://m801.music.126.net/20210913233956/a6a3d6a2742f2c8cb67cdfbcc3cefb9f/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/5546003514/e409/aed6/f93a/e0f802f6dbe22026d98c0129ec474324.m4a");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream is = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("missSpace.m4a");
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
fos.close();
is.close();
urlConnection.disconnect();
}
}
|