JAVA网络编程
概述
计算机网络
计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。
TCP
TCP/IP协议是Internet最基本的协议、Internet国际互联网络的基础,由网络层的IP协议和传输层的TCP协议组成。通俗而言:TCP负责发现传输的问题,一有问题就发出信号,要求重新传输,直到所有数据安全正确地传输到目的地。而IP是给因特网的每一台联网设备规定一个地址。
UDP
UDP用户数据报协议,是面向无连接的通讯协议,UDP数据包括目的端口号和源端口号信息,由于通讯不需要连接,所以可以实现广播发送。
UDP通讯时不需要接收方确认,属于不可靠的传输,可能会出现丢包现象,实际应用中要求程序员编程验证。
网络通信的要素
通信双方的地址
- IP
- 端口号
网络通信协议
如TCP HTTP等协议
IP
IP 类:InetAddress
- 定位网络上的设备
- 127.0.0.1 :映射本机localhost
- ip地址分类
- ipv4/ipv6
- ipv4 格式XXX.XXX.XXX.XXX,由4个字节组成,0~255
- ipv6 128位 由8个无符号整数
- 公网-私网
- ipv4/ipv6
InetAddress类使用
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestinetAddress {
public static void main(String[] args) {
try {
InetAddress byName = InetAddress.getByName("127.0.0.1");
System.out.println(byName);
//查询网站地址
InetAddress byName1 = InetAddress.getByName("www.baidu.com");
System.out.println(byName1);
System.out.println(byName1.getCanonicalHostName());//规范名字
System.out.println(byName1.getHostAddress());//ip
System.out.println(byName1.getHostName());//域名或者是主机名
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
端口
- 计算机上一个程序进程所占用的端口
- 单个协议下端口不可以重复
- 查看端口
- win :netstat -ano | findstr “端口号”
InetSocketAddress类使用
import java.net.InetSocketAddress;
public class TestInetSocketAddress {
public static void main(String[] args) {
InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 8080);
System.out.println(inetSocketAddress.toString());
System.out.println(inetSocketAddress.getAddress());
System.out.println(inetSocketAddress.getHostName());//地址
System.out.println(inetSocketAddress.getPort());//端口
}
通信协议
TCP/IP协议簇
- TCP:传输控制协议
- UDP:用户数据报协议
- IP:网络互连协议
TCP UDP对比
TCP
- 连接稳定
- 三次握手,四次挥手
- 区分客户端与服务端
- 传输完成就会释放连接,效率低
UDP
- 不连接,不稳定
- 客户端,服务端没有明确的界限
- 不论接收方是否准备好都会都会发送
TCP
实现TCP通信
客户端
- 连接服务器Socket
- 发送消息
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;
public class Client {
public static void main(String[] args) {
InetAddress inetAddress = null;
Socket socket = null;
OutputStream outputStream = null;
//1.要知道服务器的地址,端口号
while(true) {
try {
inetAddress = InetAddress.getByName("127.0.0.1");
int port = 9999;
//2.创建一个socket链接
socket = new Socket(inetAddress, port);
//3.发送消息 IO流
outputStream = socket.getOutputStream();
Scanner sc = new Scanner(System.in);
outputStream.write(sc.nextLine().getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
outputStream.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务器
- 建立服务端口ServerSocker
- 等待用户的连接accept
- 接收用户消息
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
//1.我得有一个地址
serverSocket = new ServerSocket(9999);
while(true) {
//2.等待客户端链接
socket = serverSocket.accept();
inputStream = socket.getInputStream();
byte[] bytes = new byte[1024];
//3.读取客户端的消息
byteArrayOutputStream = new ByteArrayOutputStream();
int len = 0;
while((len = inputStream.read(bytes)) != -1) {
byteArrayOutputStream.write(bytes, 0, len);
}
System.out.println(byteArrayOutputStream.toString());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
byteArrayOutputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
TCP文件上传
客户端
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
// 客户端
public class TCPClientDemo02 {
public static void main(String[] args) throws Exception {
// 1.建立连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9000);
// 2.创建输出流
OutputStream os = socket.getOutputStream();
// 3.读取文件
FileInputStream stream = new FileInputStream(new File("subei.jpg"));
// 4.输出测试文件
byte[] buffer = new byte[1024];
int len = 0;
while((len = stream.read(buffer)) != -1){
os.write(buffer,0,len);
}
// 5.通知服务器已发送完成
socket.shutdownOutput();
// 6.确定服务器已经收到,断开连接
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2 = 0;
while((len2 = stream.read(buffer2)) != -1){
byteArrayOutputStream.write(buffer2,0,len2);
}
System.out.println(byteArrayOutputStream);
// 7.关闭流
byteArrayOutputStream.close();
inputStream.close();
stream.close();
os.close();
socket.close();
}
}
服务器
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
// 服务器
public class TCPServerDemo02 {
public static void main(String[] args) throws Exception{
// 1.创建一个服务器地址
ServerSocket socket = new ServerSocket(9000);
// 2.监听客户连接
Socket accept = socket.accept(); // 阻塞式监听,能等待用户连进来
// 3.获取输入流
InputStream is = accept.getInputStream();
// 4.确定存放文件的位置
FileOutputStream fileOutputStream = new FileOutputStream("resort.jpg");
// 5.写入文件
byte[] buffer = new byte[1024];
int len = 0;
while((len = is.read(buffer)) != -1){
fileOutputStream.write(buffer,0,len);
}
// 6.通知客户端收集完毕
OutputStream outputStream = accept.getOutputStream();
outputStream.write("服务器已经收集成功,请断开连接!".getBytes());
// 7.关闭流
outputStream.close();
fileOutputStream.close();
is.close();
accept.close();
socket.close();
}
}
UDP
发送方
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
// 不需要连接服务器
public class UDPClientDemo01 {
public static void main(String[] args) throws Exception{
// 1.建立一个Socket
DatagramSocket socket = new DatagramSocket();
// 2.建个包
String msg = "服务器,你好!";
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
// 数据,数据的长度起始,要发送给谁
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0,msg.getBytes().length,localhost,port);
// 3.发送包
socket.send(packet);
// 4.关闭流
socket.close();
}
}
接收方
import java.net.DatagramPacket;
import java.net.DatagramSocket;
// 还是需要客户端的链接
public class UDPServerDemo01 {
public static void main(String[] args) throws Exception{
// 1.开放端口
DatagramSocket socket = new DatagramSocket(9090);
// 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(new String(packet.getData()));
// 3.关闭连接
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 formPort;
private String hostname;
private int toPort;
public TalkSend(int formPort, String hostname, int toPort) {
this.formPort = formPort;
this.hostname = hostname;
this.toPort = toPort;
try {
socket = new DatagramSocket(formPort);
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
try {
String s = reader.readLine();
byte[] buffer = s.getBytes();
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length,
new InetSocketAddress(hostname, toPort));
// 发送包
socket.send(packet);
if (s.equals("bye")) {
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 关闭socket
socket.close();
}
}
接收端
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
/**
* @author subeiLY
* @create 2021-06-06 14:22
*/
public class TalkReceive implements Runnable{
DatagramSocket socket = null;
private int port;
public TalkReceive(int port) {
this.port = port;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
//开放端口
// 接收包
while (true){
try {
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);
System.out.println(Thread.currentThread().getName() + ":" + receiveData);
if (receiveData.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
一个用户线程类
public class StudentClient {
public static void main(String[] args) {
new Thread(new TalkSend(7777,"localhost",8900)).start();
new Thread(new TalkReceive(9999),"老师").start();
}
}
第二个用户线程类
public class TeacherClient {
public static void main(String[] args) {
new Thread(new TalkSend(5555,"localhost",9999)).start();
new Thread(new TalkReceive(8900),"学生").start();
}
}
URL
- 统一资源定位符:定位互联网上的某一个资源
- 格式:协议:// ip地址:端口号/项目名/资源
import java.net.MalformedURLException;
import java.net.URL;
public class URLDemo01 {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://localhost:8080/helloworld/insex" +
".jsp?username=subeily&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()); // 参数
}
}
下载网络文件
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class URLDemo02 {
public static void main(String[] args) throws Exception{
// 1.下载地址
URL url = new URL("http://localhost:8080/subei/hello.html");
// 2.连接到这个资源 HTTP
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream is = urlConnection.getInputStream();
FileOutputStream stream = new FileOutputStream(new File("same.txt"));
byte[] bytes = new byte[1024];
int len = 0;
while ((len = is.read(bytes)) != -1){
stream.write(bytes,0,len);
}
stream.close();
is.close();
urlConnection.disconnect();
}
}