传输协议(UDP)

一、网络爬虫(虚拟)

  1. 获取URL
    URL url=new URL("https://www.jd.com")
  2. 下载资源
  3. 分析
  4. 处理

代码实现(取京东的数据):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
public class pachongle{
public static void main(String[] args) throws Exception {//抛出异常
URL url=new URL("https://www.jd.com");//获取京东URL
InputStream is=url.openStream();//输入流
BufferedReader br=new BufferedReader(new InputStreamReader(is,"UTF-8"));/京东的是UTF-8
String msg=null;//定义一个字符串msg
while(null!=(msg=br.readLine()))
{
System.out.println(msg);//输出
}
br.close();//一定要记得br对象用完之后关闭

}
}

代码结果如下:
京东的所有源文件(浏览器中的源文件)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
public class pachongle{
public static void main(String[] args) throws Exception {
URL url=new URL("https://www.dianping.com");//换成了点评网址
InputStream is=url.openStream();
BufferedReader br=new BufferedReader(new InputStreamReader(is,"UTF-8"));
String msg=null;
while(null!=(msg=br.readLine()))
{
System.out.println(msg);
}
br.close();

}
}
  • 提示403问题(有权限)通过简单的抓取无法做到
1
2
3
4
5
6
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: https://www.dianping.com
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at java.net.URL.openStream(Unknown Source)
at pachong.pachongle.main(pachongle.java:9)
  • 模拟浏览器HttpURLConnection
  1. //get从服务器获取资源
    对象.setRequestMethod("GET");
  2. //post往服务器上推数据
    对象.setRequestProperty("XXXXX");
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
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class pachongle{
public static void main(String[] args) throws Exception {
URL url=new URL("https://www.dianping.com");
//用HttpURLConnection类
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
//get从服务器获取资源
conn.setRequestMethod("GET");
//post往服务器上推数据
conn.setRequestProperty("XXXXX");

BufferedReader br=new BufferedReader(new InputStreamReader(conn.InputStream(),"UTF-8"));
String msg=null;
while(null!=(msg=br.readLine()))
{
System.out.println(msg);
}
br.close();

}
}

二、UDP(非面向连接)

  1. 使用基于UDP协议的Socket网络编程实现
  2. 不需要利用IO流实现数据传输
  3. 每个数据发送单元被统一封装成数据包。发送方将数据包发送到网络中,数据包在网络中寻找目的地。

UDP基本概念:

  • DatagramSocket:用于发送/接收数据包的套接字
  1. send(DatagramPacket 对象)
  2. receive(DatagramPacket 对象)
  3. close()
  • DatagramPacket:数据包
    主要创建对象new(容器,0,长度)

发送端(client)

1
2
3
4
5
1.使用DatagramSocket端口创建接收端
2.准备数据(转成字节数组)
3.封装成DatagramPacket包裹(指定目的地)
4.发送包裹send(DatagramPacket p)
5.释放资源
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class udpClient{
public static void main(String[] args) throws Exception{
System.out.println("发送方启动中...");
//1.使用DatagramSocket端口创建接收端
DatagramSocket client=new DatagramSocket(8888);
//2.准备数据(转成字节数组)
String data="西安工程大学";
byte[] datas=data.getBytes();
//3.封装成DatagramPacket包裹(指定目的地)
DatagramPacket packet=new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost",9999));
//4.发送包裹send(DatagramPacket p)
client.send(packet);
//5.释放资源
client.close();

}
}

接收端(server)

1
2
3
4
5
1.使用DatagramSocket端口创建接收端
2.准备容器,封装成DatagramPacket包裹
3.阻塞式接收包裹receive(DatagramPacket p)
4.分析数据 getData()/getLength()
5.释放资源
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class udpServer{
public static void main(String[] args) throws Exception {
System.out.println("接收方启动中...");
//1.使用DatagramSocket端口创建接收端
DatagramSocket server=new DatagramSocket(9999);//发送的包里面也必须是9999
//2.准备容器,封装成DatagramPacket包裹
byte[] container=new byte[1024*60];
DatagramPacket packet=new DatagramPacket(container,0,container.length);
//3.阻塞式接收包裹receive(DatagramPacket p)
server.receive(packet);
//4.分析数据 getData()/getLength()
byte[] datas=packet.getData();
int len=packet.getLength();
System.out.println(new String(datas,0,len));
//5.释放资源
server.close();

}
}

代码结果如下:


  • 多次交流的前提:(多次输入)

发送端(client)

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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class udpClient{
public static void main(String[] args) throws Exception{
System.out.println("发送方启动中...");
//1.使用DatagramSocket端口创建接收端
DatagramSocket client=new DatagramSocket(8888);
//2.准备数据(转成字节数组)
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));//使用这个控制台输入

while(true) //多了一个while循环
{
String data=reader.readLine();//对象不断输入

byte[] datas=data.getBytes();
//3.封装成DatagramPacket包裹(指定目的地)
DatagramPacket packet=new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost",9999));
//4.发送包裹send(DatagramPacket p)
client.send(packet);
if(data.equals("bye")) //当输入的数据为bye时输出
{
break;
}
}
//5.释放资源
client.close();
}
}

接收端(server)

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
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class udpServer{
public static void main(String[] args) throws Exception {
System.out.println("接收方启动中...");
//1.使用DatagramSocket端口创建接收端
DatagramSocket server=new DatagramSocket(9999);//发送的包里面也必须是9999
//2.准备容器,封装成DatagramPacket包裹
while(true) { //比之前的多了一个while循环
byte[] container=new byte[1024*60];
DatagramPacket packet=new DatagramPacket(container,0,container.length);
//3.阻塞式接收包裹receive(DatagramPacket p)
server.receive(packet);
//4.分析数据 getData()/getLength()
byte[] datas=packet.getData();
int len=packet.getLength();
String data=new String(datas,0,len);
System.out.println(data);
if(data.equals("bye")) //当最后的数据为bye就结束
break;
}
//5.释放资源
server.close();

}
}

三、UDP多次交流(多线程)

TalkReceive类

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
package UDP;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
public class TalkReceive implements Runnable {
private DatagramSocket server;
//1.
public TalkReceive(int port) {
try {
server=new DatagramSocket(port);//抛出异常
} catch (SocketException e) {
e.printStackTrace();
}
}

//2.

@Override
public void run() { //将封装udpServer中的while循环内容
while(true) {
byte[] container=new byte[1024*60];
DatagramPacket packet=new DatagramPacket(container,0,container.length);
//3.阻塞式接收包裹receive(DatagramPacket p)
try {
server.receive(packet);
//4.分析数据 getData()/getLength()
byte[] datas=packet.getData();
int len=packet.getLength();
String data=new String(datas,0,len);
System.out.println(data);
if(data.equals("bye"))
{
break;
}

} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}

//5.释放资源
server.close();
}

}
}

TalkSend类

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
56
57
58
59
60
61
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;

/*
* 封装updTalkClient
*/
public class TalkSend implements Runnable{
//1
private DatagramSocket client;
private BufferedReader reader;
private String toIP;//对方地址
private int toPort;//对方端口
public TalkSend(int port,String toIP,int toPort) {
this.toIP=toIP;
this.toPort=toPort;
try {
client=new DatagramSocket(port);
reader=new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}
//2
@Override
public void run() { //直接将updTalkClient中的while拿过来
while(true)
{
String data;
try {
data = reader.readLine();
byte[] datas=data.getBytes();
//3.封装成DatagramPacket包裹(指定目的地)
DatagramPacket packet=new DatagramPacket(datas,0,datas.length,new InetSocketAddress(this.toIP,this.toPort));//将本地主机换成toIP和toPort端口
//4.发送包裹send(DatagramPacket p)
try {
client.send(packet);
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
if(data.equals("bye"))
{
break;
}
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}

//5.释放资源
client.close();
}
}


}

然后构建两个对话类

TalkTeacher类

1
2
3
4
5
6
7

public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkReceive(9999)).start();//接收9999
new Thread(new TalkSend(5555,"localhost",8888)).start();//8888发送
}
}

TalkStudent类

1
2
3
4
5
6
7
8
9
/*
* 加入多线程,实现双向交流 模拟在线咨询
*/
public class TalkStudent {
public static void main(String[] args) {
new Thread(new TalkSend(7777,"localhost",9999)).start();//9999发送
new Thread(new TalkReceive(8888)).start();//接收8888
}
}

四、TCP的使用(面向连接可靠)
相对于UDP而言,TCP是一种面向连接的通讯方式,提供一种可靠的方法。

×

纯属好玩

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
,