利用Datagramsocket创建连接,实现UDP,并使用多线程实现教师端和学生端的聊天。 接收端的程序如下:
package chats;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
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 {
this.socket = new DatagramSocket(this.port);
} catch (Exception var4) {
var4.printStackTrace();
}
}
public void run() {
while(true) {
try {
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
this.socket.receive(packet);
byte[] bytes1 = packet.getData();
String ans = new String(bytes1, 0, bytes1.length);
System.out.println(this.msgfrom + ":" + ans);
if (!ans.trim().equals("bye")) {
continue;
}
this.socket.close();
} catch (Exception var5) {
var5.printStackTrace();
}
return;
}
}
}
数据发送端的写法:
package chats;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class talksend implements Runnable {
DatagramSocket socket = null;
BufferedReader bufferedReader = null;
private int fromport;
private String toip;
private int toport;
public talksend(int fromport, String toip, int toport) {
this.fromport = fromport;
this.toport = toport;
this.toip = toip;
try {
this.socket = new DatagramSocket(fromport);
this.bufferedReader = new BufferedReader(new InputStreamReader(System.in));
} catch (Exception var5) {
var5.printStackTrace();
}
}
public void run() {
while(true) {
try {
String data = this.bufferedReader.readLine();
byte[] Data = data.getBytes();
DatagramPacket packet = new DatagramPacket(Data, 0, Data.length, new InetSocketAddress(this.toip, this.toport));
this.socket.send(packet);
String ques = new String(Data, 0, Data.length);
if (!ques.trim().equals("bye")) {
continue;
}
this.socket.close();
} catch (Exception var5) {
var5.printStackTrace();
}
return;
}
}
}
学生类和教师类,创建两个线程,分别为发送和接受。
package chats;
public class talkstudent {
public talkstudent() {
}
public static void main(String[] args) {
(new Thread(new talksend(7777, "localhost", 9999))).start();
(new Thread(new talkreceive(8888, "老师"))).start();
}
}
package chats;
public class talkteacher {
public talkteacher() {
}
public static void main(String[] args) {
(new Thread(new talksend(5555, "localhost", 8888))).start();
(new Thread(new talkreceive(9999, "学生"))).start();
}
}
附: java通过URL类,下载网络资源。 首先可以通过网络抓包的方式,获取目标视频或音乐的url的值。 先创建一个URL对象,之后通过openconnection方法创建连接,由于大多数网站使用https协议,将其进行强制类型转换为HttpURLConnection,万物皆为流,使用inputStream读取http连接,在使用FileOutputStream将内容下载到指定的文件位置。
package URLtest;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class URLtest {
public static void main(String[] args) throws Exception{
URL url=new URL("https://m701.music.126.net/20210908204202/62ad2629c5959aa955d40bbe360fb3fd/jdyyaac/obj/w5rDlsOJwrLDjj7CmsOj/10534066865/ec0a/9bbc/3590/b4d04ce9553a85b39d002fbdfa1938e1.m4a");
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
InputStream inputStream=httpURLConnection.getInputStream();
FileOutputStream fos=new FileOutputStream("b4d04ce9553a85b39d002fbdfa1938e1.m4a");
byte[] bytes=new byte[1024];
int len;
while((len=inputStream.read(bytes))!=-1){
fos.write(bytes,0,bytes.length);
}
fos.close();
inputStream.close();
httpURLConnection.disconnect();
}
}
|