网络编程(3)
UDP实现简单的聊天
接收端
package com.yangxu.chat;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpReceiver {
public static void main(String[] args) throws Exception {
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 receive = new String(data, 0, packet.getLength());
System.out.println(receive);
if (receive.equals("bye")){
break;
}
}
socket.close();
}
}
发送端
package com.yangxu.chat;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class UdpSender {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(8888);
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true){
String s = reader.readLine();
byte[] bytes = s.getBytes();
DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length,new InetSocketAddress("localhost",6666));
socket.send(packet);
if (s.equals("bye")){
break;
}
}
socket.close();
}
}
UDP实现简单“在线咨询”
老师的“客户端”
package com.yangxu.chat2;
public class ChatOfTeacher {
public static void main(String[] args) {
new Thread(new ChatSender("localhost",8888,6666)).start();
new Thread(new ChatReceive("学生",7777)).start();
}
}
学生的“客户端”
package com.yangxu.chat2;
public class ChatOfStudent {
public static void main(String[] args) {
new Thread(new ChatSender("localhost",7777,5555)).start();
new Thread(new ChatReceive("老师",8888)).start();
}
}
发送端
package com.yangxu.chat2;
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;
public class ChatSender implements Runnable{
DatagramSocket socket = null;
BufferedReader reader = null;
private String toIP;
private int toPort;
private int fromPort;
public ChatSender(String toIP, int toPort, int fromPort) {
this.toIP = toIP;
this.toPort = toPort;
this.fromPort = fromPort;
try {
socket = new DatagramSocket(fromPort);
} catch (SocketException e) {
e.printStackTrace();
}
reader = new BufferedReader(new InputStreamReader(System.in));
}
@Override
public void run() {
while (true){
String s = null;
try {
s = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
byte[] bytes = s.getBytes();
DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length,new InetSocketAddress(this.toIP,this.toPort));
try {
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
}
if (s.equals("bye")){
break;
}
}
socket.close();
}
}
接收端
package com.yangxu.chat2;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class ChatReceive implements Runnable{
DatagramSocket socket = null;
private int port;
private String name;
public ChatReceive(String name, int port) {
this.port = port;
this.name = name;
try {
socket = new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try {
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length);
socket.receive(packet);
byte[] data = packet.getData();
String receive = new String(data, 0, packet.getLength());
System.out.println(name+":"+receive);
if (receive.equals("bye")){
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
URL
定义啥的建议参考《计算机网络技术》就大学厚厚的红色那本书
用TomCat下载网易云歌曲千千阙歌
package com.yangxu.url;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class UrlDownLoad {
public static void main(String[] args) throws Exception {
URL url = new URL("https://m804.music.126.net/20220304194033/c8b3c4167301017e5f6a7980d308a30b/jdyyaac/5153/020b/070f/f96cdc5c8e5ae945924f0ec513061b98.m4a?authSecret=0000017f54a4608b1ae60aaba035f194");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("test.m4a");
byte[] bytes = new byte[1024];
int len;
while ((len=inputStream.read(bytes))!=-1){
fos.write(bytes,0,len);
}
urlConnection.disconnect();
inputStream.close();
fos.close();
}
}
|