JAVA— 网络编程
- U D P 多线程在线咨询
- URL下载网上资源
通过URL下载网上资源下载
端口
TCP实现聊天
U D P 多线程在线咨询
端口
端口表示计算机上一个程序的进程
- 不同的进程有不同的端口号 用来区分软件
- 被规定 0~65535
- 端口分类 (1)公认端口(Well Known Ports)
(2)注册端口(Registered Ports)
(3)动态和/或私有端口(Dyanmic and /or Private Ports)
查看所有的端口
代码语言:javascript复制netstat -ano #查看所有的端口
netstat -ano findstr "5900" # 查看指定的端口
代码语言:javascript复制import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestIntAddress {
public static void main(String[] args) {
try {
// 查询本机地址
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress1);
// 查询百度的 主机地址
InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress2);
System.out.println(inetAddress2.getAddress());
// 规范的名字
System.out.println(inetAddress2.getCanonicalHostName());
//查询 ip
System.out.println(inetAddress2.getHostAddress());
//查询域名
System.out.println(inetAddress2.getHostName());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
TCP实现聊天
客户端:
- 连接服务器 Socket
- 发送消息
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
// 客户端
public class TcpClientDemo01 {
public static void main(String[] args) {
OutputStream os =null;
Socket socket = null;
try {
// 知道 服务器地址 和端口
InetAddress serverIp= InetAddress.getByName("127.0.0.1");
int port = 9999;
// 创建一个socket 连接
socket = new Socket(serverIp,port);
// 3 发送消息 IO 流
os = socket.getOutputStream();
os.write("你好啊,大哥哥,每一天都要开开心心哦 六一儿童节礼物还没有给你!".getBytes());
} catch (Exception e) {
e.printStackTrace();
}
finally {
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务器
- 建立服务的端口 ServerSocket
- 等待用户的链接 accept
- 接收用户的消息
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
// 服务器端
public class TcpServerDemo01 {
public static void main(String[] args) {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
// 我得有一个地址
serverSocket = new ServerSocket(9999);
// 等待客户端连接过来
socket = serverSocket.accept();
// 读取客户端的消息
is = socket.getInputStream();
//管道流
baos = new ByteArrayOutputStream();
byte[] buffer =new byte[1024];
int len;
while((len = is.read(buffer)) != -1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
UDP多线程在线咨询
代码语言:javascript复制// 第一个类 Send 发送
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 reader = null;
private int fromIP;
private String toIP;
private int toPort;
public TalkSend(int fromIP, String toIP, int toPort) {
this.fromIP = fromIP;
this.toIP = toIP;
this.toPort = toPort;
try {
socket = new DatagramSocket(fromIP);
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();
}
}
代码语言:javascript复制// 第二个类 Receive 接收
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
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(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);
socket.receive(packet);
// 阻塞时接收包裹
// 断开连接 bye
byte[] data = packet.getData();
String receiveDate = new String (data, 0 ,data.length);
System.out.println(msgFrom ":" receiveDate);
if(receiveDate.equals("bye")){
break;
}
}catch (IOException e){
e.printStackTrace();
}
}
socket.close();
}
}
测试类 学生 和 老师
代码语言:javascript复制public class TalkStudent {
public static void main(String[] args) {
// 开启两个线程
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceive(8888,"老师")).start();
}
}
代码语言:javascript复制public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceive(9999,"学生")).start();
}
}
- 效果图
不知道 IDEA 怎么控制台怎分屏
URL网上资源下载
- 本次是以从网上下载网易云音乐为例:
- 下载的必要要素 必须知道 网上资源的URL 才可以进行下载
- 首先去浏览器上搜索 然后进入浏览器控制台 找到音频的 URL 然后这个URL 就是我们进行下载的路径。
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class UrlDown {
public static void main(String[] args) throws Exception {
// 下载地址
URL url = new URL("http://m10.music.126.net/20200602120835/02638bb77e5403d65fdb86d86f2863d6/yyaac/obj/wonDkMOGw6XDiTHCmMOi/2703362268/aa27/f3dd/abf2/062dde2b67cf0cf1ad5d242fae90f156.m4a");
// 这个里面放的地址就是 之前在网上找到的音频的url
// 连接到这个资源 HTTP
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("f156.m4a");
// 这个f156.m4a “.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();
}
}
成功就会在idea中出现一个 .m4a 格式的文件 到本地文件夹中 通过音乐软件打开即可。能播放就是成功的。
初次写这样的博客 不足之处 请见谅。