java网络编程
1.4端口Port
端口表示计算机上的一个程序的进程
-
不同的进程有不同的端口号!用来区分软件!
-
被规定0~65535
-
TCP,UDP :单个协议下,端口号不冲突
-
端口分类
-
公有端口0~1023 一般不使用
- HTTP端口:80
- HTTPS端口:443
- FTP端口:21
- Telent:23
-
程序注册端口:1024~49151,分配给用户或者程序
-
动态,私有:49152~65535
-
import java.net.InetSocketAddress; public class Test1_4 { public static void main(String[] args) { InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1",8080); System.out.println(socketAddress); System.out.println(socketAddress.getAddress()); System.out.println(socketAddress.getPort());//端口 System.out.println(socketAddress.getHostName());//地址 } }
-
1.5通信协议
网络通信协议:速率,传输码率,代码结构,传输控制…
TCP UDP对比
TCP:可靠传输
- 连接,稳定
三次握手``四次握手
- 客户端,服务端
- 传输完成,释放连接,效率低
UDP:不可靠传输
- 不连接,不稳定
- 客户端,服务端:没有明确的界限
- 不管有没有准备好,都可以发给你
- 导弹
1.6TCP实现聊天
客户端
- 连接服务器Socket
- 发送消息
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class TcpClient {
public static void main(String[] args) throws IOException {
Socket socket = null;
OutputStream os =null;
try {
//1.要知道服务器的地址,端口号
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
//2.创建一个socket连接
socket = new Socket(serverIP, port);
//发送消息IO流
os = socket.getOutputStream();
os.write("你好啊!".getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os!=null){
try {
os.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 TcpSever {
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();
}finally {
if(baos!=null ){
try{
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is!=null ){
try{
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null ){
try{
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(serverSocket!=null ){
try{
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
文件上传
服务端
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TcpSever1 {
public static void main(String[] args) throws IOException {
//1.创建服务端口
ServerSocket serverSocket = new ServerSocket(9000);
//2.监听客户端的连接
Socket socket = serverSocket.accept();//阻塞式监听会一直等待客户端连接
//3.获取输入流
InputStream is = socket.getInputStream();//定义输入流可以接收
//4.文件输出
FileOutputStream fos = new FileOutputStream(new File("receive.png"));
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
OutputStream os = socket.getOutputStream();//定义输出流可以接收
os.write("已接收".getBytes());
os.close();
fos.close();
socket.close();
serverSocket.close();
}
}
客户端
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class TcpClient1 {
public static void main(String[] args) throws IOException {
Socket socket = null;
OutputStream os = null;
FileInputStream fis = null;
try {
//1.要知道服务器的地址,端口号
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9000;
//2.创建一个socket连接
socket = new Socket(serverIP, port);
//3.创建一个输出流
os = socket.getOutputStream();
//4.读取文件
fis = new FileInputStream(new File("111.png"));
//5.写出文件
byte[] bytes = new byte[1024];
int len;
while ((len = fis.read(bytes))!= -1) {
os.write(bytes,0,len);
}
//通知服务器,我已经输出完了
socket.shutdownOutput();//我已经传输完了
//确定服务器接收完毕,才能断开连接
InputStream in = socket.getInputStream();
//string byte[]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bytes1 = new byte[1024];
int len1;
while ((len1 = in.read(bytes1)) != -1) {
baos.write(bytes1,0,len1);
}
System.out.println(baos.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
Tomcat
服务器
- 自定义S
- Tomcat服务器S:Java后天开发
客户端
- 自定义C
- 浏览器B
1.7UDP
发短信:不用连接,但是需要对方的IP地址
UDP聊天实现
发送消息
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);
//准备数据:控制台读取System.in
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
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();
}
}
接收消息
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpSender1 {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(6666);
while (true) {
//准备接收包裹
byte[] container = new byte[100];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
socket.receive(packet);//阻塞式接收
//将字节流装换为String输出
byte[] data = packet.getData();
String receiveData = new String(data, 0, data.length);
System.out.println(receiveData.length());
//断开连接
if (receiveData.equals("bye")) {
break;
}
}
socket.close();
}
}
1.8URL
https://www.baidu.com/
统一资源定位符:定位资源的,定位互联网上述的某一个资源
DNS域名解析 https://www.baidu.com/
协议://IP地址:端口/项目名/资源
URL类的介绍
import java.net.MalformedURLException;
import java.net.URL;
public class Url {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("https://www.bilibili.com/video/BV1LJ411z7vY?p=12");
System.out.println(url.getProtocol());//协议
System.out.println(url.getHost());//主机域名
System.out.println(url.getPort() );//端口
System.out.println(url.getFile());//文件
System.out.println(url.getPath());//全路径
System.out.println(url.getQuery());//参数
}
}
URL文件下载
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class UrlDown {
public static void main(String[] args) throws IOException {
//1.下载地址
URL url = new URL("http://localhost:8080/qinjiang/SecurityFile.txt");
//2.连接到这个这个资源 HTTP
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("111.txt");
byte[] bytes = new byte[1024];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
fos.write(bytes,0,len);
}
fos.close();
inputStream.close();
urlConnection.disconnect();
}
}
本次学习资源来自b站狂神说Java