100_网络编程
目录
网络编程概述
计算机网络
计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。
目的
问题
网络通信的要素
IP和端口号
网络通信协议
TCP/IP四层概念模型
IP
- ip对应java类java.net.InetAddress。
- 唯一定位一台网络上的计算机。
- 127.0.0.1:本机 localhost。
- ip地址分类
- ipv4/ipv6
- ipv4:4个字节组成。0-255,43亿。
- ipv6:128位,8段4位16进制数。如:fe80:0001:0f2c:23ac:e46f:fce1:ace6:6a2f
- 公网/私网
- 公网:互联网
- ABCDE类地址
- 私网:局域网
- 公网:互联网
- ipv4/ipv6
- 域名
package com.qing.demo01;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* 测试IP
*/
public class TestInetAddress {
public static void main(String[] args) {
try {
InetAddress address1 = InetAddress.getLocalHost();
System.out.println(address1);
InetAddress address2 = InetAddress.getByName("127.0.0.1");
System.out.println(address2);
InetAddress address3 = InetAddress.getByName("localhost");
System.out.println(address3);
InetAddress address4 = InetAddress.getByName("www.baidu.com");
System.out.println(address4);
InetAddress address5 = InetAddress.getByName("www.ali.com");
System.out.println(address5);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
L87Y12K91TH8M2R/192.168.75.1
/127.0.0.1
localhost/127.0.0.1
www.baidu.com/182.61.200.7
www.ali.com/72.52.10.14
端口port
- 不同的进程有不同的端口号。
- 0~65535。
- TCP,UDP:65536 * 2,不同协议端口可以相同,如:tcp:80,udp:80是可以的。相同协议下,端口号不能相同。
- 端口分类
- 公有端口:0~1023
- http:80
- https:443
- ftp:21
- ssh:22
- Telnet:23
- 程序注册端口:1024~49151,分配给用户和程序
- tomcat:8080
- MySQL:3306
- Oracle:1521
- 动态、私有端口:49152~65535
- 公有端口:0~1023
#查看所有端口
netstat -ano
#查看指定端口
netstat -ano | findstr "3306"
package com.qing.demo01;
import java.net.InetSocketAddress;
public class TestInetSocketAddress {
public static void main(String[] args) {
InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8080);
InetSocketAddress socketAddress1 = new InetSocketAddress("localhost", 8080);
System.out.println(socketAddress);
System.out.println(socketAddress1);
System.out.println(socketAddress1.getAddress());
System.out.println(socketAddress1.getHostName());
System.out.println(socketAddress1.getPort());
}
}
/127.0.0.1:8080
localhost/127.0.0.1:8080
localhost/127.0.0.1
localhost
8080
通信协议
TCP/IP协议簇
- TCP/IP协议簇:实际上是一组协议。
- 传输层协议
- TCP:传输控制协议
- UDP:用户数据报协议
- 网络层协议
- IP:网际互连协议
- 传输层协议
TCP与UDP对比
- TCP
- 连接,稳定
- 三次握手,四次挥手
- 连接:三次握手
- A通知B:请求连接
- B通知A:同意连接
- A通知B:收到同意
- 断开连接:四次挥手
- A通知B:请求断开连接
- B通知A:同意断开连接
- B通知A:再次确认断开连接
- A通知B:确认断开连接
- 连接:三次握手
- 客户端、服务端
- 传输完成,释放连接,效率低
- UDP
TCP传输控制协议
Socket客户端套接字
ServerSocket服务端套接字
TCP消息发送
package com.qing.demo02;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
/**
* 客户端
*/
public class TestClient01 {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
Scanner scanner = null;
try {
//1.声明服务端的IP和端口号
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
//2.创建socket连接
socket = new Socket(serverIP,port);
//3.发送消息
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();
}
}
}
}
}
package com.qing.demo02;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 服务端
*/
public class TestServer01 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket client = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//1.设置端口
serverSocket = new ServerSocket(9999);
while (true) {
//2.等待客户端连接
client = serverSocket.accept();
System.out.println(client.getInetAddress());
System.out.println(client.getPort());
//3.读取客户端的消息
is = client.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 (client != null) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
欢迎进入武侠世界!
/127.0.0.1
13650
欢迎进入武侠世界!
/127.0.0.1
13666
欢迎进入武侠世界!
TCP文件上传
package com.qing.demo02;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
/**
* 客户端
*/
public class TestClient02 {
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("D:\\code\\JavaSE\\net\\qing.jpg");
//4.写出文件
byte[] buffer = new byte[1024];
int len;
while ((len=fis.read(buffer)) != -1) {
os.write(buffer,0,len);
}
//通知服务端,已经传输完毕
socket.shutdownOutput();
//5.确认服务端接受完毕,才能断开连接
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());
//6.关闭资源
fis.close();
os.close();
socket.close();
}
}
package com.qing.demo02;
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 TestServer02 {
public static void main(String[] args) throws IOException {
//1.创建服务
ServerSocket serverSocket = new ServerSocket(9999);
//2.监听客户端的连接
Socket client = serverSocket.accept();//阻塞式监听,会一直等待客户端连接
//3.获取输入流
InputStream is = client.getInputStream();
//4.文件输出
FileOutputStream fos = new FileOutputStream("D:\\code\\JavaSE\\net\\receive.jpg");
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer)) != -1) {
fos.write(buffer,0,len);
}
//5.通知客户端,已接受完毕
OutputStream os = client.getOutputStream();
os.write("服务端已接受完毕,可以断开连接".getBytes());
//6.关闭资源
fos.close();
is.close();
client.close();
serverSocket.close();
}
}
UDP用户数据报协议
DatagramPacket数据报包
DatagramSocket发送和接收数据报包的套接字
UDP消息发送
package com.qing.demo03;
import java.io.IOException;
import java.net.*;
/**
* udp发送端,udp不需要连接服务端
*/
public class UdpSend01 {
public static void main(String[] args) throws IOException {
//1.创建socket
DatagramSocket socket = new DatagramSocket();
//2.创建数据报包
String msg = "秋风扫落叶";
DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,InetAddress.getByName("localhost"),9999);
//3.发送数据报包
socket.send(packet);
//4.关闭资源
socket.close();
}
}
package com.qing.demo03;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
/**
* udp接收端,udp没有服务端
*/
public class UdpReceive01 {
public static void main(String[] args) throws IOException {
//1.开放端口
DatagramSocket socket = new DatagramSocket(9999);
//2.接收数据报包
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(packet.getPort());
System.out.println(new String(packet.getData()));
//关闭资源
socket.close();
}
}
127.0.0.1
64144
秋风扫落叶
UDP控制台多次输入发送
package com.qing.chat;
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 UdpSender01 {
public static void main(String[] args) throws IOException {
//1.开放端口
DatagramSocket socket = new DatagramSocket(8888);
//2.读取控制台输入
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",9999));
//3.发送数据报包
socket.send(packet);
if ("bye".equals(data)) {
break;
}
}
//4.关闭资源
socket.close();
}
}
左一拳
右一拳
上一拳
下一拳
bye
package com.qing.chat;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class UdpReceive01 {
public static void main(String[] args) throws IOException {
//1.开放端口
DatagramSocket socket = new DatagramSocket(9999);
//2.接收数据报包
while (true) {
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
socket.receive(packet);//阻塞式接收
byte[] datas = packet.getData();
String data = new String(datas).trim();
System.out.println(data);
if ("bye".equals(data)) {
break;
}
}
//3.关闭资源
socket.close();
}
}
左一拳
右一拳
上一拳
下一拳
bye
UDP多线程聊天
package com.qing.chat;
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 {
private String toIP;
private int toPort;
private DatagramSocket socket = null;
private BufferedReader reader = null;
public TalkSend(String toIP, int toPort) {
this.toIP = toIP;
this.toPort = toPort;
reader = new BufferedReader(new InputStreamReader(System.in));
try {
socket = new DatagramSocket();
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
String data = null;
try {
data = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress(toIP,toPort));
try {
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
if ("bye".equals(data)) {
break;
}
}
socket.close();
}
}
package com.qing.chat;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class TalkReceive implements Runnable {
private int port;
private String msgFrom;
private DatagramSocket socket = null;
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[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
try {
socket.receive(packet);//阻塞式接收
} catch (IOException e) {
e.printStackTrace();
}
byte[] datas = packet.getData();
String data = new String(datas).trim();
System.out.println(msgFrom + "->" + data);
if ("bye".equals(data)) {
break;
}
}
//3.关闭资源
socket.close();
}
}
package com.qing.chat;
public class TalkStudent {
public static void main(String[] args) {
//开启两个线程
new Thread(new TalkSend("localhost",9999)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
}
package com.qing.chat;
public class TalkTeacher {
public static void main(String[] args) {
//开启两个线程
new Thread(new TalkSend("localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生")).start();
}
}
老师好
老师->同学好
老师辛苦了
老师->为学生服务
学生->老师好
同学好
学生->老师辛苦了
为学生服务
URL
- URL统一资源定位符:定位资源的,定位互联网上的某一个资源。
https://192.168.10.217:80/demo/index.html
协议://IP:端口/项目名/资源
URL下载网络资源
package com.qing.demo04;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
/**
* URL下载网络资源
*/
public class UrlDown {
public static void main(String[] args) throws IOException {
//1.下载地址
URL url = new URL("https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/topnav/zhidao@2x-e9b427ecc4.png");
//2.连接资源
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
InputStream is = httpURLConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("urlDown.png");
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer)) != -1) {
fos.write(buffer,0,len);
}
fos.close();
is.close();
httpURLConnection.disconnect();//断开连接
}
}


浙公网安备 33010602011771号