笔记内容学自链接视频:https://www.bilibili.com/video/BV1LJ411z7vY
计算机网络
ip
java将网络编程所用到的方法都写在了java.net
包中
Inetaddress
类用于处理与ip地址相关的内容
InetAddress
类没有构造方法,所以不能直接new出p一个对象;i
可以通过InetAddress
类的静态方法获得InetAddress
的对象;
如:
InetAddress.getLocalHost();
InetAddress.getByName("");
package 网络编程;
import java.net.*;
public class Inetaddress类 {
public static void main(String[] args) {
try {
InetAddress get = InetAddress.getLocalHost();
System.out.println(get);
} catch (Exception e) {
e.printStackTrace();
}
}
}
端口
InetSocketAddress
类用于对端口进行操作
InetSocketAddress
类可以直接new
package 网络编程;
import java.net.InetSocketAddress;
public class 端口 {
public static void main(String[] args) {
InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1",80);
System.out.println(inetSocketAddress);
}
}
网络编程实战演练
注意:java
中客户端和服务端的访问过程,都是通关==管道流==来实现的
比如发送和接收用IO流(InputStream,OutputStream
),上传文件用文件流(FileInputStream,FileOutputStream
)等等
模拟实现客户端与服务端通
Server:
package 网络编程;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
try {
//需要一个地址
ServerSocket serverSocket = new ServerSocket(9999);
//等待客户连接
Socket accept = serverSocket.accept();
//获取客户端发送的内容
InputStream inputStream = accept.getInputStream();
//字节数组输出流
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//设置缓冲区
byte[] bytes = new byte[1024];
int len;
while ((len = inputStream.read(bytes)) != -1){
byteArrayOutputStream.write(bytes,0,len);
}
//输出
System.out.println(byteArrayOutputStream);
//关闭流
byteArrayOutputStream.close();
inputStream.close();
accept.close();
serverSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Client:
package 网络编程;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class Client {
public static void main(String[] args) {
//连接地址
try {
InetAddress get1 = InetAddress.getByName("127.0.0.1");
int port = 9999;
Socket socket = new Socket(get1,port);
//输出流,获取输出
OutputStream os = socket.getOutputStream();
//写入输出
os.write("我是Atmujie".getBytes(StandardCharsets.UTF_8));
//关闭流
os.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
文件上传
Server:
package 网络编程;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class 文件上传Server {
public static void main(String[] args) {
try {
//创建地址
ServerSocket serverSocket = new ServerSocket(9999);
//等待客户发送
Socket accept = serverSocket.accept();
//接收流
InputStream inputStream = accept.getInputStream();
//读取流
FileOutputStream files = new FileOutputStream(new File("C:\\Users\\MuJie\\Downloads\\111\\222\\1.txt"));
byte[] bytes = new byte[1024];
int len;
while ((len = inputStream.read(bytes)) != -1){
files.write(bytes,0,len);
}
//关闭本次连接
socket.shutdownOutput();
//接收客户端回应
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
InputStream inputStream = socket.getInputStream();
byte[] bytes2 = new byte[1024];
int len2;
while ((len2 = inputStream.read(bytes2)) != -1) {
byteArrayOutputStream.write(bytes2,0,len2);
}
//判断是否成功
System.out.println(byteArrayOutputStream.toString());
//关闭流
byteArrayOutputStream.close();
inputStream.close();
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Client:
package 网络编程;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
public class 文件上传Client {
public static void main(String[] args) {
try {
//连接地址
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9999);
//将文件读取为流
FileInputStream files = new FileInputStream(new File("C:\\Users\\MuJie\\Downloads\\1.txt"));
//IO流
OutputStream os = socket.getOutputStream();
//将文件流写入IO流发送
byte[] bytes = new byte[1024];
int len;
while ((len = files.read(bytes)) != -1) {
os.write(bytes,0,len);
}
//通知客户端接收完毕
OutputStream outputStream = accept.getOutputStream();
outputStream.write("success".getBytes(StandardCharsets.UTF_8));
//关闭流
files.close();
inputStream.close();
accept.close();
serverSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
UDP
发送数据包
Client:
import java.net.*;
import java.nio.charset.StandardCharsets;
public class UdpClient {
public static void main(String[] args) {
try {
///创建包
DatagramSocket socket = new DatagramSocket();
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
String string = "你好,服务器";
DatagramPacket datagramPacket = new DatagramPacket(string.getBytes(StandardCharsets.UTF_8),0,string.getBytes(StandardCharsets.UTF_8).length,localhost,port);
/*DatagramPacket语法:
* new DatagramPacket(数据【byte形式】,开始位置,结束位置,ip地址,端口)
*/
//发送包
socket.send(datagramPacket);
//关闭流
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Server:
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class UdpServer {
public static void main(String[] args) {
try {
//开放端口
DatagramSocket datagramSocket = new DatagramSocket(9090);
//接收数据
byte[] bytes = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(bytes, 0, bytes.length);
//监听
datagramSocket.receive(datagramPacket);
//输出
System.out.println(new String(datagramPacket.getData(),0,datagramPacket.getLength()));
//关闭连接
datagramSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}