Java学习day12
目录
TCP
Tomcat
服务端
- 自定义 S
- Tomcat服务器 S:java后台开发
客户端 - 自定义 C
- 浏览器 B
UDP
发短信:不用链接,但是需要知道对方的地址
Datagram Packet&&Datagram Socket
UdpSendDemo01
//不需要连接服务器
public class UdpSendDemo01 {
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,9090);
//3.发送包
socket.send(packet);
//4.关闭流
socket.close();
}
}
UdpReceiveDemo01
//等待客户端的链接
public class UdpReceiveDemo01 {
public static void main(String[] args) throws Exception {
//开放端口
DatagramSocket socket = new DatagramSocket(9090);
//接收数据包
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();
}
}
运行结果:
127.0.0.1
你好服务器
UDP不需要建立连接,也没有客户端和服务端的概念,只有发送端和接收端
循环发送和接收
UdpSenderDemo01
public class UdpSenderDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(8888);
//准备数据:控制台读取System.in
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 (data.equals("bye")){
break;
}
}
socket.close();
}
}
UdpReceiveDemo01
public class UdpReceiveDemo01 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(6666);
while (true) {
//准备接受包裹
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container,0,container.length);
socket.receive(packet);//阻塞式接受包裹
//断开连接 bye
byte[] data = packet.getData();
String receiveData = new String(data, 0, data.length);
System.out.println(receiveData.trim());
if(receiveData.trim().equals("bye")){
break;
}
}
socket.close();
}
}
接收端在判断时用.trim
去除多余空格
运行结果:
Sender输入:
test1
test2
bye
程序结束
Receive接收:
test1
test2
bye
程序结束
在线咨询
TalkSend
public class TalkSend implements Runnable {
DatagramSocket socket = null;
BufferedReader reader = null;
private int fromPort;
private String toIP;
private int toPort;
public TalkSend(int fromPort, String toIP, int toPort) {
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;
try {
socket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (Exception 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 (data.equals("bye")) {
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
socket.close();
}
}
TalkReceive
public class TalkReceive implements Runnable{
DatagramSocket socket = null;
private int port;
private String msgFrom;
public TalkReceive(int port,String msgFrom){
this.port=port;
this.msgFrom=msgFrom;
try {
socket = new DatagramSocket(this.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);//阻塞式接受包裹
//断开连接 bye
byte[] data = packet.getData();
String receiveData = new String(data, 0, data.length);
System.out.println(msgFrom+":"+receiveData.trim());
if(receiveData.trim().equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
TalkTeacher
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生")).start();
}
}
TalkStudent
public class TalkStudent {
public static void main(String[] args) {
//开启两个线程
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
}
这样就可以实现双方实时交流
注:
- 同一个实例,发送的端口和接受的端口不是一个端口。
- 本例中TalkReceive中的msgFrom对象,是接收方写的,是本地定义接收到的消息的发送方的名称。
- 线程确实好用,有更加深刻的认识。
- 如本地和对方电脑各自打开端口,应该可以用此程序和IP及端口号完成信息的传输。
URL
统一资源定位符:定位资源的,定位互联网上的某一个资源
DNS:域名解析 www.baidu.com -> xxx.x..x..x
https://www.baidu.com
协议://ip地址:端口/项目名/资源
UrlDown
public class UrlDown {
public static void main(String[] args) throws Exception {
//1.下载地址
//URL url = new URL("http://localhost:8080/hufo/Security.txt");
URL url = new URL("https://m801.music.126.net/20211219174439/ab07c905816680e1bbe177a25affa306/jdyyaac/050b/0e5e/040f/2ac9f88e439461bdc4f23e6ed61ccbe9.m4a");
//2.连接到这个资源HTTP
HttpURLConnection urlConnection =(HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
//FileOutputStream fos = new FileOutputStream("SecurityText.txt");
FileOutputStream fos = new FileOutputStream("a spring without you.m4a");
byte[] buffer = new byte[1024];
int len;
while((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);//写出这个数据
}
fos.close();
inputStream.close();
urlConnection.disconnect();//断开连接
}
}
这样就能直接从网页下载资源了,如果下载别的音乐、视频,浏览器中点击F12打开开发者模式,在网络里找到对应格式的文件,复制名字,在最前面加上“/”,并加在当前网址的最后,然后就可以通过URL下载。