前言:

本文内容:UDP聊天实现、UDP多线程在线咨询、UDP下载网络资源

推荐免费Java网络编程实战讲解视频:【狂神说Java】网络编程实战讲解_哔哩哔哩_bilibili

Java网络编程(NET)笔记代码下载地址:

蓝奏云:下载地址 密码:joker

百度云:下载地址 提取码:jpvx

UDP聊天实现

循环发送消息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.jokerdig.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;

/**
* @author Joker大雄
* @data 2021/9/4 - 10:37
**/
public class UdpSenderDemo01 {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(8888);

//准备数据,控制台读取 System.in
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

//循环发送
while(true){
String data=reader.readLine();

byte[] bytes = data.getBytes();
DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length,new InetSocketAddress("localhost",6666));

socket.send(packet);
if(data.equals("bye")){
break;
}
}
}
}

循环接收消息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.jokerdig.chat;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
* @author Joker大雄
* @data 2021/9/4 - 10:37
**/
public class UdpServerDemo01 {
public static void main(String[] args) throws IOException{
DatagramSocket socket = new DatagramSocket(6666);

while (true){
//准备接收包裹
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length);
socket.receive(packet);
//断开
byte[] data = packet.getData();
String s = new String(data,0, packet.getLength());//packet.length()会无法停止
System.out.println(s);
if(s.equals("bye")){
break;
}
}
socket.close();//释放资源
}
}

UDP多线程在线咨询

在线咨询:

两个人都可以是发送方,也都可以是接收方。

发送端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.jokerdig.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;

/**
* @author Joker大雄
* @data 2021/9/4 - 12:49
**/
public class TalkSeed implements Runnable{

DatagramSocket socket=null;
BufferedReader reader=null;
private int fromPort;
private String toIp;
private int toPort;
//构造器
public TalkSeed(int fromPort, String toIp, int toPort) {
this.fromPort = fromPort;
this.toIp = toIp;
this.toPort = toPort;
try {
socket = new DatagramSocket(fromPort);
//准备数据,控制台读取 System.in
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}

@Override
public void run() {
//循环发送
while(true){
String data= null;
try {
data = reader.readLine();
byte[] bytes = data.getBytes();
DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length,new InetSocketAddress(this.toIp,this.toPort));
socket.send(packet);

if(data.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

接收端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.jokerdig.chat;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

/**
* @author Joker大雄
* @data 2021/9/4 - 13:23
**/
public class TalkReceive implements Runnable{
DatagramSocket socket=null;
DatagramPacket packet=null;
private int port;
private String fromMsg;

public TalkReceive(int port,String fromMsg) {
this.port = port;
this.fromMsg = fromMsg;
try {
socket=new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}

@Override
public void run() {

while (true){
//准备接收包裹
try {
byte[] bytes = new byte[1024];
packet = new DatagramPacket(bytes,0,bytes.length);
socket.receive(packet);
//断开
byte[] data = packet.getData();
String s = new String(data,0, packet.getLength());//packet.length()会无法停止
System.out.println(fromMsg+"发送:"+s);
if(s.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();//释放资源
}
}

用户:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.jokerdig.chat;

/**
* @author Joker大雄
* @data 2021/9/4 - 13:35
**/
//用户
public class TalkUser {
public static void main(String[] args) {
//开启两个线程
new Thread(new TalkSeed(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"客户")).start();
}
}

客户:

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.jokerdig.chat;

/**
* @author Joker大雄
* @data 2021/9/4 - 13:36
**/
//客户
public class TalkClient {
public static void main(String[] args) {
new Thread(new TalkSeed(6666,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"用户")).start();
}
}

UDP下载网络资源

URL:统一资源定位符,定位互联网某一资源。

DNS域名解析:www.jokerdig.com


协议://ip地址:端口/项目名/资源

URL方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.jokerdig.url;

import java.net.MalformedURLException;
import java.net.URL;

/**
* @author Joker大雄
* @data 2021/9/4 - 13:56
**/
public class UrlDemo {
public static void main(String[] args) throws MalformedURLException {
URL url= new URL("https://localhost:8080/helloword/index.jsp?username=name&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());//参数
}
}

URL文件下载:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.jokerdig.url;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
* @author Joker大雄
* @data 2021/9/4 - 14:05
**/
public class UrlFileDownload {
public static void main(String[] args) throws Exception{
//下载地址
URL url = new URL("https://jokerdig.com/images/avatar.png");
//连接资源
HttpURLConnection http=(HttpURLConnection) url.openConnection();
InputStream inputStream = http.getInputStream();
FileOutputStream file = new FileOutputStream("avatar.png");

byte[] buffer = new byte[1024];
int len;
while((len=inputStream.read(buffer))!=-1){
file.write(buffer,0,len);//写出数据
}
//释放资源
file.close();
inputStream.close();
http.disconnect();//断开
}
}