网络编程
1、网络编程的目的:
无线电台…传播交流信息,数据交换,通信
想要达到这个效果需要什么:
- 如何准确的定位网络上的一台主机 192.168.16.124:端口,定位到这个计算机上的某个资源
- 找到了这个主机,如何传输数据呢?
javaweb:网页编程 B/S
网络编程:TCP/IP C/S
2、网络通信的要素
如何实现网络的通信?
小结:
- 网络编程中有两个主要的问题:
- 如何准确的定位到网络上的一台或者多台主机。
- 找到主机之后如何进行通信
- 网络编程中的要素
- IP和端口号IP
- 网络通信协议udp,tcp
- 万物皆对象
3、IP
ip地址:InetAddress
- 唯一定位一台网络上计算机
- 127.0.0.1 :本机 localhostip
- 地址的分类
- IPv4(四个字节) 127.0.0.1
- ipv6 128位。8个无符号整数
- 示例:2001:0aa2:cccc:0015:0000:0000:1aaa:1312
- 公网(互联网)-私网(局域网)
- 192.168.XX.XX,专门给组织内部使用
- ABCD类地址
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vJIoYaAG-1625377554484)(C:\Users\86152\AppData\Roaming\Typora\typora-user-images\image-20210621104816758.png)]
4、端口
端口表示计算机上的一个程序的进程
-
不同的进程有不同分布!用来区分软件! -
被规定0~65535 -
TCP,UDP: 65535* 2 tcp: 80, udp: 80,单个协议下,端口号不能冲突 -
端口分类
-
公有端口0~1023
- HTTP:80
- HTTPS:443
- FTP∶21
- Telent : 23
-
程序注册端口:1024-49151,分配用户和程序
- Tomcat:8080
- MySQL: 3306
- Oracle:1521
-
动态、私有:49152~65535 -
DOS命令 * - netstat -ano #查看所有的端口
* - netstat -ano|findstr "5900" #查看指定的端口
* - tasklist|findstr "8696" #查看指定端口的进程
* - Ctrl+shift+Esc
## InetAddress
try {
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress);
InetAddress inetAddress2 = InetAddress.getByName("localhost");
System.out.println(inetAddress2);
InetAddress inetAddress3 = InetAddress.getLocalHost();
System.out.println(inetAddress3);
InetAddress inetAddress1 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress1);
} catch (UnknownHostException e) {
e.printStackTrace();
## InetSocketAddress
InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8080);
InetSocketAddress socketAddress1 = new InetSocketAddress("localhost", 8080);
System.out.println(socketAddress);
System.out.println(socketAddress1);
System.out.println(socketAddress.getAddress());
System.out.println(socketAddress.getHostName());
System.out.println(socketAddress.getPort());
1.5、通信协议 -
协议:约定,就好比我们现在说的是普通话。 -
网络通信协议:速率,传输码率,代码结构,传输控制……… -
Tcp/ip协议簇
- 重要:
- TCP:用户传输协议(打电话)
- 连接。稳定。
- 三次握手 四次挥手
- 客户端、服务端
- 传输完成,释放连接,效率低
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-hQm6Frzf-1625377554486)(C:\Users\86152\AppData\Roaming\Typora\typora-user-images\image-20210621152118124.png)]
-
UDP:用户数据报协议(发短信)
- 不连接,不稳定
- 客户端、服务端:没有明确的界限
- 不管有没有准备好,都可以发给你…
- 导弹
-
出名的协议:
5、TCP
5.1、客户端
- 连接服务器 Socket
- 发送消息
try {
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
socket = new Socket(serverIP,port);
os = socket.getOutputStream();
os.write("你好,欢迎学习java".getBytes());
} catch (Exception e) {
e.printStackTrace();
5.2、服务端
- 建立服务的端口 ServerSocket
- 等待用户的连接 accept
- 接受用的消息
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());
5.3、文件上传
服务端
public static void main(String[] args) throws Exception {
ServerSocket serverSocket =new ServerSocket(9000);
Socket socket = serverSocket.accept();
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("receive.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer))!=-1){
fos.write(buffer, 0, len);
System.out.println(fos);
}
OutputStream os = socket.getOutputStream();
os.write("我接收完毕了,你可以断开了".getBytes());
fos.close();
is.close();
socket.close();
serverSocket.close();
客户端
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
OutputStream os = socket.getOutputStream();
FileInputStream fis = new FileInputStream(new File("C:\\Users\\86152\\Desktop\\1.png"));
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer))!=-1){
os.write(buffer, 0, len);
}
socket.shutdownOutput();
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2;
while ((len2 = inputStream.read(buffer2))!= -1){
baos.write(buffer2, 0, len2);
}
System.out.println(baos);
baos.close();
inputStream.close();
fis.close();
os.close();
socket.close();
5.4、 Tomcat
服务端
客户端
6、UDP
发短信:不用连接,需要知道对方的地址!
发送消息
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket();
String msg = "您好啊,服务器!";
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port);
socket.send(packet);
socket.close();
接收消息
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(9090);
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
System.out.println(packet.getAddress().getHostAddress());
System.out.println(new String(packet.getData(), 0, packet.getLength()));
socket.close();
循环发送消息
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(8888);
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();
循环接收消息
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(6666);
while (true){
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
socket.receive(packet);
byte[] data = packet.getData();
String receiveData = new String(data, 0, data.length);
System.out.println(receiveData);
if (receiveData.equals("bye")){
break;
}
}
socket.close();
7、URL
https://www.baidu.com/
统一资源定位符:定位资源的,定位互联网上的某一个资源
DNS域名解析 www.baidu.com —> xxx.xxx.xxx.xxx
1、协议://ip地址:端口/项目号/z
URL url = new URL("https://localhost:8080/helloworld/index.jsp?username=kuangshen&password=123");
System.out.println(url.getProtocol());
System.out.println(url.getHost());
System.out.println(url.getPort());
System.out.println(url.getPath());
System.out.println(url.getFile());
System.out.println(url.getQuery());
网络抓包
URL url = new URL("http://p2.music.126.net/akRVyzotjcEzSO78ywoqGg==/109951165970112797.jpg?param=50y50");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("109951165970112797.jpg");
byte[] buffer = new byte[1024];
int len;
while ((len=inputStream.read(buffer))!=-1){
fos.write(buffer, 0, len);
}
fos.close();
inputStream.close();
urlConnection.disconnect();
|