Java网络编程:通信概述,TCP, UDP, URL
网络编程
TCP,UDP
1.1 概述
计算机网络:
将地理位置不同给的具有独立功能的多台计算机及其外部设备,通过通信路线连接起来,在网络操作系统,网络管理软件机器网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统
网络编程的目的:
数据交换,通信
如何实现:
- 如何准确的定位网络上的一台主机:IP地址,端口,定位到计算机上的某个资源
- 如何传输数据
javaweb:网页编程 B/S
网络编程:TCP/IP 客户端:C/S架构
1.2 网络通信的要素
实现网络通信:
-
通信双方的地址
- IP地址
- 端口号
-
规则:网络通信的协议
-
TCP/IP参考模型(传输层)
-
1.3 IP
IP地址:InetAddress
- 唯一定位一台网络上的计算机
- 本机(localhost):127.0.0.1
- IP地址的分类:
- IPV4/IPV6
- IPV4:
127.0.0.1
,4个字节组成,每个字节0~255,42亿个,十进制,2011年已经用尽 - IPV6:
fe80::fcaa:dcca:502b:b78d%11
, 128位,8个无符号整数,十六进制
- IPV4:
- 公网(互联网)-私网(局域网)
- 局域网:192.168.xx.xx,组织内部使用
- ABCD类地址
- IPV4/IPV6
- 域名:记忆IP问题
- IP:www.abc.com
//查询本机地址
InetAddress address1 = InetAddress.getByName("127.0.0.1");
InetAddress address2 = InetAddress.getByName("localhost");
InetAddress address3 = InetAddress.getLocalHost();
System.out.println(address1);
System.out.println(address2);
System.out.println(address3);
//查询网站IP地址
InetAddress address4 = InetAddress.getByName("www.baidu.com");
System.out.println(address4);
//常用方法
System.out.println(address4.getAddress());//不常用
System.out.println(address4.getCanonicalHostName());//规范IP地址
System.out.println(address4.getHostAddress());//IP
System.out.println(address4.getHostName());//域名或电脑名称
1.4 端口
端口表示计算机上的一个程序的进程:
-
不同的进程有不同的端口号,用于区分软件
-
端口范围:0~65535
-
TCP和UDP各有65535个端口,单个协议下端口号不能冲突,不同协议可以相同
-
端口分类:
- 公有端口:0~1023
- http:80
- https: 443
- ftp: 21
- Telent : 23(远程监听)
- 程序注册端口:2014~49151,分配给用户或程序的端口
- Tomcat: 8080
- MySQL: 3306
- Oracle: 1521
- 动态/私有端口:49152~66535
- 公有端口:0~1023
-
查看端口的命令:
netstat -ano #查看所有端口
netstat -ano|findstr "xxxx" #查看指定端口
tasklist|findstr "xxxx" #查看指定进程ID(PID)的进程
Ctrl + Shift + Esc
: 打开任务管理器
InetSocketAddress
:端口操作
InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8080);
InetSocketAddress inetSocketAddress2 = new InetSocketAddress("localhost", 8080);
System.out.println(inetSocketAddress);
System.out.println(inetSocketAddress2);
System.out.println(inetSocketAddress.getAddress());
System.out.println(inetSocketAddress.getHostName());//hosts地址
System.out.println(inetSocketAddress.getPort());//端口
1.5 通信协议
协议:约定
网络通信协议:速率,传输码率,代码结构,传输控制……
简化 -> 分层 ->TCP/IP协议簇(一组协议):
- 重要协议
- TCP: 用户传输协议
- UDP: 用户数据报协议
- 出名协议:
- TCP: 用户传输协议
- IP: 网络互连协议
TCP和UDP对比
- TCP:打电话
- 需要连接,稳定
- 三次握手,四次挥手:3次连接(ABA),4次断开(ABBA),确保连接稳定
- 客户端、服务端
- 传输完成释放连接,效率低
- UDP: 发短信
- 不连接,不稳定
- 客户端、服务端:没用明确的界限
- 不管有没有准备都可以发送
1.6 TCP
模拟客户端和服务端连接:使用时先启动服务端再启动客户端
- 客户端
- 连接服务器Socket
- 发送消息
- 服务端:
- 建立服务器端口ServerSocket
- 等待用户的 连接accept
- 接受用户的消息
通信实现
- 客户端代码(使用方法抛出异常,省去关闭时的if)
public class TcpClientDemo01 {
public static void main(String[] args) throws IOException {
//1. 获得服务器地址,端口号
InetAddress serverIp = InetAddress.getByName("127.0.0.1");
int port = 9999;
//2. 创建一个socket连接
Socket socket = new Socket(serverIp, port);
//3. 发送消息 IO流
OutputStream os = socket.getOutputStream();
os.write("hello".getBytes());
socket.close();
os.close();
}
}
- 服务端核心代码:在服务端嵌套while方法,可以实现循环监听
//1. 创建地址,端口号
serverSocket = new ServerSocket(9999);
//2. 等待连接
//这里的socket与客户端中的是同一个对象
socket = serverSocket.accept();
//3. 读取客户端的消息
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());
服务端完整代码:使用try...catch抛出异常,便于处理。关闭需要从最后启动的顺序开始
//服务端
public class TcpServerDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//1. 创建地址,端口号
serverSocket = new ServerSocket(9999);
//2. 等待连接
//这里的socket与客户端中的是同一个对象
socket = serverSocket.accept();
//3. 读取客户端的消息
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();
}
}
}
}
}
文件上传
- 服务器端
public class TcpServerDemo02 {
public static void main(String[] args) throws IOException {
//1. 创建服务
ServerSocket serverSocket = new ServerSocket(9999);
//2. 监听客户端的连接
//阻塞式监听,一直监听直到或许到想要的信息,再继续运行后面的代码
Socket socket = serverSocket.accept();
//3. 获取输入流
InputStream is = socket.getInputStream();
//4. 文件输出
FileOutputStream fos = new FileOutputStream(new File("F:\\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("received, over".getBytes());
//5. 关闭资源
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
- 客户端
public class TcpClientDDemo02 {
public static void main(String[] args) throws IOException {
//1. 创建一个Socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9999);
//2. 创建一个输出流
OutputStream os = socket.getOutputStream();
//3. 文件流
FileInputStream fis = new FileInputStream(new File("F:\\wall.jpg"));
//4. 写出文件
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[] buffer2 = new byte[1024];
int len2;
while ((len2 = is.read(buffer2)) != -1){
baos.write(buffer2,0,len2);
}
System.out.println(baos.toString());
//5. 关闭资源
baos.close();
is.close();
fis.close();
os.close();
socket.close();
}
}
Tomcat
B/S
- 服务器
- 自定义S
- Tomcat服务器 S: 8080
- 客户端
- 自定义 C
- 浏览器 B
1.7 UDP
发短息:不用连接,但需要对方的地址
发送端也可以作为接收端
- 发送端
//不需要连接服务器
public class UdpClientDemo01 {
public static void main(String[] args) throws IOException {
//1. 建立socket
DatagramSocket socket = new DatagramSocket();
//2. 建包
String msg = "hello";
InetAddress localHost = InetAddress.getLocalHost();
int port = 9000;
//数据,数据的起始长度,发送给谁
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 IOException {
//开放端口
DatagramSocket socket = new DatagramSocket(9000);
//接收数据包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
socket.receive(packet);
System.out.println(packet.getAddress().getHostAddress());
System.out.println(new String(packet.getData(),0,packet.getLength()));
//关闭
socket.close();
}
}
通信
单向多次发送和多次接收
- 发送端
public class UdpSenderDemo01 {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(8888);
//读取控制台数据System.in(比scanner效率更高)
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true){
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost",6666));
socket.send(packet);
//关闭条件
if ("over".equals(data)){
break;
}
}
socket.close();
}
}
- 接收端
public class UdpReceiveDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(6666);
while (true){
//准备接收package
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container,0,container.length);
socket.receive(packet);//阻塞式接收package
//断开连接
byte[] data = packet.getData();
String s = new String(data, 0,packet.getLength());//读取长度设为数据包的长度,或用trim()去除空格
System.out.println(s);
//关闭条件,在前面的s确保接收与发送内容相同
if ("over".equals(s)){
break;
}
}
socket.close();
}
}
双向通信
实现双向通信
- 发送类
public class TestSend implements Runnable{
DatagramSocket socket = null;
BufferedReader reader = null;
private int fromPort;
private int toPort;
private String toIP;
public TestSend(int fromPort, int toPort, String toIP) {
this.fromPort = fromPort;
this.toPort = toPort;
this.toIP = toIP;
try {
socket = new DatagramSocket(fromPort);
//读取控制台数据System.in(比scanner效率更高)
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 ("over".equals(data)){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
- 接收类
public class TestReceive implements Runnable{
DatagramSocket socket = null;
private int port;
public TestReceive(int port) {
this.port = port;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
//准备接收package
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container,0,container.length);
socket.receive(packet);//阻塞式接收package
//断开连接
byte[] data = packet.getData();
String s = new String(data, 0,packet.getLength());//读取长度设为数据包的长度,或用trim()去除空格
System.out.println(this.port + ":" +s);
if ("over".equals(s)){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
- 测试类
public class TestStudent {
public static void main(String[] args) {
//开启两个线程
new Thread(new TestSend(6666,7777,"localhost")).start();
new Thread(new TestReceive(9999)).start();
}
}
public class TestTeacher {
public static void main(String[] args) {
new Thread(new TestSend(8888,9999,"localhost")).start();
new Thread(new TestReceive(7777)).start();
}
}
1.8 URL
URL(统一资源定位符):用于定位资源,协议://IP地址: 端口/项目名/资源
DNS解析:把域名解析为IP
URL方法
public class URLDemo01 {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://localhost:8080/hello/index.jsp?username=chachan53&password=123");
System.out.println(url.getProtocol());//协议
System.out.println(url.getHost());//主机IP
System.out.println(url.getPort());//端口
System.out.println(url.getPath());//文件名
System.out.println(url.getFile());//文件全路径
System.out.println(url.getQuery());//参数
}
}
URL下载文件
使用Tomcat在本地配置的url进行下载测试(实际使用时可以替换其他文件的url,修改下载文件的尾缀)
public class UrlDownload {
public static void main(String[] args) throws IOException {
//1. 下载地址
URL url = new URL("http://localhost:8080/test/test.txt");
//2. 连接到资源,HTTP
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//3. 下载
InputStream is = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("test.txt");
byte[] buffer = new byte[1024];
int len;
//写数据
while ((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
fos.close();
is.close();
urlConnection.disconnect();
}
}