端口
端口表示计算机上的一个程序的进程
-
不同的进程有不同的端口号,用来区分软件!
-
被规定0~65535
-
TCP,UDP:65535*2 tcp80和udp80不冲突
-
端口分类
-
公有端口 0~1023
-
HTTP:80
-
HTTPS:443
-
FTP:21
-
Telnet:23
-
-
程序注册端口:1024~49151,分配用户或者程序
-
Tomcat:8080
-
MySql : 3306
-
Oracle:1521
-
-
动态、私有:49152~65535
-
netstat -ano #查看所有端口
netstat -ano|findstr "5900" #查看指定的端口
tasklist|findstr "13956" #通过pid查看指定程序
#Ctrl+Shift+Esc 打开任务管理器的快捷键
通信协议
TCP/IP协议簇
TCP(transmission control protocol):传输控制协议(打电话)
-
连接,稳定
-
三次握手,四次挥手
-
客户端,服务端
-
传输完成,释放连接,效率低
UDP(user datagram protocol):用户数据报协议(发短信)
-
不连接,不稳定
-
客户端和服务端没有明确的界限
-
不管有没有准备好,都可以发给你
TCP实例
客户端和服务端进行通信
客户端
-
连接服务器socket
-
发送消息
package internetProgram.tcp;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
/**
* @ClassName TcpClient
* @Description TODO
* @Author QiuYiping
* @Date 2022/4/9 12:44
*/
public class TcpClient {
public static void main(String[] args) throws IOException {
Socket socket = null;
OutputStream os = null;
try {
//1.通过服务器的ip地址和端口号建立连接
InetAddress serverIp = InetAddress.getByName("127.0.0.1");
int port = 9999;
//2.建立一个socket连接用于发送数据
socket = new Socket(serverIp, port);
//3.发送消息 IO流
os = socket.getOutputStream();
os.write("你好,欢迎学习狂神说java".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
os.close();
socket.close();
}
}
}
服务端
-
建立服务的端口ServerSocket
-
等待用户的链接 accept
-
接收客户的信息
package internetProgram.tcp;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @ClassName TcpServer
* @Description TODO
* @Author QiuYiping
* @Date 2022/4/9 12:44
*/
public class TcpServer {
public static void main(String[] args) throws IOException {
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//1.首先得有一个服务端的端口
ServerSocket serverSocket = new ServerSocket(9999);
//2.此时获取的socket连接即为客户端要发送数据的那个
socket = serverSocket.accept();
is = socket.getInputStream();
//3.接收信息
//管道流做中转
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
baos.close();
is.close();
socket.close();
}
}
}
运行结果:
文件上传
-
客户端上传文件
package internetProgram.tcp;
import java.io.*;
import java.net.Socket;
/**
* @ClassName TcpFileTransfer
* @Description TODO
* @Author QiuYiping
* @Date 2022/4/9 14:06
*/
public class TcpFileTransfer {
public static void main(String[] args) throws IOException {
Socket socket = null;
OutputStream os = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
socket = new Socket("127.0.0.1", 9003);
FileInputStream fis = new FileInputStream(new File("sanrenxing.JPG"));
int len = 0;
byte[] bytes = new byte[1024];
os = socket.getOutputStream();
while ((len = fis.read(bytes)) != -1) {
os.write(bytes, 0, len);
}
//通知服务器我已经结束了,如果不加这行代码则服务器和客户端都会阻塞
//s.shutdownOutput();关闭的是客户端的输出流,同时服务器端的输入流也随之关闭。
socket.shutdownOutput();
//确定服务器接收完毕,才能够断开连接
is = socket.getInputStream();
baos = new ByteArrayOutputStream();
int len2 = 0;
byte[] bytes2 = new byte[1024];
while ((len2 = is.read(bytes2)) != -1) {
baos.write(bytes2, 0, len2);
}
System.out.println(baos.toString());
//关闭资源
is.close();
baos.close();
fis.close();
s.close();
socket.close();
}
}
-
服务端接收文件
package internetProgram.tcp;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @ClassName TcpFileReceive
* @Description TODO
* @Author QiuYiping
* @Date 2022/4/9 14:07
*/
public class TcpFileReceive {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream is = null;
FileOutputStream fos = null;
serverSocket = new ServerSocket(9003);
socket = serverSocket.accept();
is = socket.getInputStream();
int len = 0;
byte[] bytes = new byte[1024];
fos = new FileOutputStream("receive.JPG");
while ((len = is.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
//通知客户端我接收完毕了
OutputStream os = socket.getOutputStream();
os.write("我接收完毕了,你可以断开了".getBytes());
//关闭资源
os.close();
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
运行结果:
注:如果客户端不加socket.shutdownOutput()方法会造成客户端和服务端都阻塞,服务不会结束
UDP实例
udp实现传数据
-
发送端
package internetProgram.udp;
import java.io.IOException;
import java.net.*;
/**
* @ClassName UDPClient
* @Description TODO
* @Author QiuYiping
* @Date 2022/4/9 17:23
*/
//不需要连接服务器
public class UdpClient {
public static void main(String[] args) throws IOException {
//1.建立一个socket
DatagramSocket socket = new DatagramSocket();
//2.发送包
String msg = "你好啊!";
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
//数据,数据的起始长度,要发送给谁
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);
//3.发送包
socket.send(packet);
//4.关闭流
socket.close();
}
}
-
接收端
package internetProgram.udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* @ClassName UdpServer
* @Description TODO
* @Author QiuYiping
* @Date 2022/4/9 17:34
*/
public class UdpServer {
public static void main(String[] args) throws IOException {
//1.开放端口
DatagramSocket socket = new DatagramSocket(9090);
//2.接收数据包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet); //阻塞接收
//打印ip地址
System.out.println(packet.getAddress().getHostAddress());
//打印消息内容
System.out.println(new String(packet.getData(), 0, packet.getLength()));
//3.关闭连接
socket.close();
}
}
运行结果:
聊天实现
-
发送端
package internetProgram.udp.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;
public class UdpSender {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket();
while (true) {
//读取控制台输入的数据
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 6666));
socket.send(packet);
if (data.equals("bye")) {
break;
}
}
socket.close();
}
}
-
接收端
package internetProgram.udp.chat;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpReceiver {
public static void main(String[] args) throws IOException {
DatagramSocket socket = new DatagramSocket(6666);
while (true) {
//准备接收包裹
byte[] content = new byte[1024];
DatagramPacket packet = new DatagramPacket(content, 0, content.length);
socket.receive(packet); //阻塞式接收包裹
//packet即为接收的数据
byte[] data = packet.getData();
String receivedData = new String(data, 0, packet.getLength());
//如果为bye,则断开连接
System.out.println(receivedData);
if (receivedData.equals("bye")) {
break;
}
}
socket.close();
}
}
运行结果:
在线咨询(两个人都可以是发送方,也可以是接收方)
多线程实现
-
发送方
package internetProgram.udp.dualChat;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class TalkSend implements Runnable {
private String toIp;
private int toPort;
DatagramSocket socket = null;
BufferedReader reader = null;
public TalkSend(String toIp, int toPort) {
this.toIp = toIp;
this.toPort = toPort;
try {
socket = new DatagramSocket();
//读取控制台输入的数据
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(toIp, toPort));
socket.send(packet);
if (data.equals("bye")) {
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
-
接收方
package internetProgram.udp.dualChat;
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[] content = new byte[1024];
DatagramPacket packet = new DatagramPacket(content, 0, content.length);
socket.receive(packet); //阻塞式接收包裹
//packet即为接收的数据
byte[] data = packet.getData();
String receivedData = new String(data, 0, packet.getLength());
//如果为bye,则断开连接
System.out.println(msgFrom + ": " + receivedData);
if (receivedData.equals("bye")) {
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
socket.close();
}
}
-
发送信息和接收信息实例(老师)
package internetProgram.udp.dualChat;
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend("localhost", 9999)).start();
new Thread(new TalkReceive(7777, "学生")).start();
}
}
-
发送信息和接收信息实例(学生)
public class TalkStudent {
public static void main(String[] args) {
new Thread(new TalkSend("localhost", 7777)).start();
new Thread(new TalkReceive(9999,"老师")).start();
}
}
运行结果:
分析:学生向老师发送bye之后,学生作为发送方已经关闭socket连接了,而老师作为接收端,收到了bye之后也关闭了接收端的socket,因此学生发送给老师的信息无法接收,此时只能是老师作为发送端,而学生作为接收方。
老师发送bye之后,作为发送端也结束了,因此老师端的两个run()方法都结束了,而学生作为接收方收到了bye之后,接收方的socke也关闭连接了,因此学生端的凉热run()方法都结束了
URL
uniform resource locator:统一资源定位符
文件下载
public class URLDownload {
public static void main(String[] args) throws IOException {
URL url = new URL("https://m10.music.126.net/20201023153940/81460b2d84d025c95423cedccea22610/yyaac/0708/0652/0508/0b9b6827b718aa223af92bd52aa2424f.m4a");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream is = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("music.m4a");
int len;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, buffer.length);
}
fos.close();
is.close();
urlConnection.disconnect();
}
}