title: 尚学堂-Day020
date: 2020-03-05 20:21:04
tags:
- Java
- 尚学堂
categories:
- 尚学堂
- Java
- 网络编程
- 端口和URL
- UDP和TCP
一、概念
网络即将不同区域的电脑连接到一起, 组成局域网、城域网或广域网。把分布在不同地理区域的计算机与专门的外部设备用通信线路互连成一个规模大、功能强的网络系统,从而使众多的计算机可以方便地互相传递信息,共享硬件、软件、数据信息等资源。
二、端口和URL
2.1、端口
端口是虚拟的概念,并不是说在主机上真的有若干个端口。通过端口,可以在一个主机 上运行多个网络应用程序。
public class Test {
public static void main(String[] args) throws Exception {
InetAddress address = InetAddress.getLocalHost(); // 获取主机的地址的IP
System.out.println(address);
System.out.println(address.getHostAddress()); // 获取IP
System.out.println(address.getHostName()); // 获取名称(域名)
address = InetAddress.getByName("www.yanghuisen.cn");
System.out.println(address);
System.out.println(address.getHostAddress()); // 获取IP
System.out.println(address.getHostName()); // 获取名称(域名)
}
}
运行结果 |
---|
DESKTOP-I1HKGCQ/192.168.1.2 192.168.1.2 DESKTOP-I1HKGCQ www.yanghuisen.cn/122.114.134.11 122.114.134.11 www.yanghuisen.cn |
2.2、URL
URL全称是Uniform Resource Location,也就是统一资源位置。实际上,URL就是一种特殊的URI,它除了标识一个资源,还会为资源提供一个特定的网络位置,客户端可以通过它来获取URL对应的资源。
public class Test1 {
public static void main(String[] args) {
InetSocketAddress address = new InetSocketAddress("blog.yanghuisen.cn", 80);
System.out.println(address);
System.out.println(address.getHostName());
System.out.println(address.getAddress());
System.out.println(address.getPort()); // 获取端口
}
}
运行结果 |
---|
blog.yanghuisen.cn/185.199.111.153:80 blog.yanghuisen.cn blog.yanghuisen.cn/185.199.111.153 80 |
三、UDP和TCP
UDP:一种无连接的传输层协议,提供面向事务的简单不可靠信息传送服务。其特点为:非面向连接;传输不可靠;数据可能丢失。
TCP:一种面向连接(连接导向)的、可靠的、基于字节流的传输层(Transport layer)通信协议的点到点的通信 。
3.1、UDP
public class UDPServer {
public static void main(String[] args) throws Exception {
DatagramSocket server = new DatagramSocket(8888); // 创价服务器
byte[] bytes = new byte[1024];
DatagramPacket dp = new DatagramPacket(bytes,bytes.length); // 创建数据报包,指定容器和长度
server.receive(dp); // 阻塞式等待
String msg = new String(bytes,0,dp.getLength()); // dp.getLength()返回实际数据长度
System.out.println("客户端说:"+msg);
server.close();
}
}
public class UDPClient {
public static void main(String[] args) throws Exception {
byte[] bytes = "啊哈哈".getBytes();
DatagramPacket dp = new DatagramPacket(bytes, bytes.length,new InetSocketAddress("localhost", 8888));
DatagramSocket client = new DatagramSocket(8889);
client.send(dp);
client.close();
}
}
运行结果 |
---|
客户端说:啊哈哈 |
3.2、TCP
public class TCPServer {
public static void main(String[] args) throws Exception {
// 创建服务器
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("服务器启动成功");
Socket client = serverSocket.accept(); // 阻塞式等待
System.out.println(client.getInetAddress().getHostName()+"连接上了服务器");
OutputStream os = client.getOutputStream();
os.write("你好客户端,欢迎加入,请发言".getBytes());
new Thread(new Runnable() {
@Override
public void run() {
while(true) {
InputStream is;
try {
is = client.getInputStream();
byte[] bytes = new byte[1024];
int len = is.read(bytes);
System.out.println("客户端说:"+new String(bytes,0,len));
} catch (IOException e) {
try {
serverSocket.close();
return;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}).start();
Scanner in = new Scanner(System.in);
String msg = "";
while(true) {
msg = in.nextLine();
if(msg.equalsIgnoreCase("bye")) {
serverSocket.close();
return;
}
os = client.getOutputStream();
os.write(msg.getBytes());
}
}
}
public class TCPClient {
public static void main(String[] args) throws Exception {
Socket client = new Socket("localhost",8888);
new Thread(new Runnable() {
@Override
public void run() {
while(true) {
InputStream is;
try {
is = client.getInputStream();
byte[] bytes = new byte[1024];
int len = is.read(bytes);
System.out.println("服务器说:"+new String(bytes,0,len));
} catch (IOException e) {
try {
client.close();
return;
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
}).start();
OutputStream os = client.getOutputStream();
Scanner in = new Scanner(System.in);
String msg = "";
while(true) {
msg = in.nextLine();
if(msg.equalsIgnoreCase("bye")) {
client.close();
return;
}
os = client.getOutputStream();
os.write(msg.getBytes());
}
}
}
运行结果 |
---|
来源:CSDN
作者:Ys2025
链接:https://blog.csdn.net/Asdzxc968/article/details/104769926