个人主页: 叶落闲庭
我的专栏:
c语言
数据结构
javaweb
石可破也,而不可夺坚;丹可磨也,而不可夺赤。
BS | CS |
---|---|
不需要开发客户端 | 画面可以做的非常精美,用户体验好 |
用户不需要下载,打开浏览器就能使用 | 需要开发客户端,也需要开发服务端 |
如果应用过大,用户体验受到影响 | 用户需要下载和更新的时候太麻烦 |
全称:Internet Protocol,是互联网协议地址,也称IP地址。
是分配给上网设备的数字标签。
唯一的
分类:IPv4、IPv6
DOS常用命令:
ipconfig:查看本机IP地址
ping IP地址:检查网络是否连通
InetAddress:此类表示Internet协议(IP)地址
方法名 | 说明 |
---|---|
static InetAddress getByName(String host) |
确定主机名称的IP地址。主机名称可以是机器名称,也可以是IP地址 |
String getHostName() |
获取此IP地址的主机名 |
String getHostAddress() |
返回文本显示中的IP地址字符串 |
package com.practice;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressDemo {
public static void main(String[] args) throws UnknownHostException {
//获取InetAddress对象
//一台电脑的对象
InetAddress address = InetAddress.getByName("DESKTOP-MB3PIVP");
//InetAddress address = InetAddress.getByName("192.168.1.66");
System.out.println(address);
//public String getHostName():获取此IP地址的主机名
String name = address.getHostName();
//public String getHostAddress():返回文本显示中的IP地址字符串
String ip = address.getHostAddress();
System.out.println("主机名:" + name);
System.out.println("IP地址:" + ip);
}
}
协议
UDP协议
TCP协议
传输控制协议 (Transmission Control Protocol)
TCP协议是面向连接的通信协议,即传输数据之前,在发送端和接收端建立逻辑连接,然后再传输数据,它提供了两台计算机之间可靠无差错的数据传输。在TCP连接中必须要明确客户端与服务器端,由客户端向服务端发出连接请求,每次连接的创建都需要经过“三次握手”
三次握手:TCP协议中,在发送数据的准备阶段,客户端与服务器之间的三次交互,以保证连接的可靠
第一次握手,客户端向服务器端发出连接请求,等待服务器确认
第二次握手,服务器端向客户端回送一个响应,通知客户端收到了连接请求
第三次握手,客户端再次向服务器端发送确认信息,确认连接
完成三次握手,连接建立后,客户端和服务器就可以开始进行数据传输了。由于这种面向连接的特性,TCP协议可以保证传输数据的安全,所以应用十分广泛。例如上传文件、下载文件、浏览网页等。
package com.practice.udpdemo;
import java.io.IOException;
import java.net.*;
/**
* @Author YJ
* @Date 2023/7/29 10:44
* Description:UDP发送数据
*/
public class SetMessageDemo {
public static void main(String[] args) throws IOException {
//1.创建对象
//创建发送端的Socket对象(DatagramSocket)
//绑定端口,通过这个端口往外发送数据
//空参:所有可用端口中随机一个进行使用
//有参:指定端口进行绑定
DatagramSocket ds = new DatagramSocket();
String str = "你好呀!!!";
byte[] bytes = str.getBytes();
InetAddress address = InetAddress.getByName("127.0.0.1");
int port = 10086;
//2.打包数据
DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,port);
//3.发送数据
ds.send(dp);
//4.释放资源
ds.close();
}
}
接收数据的步骤
package com.practice.udpdemo;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* @Author YJ
* @Date 2023/7/29 10:55
* Description:UDP接收数据
*/
public class ReceiveDemo {
public static void main(String[] args) throws IOException {
1.创建对象
//创建发送端的Socket对象(DatagramSocket)
//在接收的时候,一定要绑定端口
//绑定的端口一定要跟发送的端口保持一致
DatagramSocket ds = new DatagramSocket(10086);
//2.接受数据包
byte[] bytes = new byte[1024];
DatagramPacket dp= new DatagramPacket(bytes,bytes.length);
//receive是阻塞的,会在这死等
ds.receive(dp);
//3.解析数据包
byte[] data = dp.getData();
int length = dp.getLength();
InetAddress address = dp.getAddress();
int port = dp.getPort();
System.out.println("接收到数据:"+ new String(data,0,length));
System.out.println("数据是从“"+ address + "”在这台电脑中的 "+ port+" 端口发出的");
ds.close();
}
}
package com.practice.udpdemo;
import java.io.IOException;
import java.net.*;
import java.util.Scanner;
/**
* @Author YJ
* @Date 2023/7/29 11:09
* Description:发送数据
*/
public class SendMessage {
public static void main(String[] args) throws IOException {
//1.创建对象
DatagramSocket ds = new DatagramSocket();
//2.打包数据
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入消息:");
String str = sc.next();
if("886".equals(str)){
break;
}
byte[] bytes = str.getBytes();
InetAddress address = InetAddress.getByName("127.0.0.1");
int port = 10086;
DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,port);
//3.发送数据
ds.send(dp);
}
ds.close();
}
}
package com.practice.udpdemo;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
/**
* @Author YJ
* @Date 2023/7/29 11:10
* Description:接收数据
*/
public class ReceiveMessage {
public static void main(String[] args) throws Exception {
1.创建对象
DatagramSocket ds = new DatagramSocket(10086);
//2.接收数据包
byte[] bytes = new byte[1024];
while (true) {
DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
ds.receive(dp);
//3.解析数据
byte[] data = dp.getData();
int length = dp.getLength();
String ip = dp.getAddress().getHostAddress();
String name = dp.getAddress().getHostName();
System.out.println("ip为:" + ip + "主机名为:" + name + "的人,发送了:"+ new String(data,0,length));
}
}
}
一对一的形式,之前的代码都是单播的形式
package com.practice.udpdemo;
import java.io.IOException;
import java.net.*;
import java.util.Scanner;
/**
* @Author YJ
* @Date 2023/7/29 11:09
* Description:单播
*/
public class SendMessage {
public static void main(String[] args) throws IOException {
//1.创建对象
DatagramSocket ds = new DatagramSocket();
//2.打包数据
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入消息:");
String str = sc.next();
if("886".equals(str)){
break;
}
byte[] bytes = str.getBytes();
InetAddress address = InetAddress.getByName("127.0.0.1");
int port = 10086;
DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,port);
//3.发送数据
ds.send(dp);
}
ds.close();
}
}
组播地址:224.0.0.0 ~ 239.255.255.255
- 其中224.0.0.0 ~ 224.0.0.255为预留组播地址
package com.practice.udpdemo;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
/**
* @Author YJ
* @Date 2023/7/29 11:39
* Description:组播发送 -- 发送端
*/
public class SendMessage1 {
public static void main(String[] args) throws IOException {
//1.创建MulticastSocket对象
MulticastSocket ms = new MulticastSocket();
String str = "你好~";
byte[] bytes = str.getBytes();
InetAddress address = InetAddress.getByName("224.0.0.1");
int port = 10000;
//2.打包数据
DatagramPacket datagramPacket = new DatagramPacket(bytes,bytes.length,address,port);
//3.发送数据
ms.send(datagramPacket);
//4.释放资源
ms.close();
}
}
package com.practice.udpdemo;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
/**
* @Author YJ
* @Date 2023/7/29 11:10
* Description:接收数据 -- 接收端1
*/
public class ReceiveMessage1 {
public static void main(String[] args) throws Exception {
1.创建对象
MulticastSocket ms = new MulticastSocket(10000);
//2.将当前本机,添加到224.0.0.1这一组中
InetAddress address = InetAddress.getByName("224.0.0.1");
ms.joinGroup(address);
//3.打包数据
byte[] bytes = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
//4.接收数据
ms.receive(dp);
//5.解析数据
byte[] data = dp.getData();
int length = dp.getLength();
String ip = dp.getAddress().getHostAddress();
String name = dp.getAddress().getHostName();
System.out.println("ip为:" + ip + "主机名为:" + name + "的人,发送了:"+ new String(data,0,length));
}
}
package com.practice.udpdemo;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
/**
* @Author YJ
* @Date 2023/7/29 11:10
* Description:接收数据 -- 接收端2
*/
public class ReceiveMessage2 {
public static void main(String[] args) throws Exception {
1.创建对象
MulticastSocket ms = new MulticastSocket(10000);
//2.将当前本机,添加到224.0.0.1这一组中
InetAddress address = InetAddress.getByName("224.0.0.1");
ms.joinGroup(address);
//3.打包数据
byte[] bytes = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes,bytes.length);
//4.接收数据
ms.receive(dp);
//5.解析数据
byte[] data = dp.getData();
int length = dp.getLength();
String ip = dp.getAddress().getHostAddress();
String name = dp.getAddress().getHostName();
System.out.println("ip为:" + ip + "主机名为:" + name + "的人,发送了:"+ new String(data,0,length));
}
}
广播地址:255.255.255.255
package com.practice.udpdemo;
import java.io.IOException;
import java.net.*;
import java.util.Scanner;
/**
* @Author YJ
* @Date 2023/7/29 11:09
* Description:广播
*/
public class SendMessage {
public static void main(String[] args) throws IOException {
//1.创建对象
DatagramSocket ds = new DatagramSocket();
//2.打包数据
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请输入消息:");
String str = sc.next();
if("886".equals(str)){
break;
}
byte[] bytes = str.getBytes();
InetAddress address = InetAddress.getByName("255.255.255.255");
int port = 10086;
DatagramPacket dp = new DatagramPacket(bytes,bytes.length,address,port);
//3.发送数据
ds.send(dp);
}
ds.close();
}
}
Java中的TCP通信:
客户端
Socket(String host,int port)
OutputStream getOutputStream()
void close()
服务器
package com.practice.udpdemo;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
public class ClientDemo {
public static void main(String[] args) throws IOException {
//创建客户端的Socket对象(Socket)
//Socket(String host, int port)
//创建流套接字并将其连接到指定主机上的指定端口号
Socket s = new Socket("127.0.0.1",10001);
//获取输出流,写数据
//OutputStream getOutputStream() 返回此套接字的输出流
OutputStream os = s.getOutputStream();
os.write("hello,tcp,我来了".getBytes());
//释放资源
s.close();
}
}
package com.practice.udpdemo;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
//1.创建服务器端的Socket对象(ServerSocket)
//ServerSocket(int port) 创建绑定到指定端口的服务器套接字
ServerSocket ss = new ServerSocket(10001);
//监听客户端连接
//Socket accept() 侦听要连接到此套接字并接受它
Socket s = ss.accept();
//3.从连接通道中获取输入流,读数据,并把数据显示在控制台
InputStream is = s.getInputStream();
/*byte[] bys = new byte[1024];
int len = is.read(bys);*/
int b;
while ((b = is.read()) != -1) {
System.out.println((char) b);
}
/*String data = new String(bys,0,len);
System.out.println("数据是:" + data);*/
//释放资源
s.close();
ss.close();
}
}
package com.practice.udpdemo;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
//1.创建服务器端的Socket对象(ServerSocket)
//ServerSocket(int port) 创建绑定到指定端口的服务器套接字
ServerSocket ss = new ServerSocket(10001);
//监听客户端连接
//Socket accept() 侦听要连接到此套接字并接受它
Socket s = ss.accept();
//3.从连接通道中获取输入流,读数据,并把数据显示在控制台
InputStream is = s.getInputStream();
//转换流
InputStreamReader isr = new InputStreamReader(is);
int b;
while ((b = isr.read()) != -1) {
System.out.println((char) b);
}
//释放资源
s.close();
ss.close();
}
}
package com.practice.udpdemo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerDemo {
public static void main(String[] args) throws IOException {
//1.创建服务器端的Socket对象(ServerSocket)
//ServerSocket(int port) 创建绑定到指定端口的服务器套接字
ServerSocket ss = new ServerSocket(10001);
//监听客户端连接
//Socket accept() 侦听要连接到此套接字并接受它
Socket s = ss.accept();
//3.从连接通道中获取输入流,读数据,并把数据显示在控制台
/*InputStream is = s.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader bf = new BufferedReader(isr);*/
//写成一行
BufferedReader bf = new BufferedReader(new InputStreamReader(s.getInputStream()));
int b;
while ((b = bf.read()) != -1) {
System.out.print((char) b);
}
//释放资源
s.close();
ss.close();
}
}
关于网络编程的基本知识我们都有了了解,希望对大家有所帮助,欢迎各位小伙伴点赞+关注!!!