1.网络编程概述
package com.yl.pdfdemo.day08.p5;
import org.junit.Test;
import java.net.InetAddress;
import java.net.UnknownHostException;
/**
* @Author wfj
* @Date 2021/7/1
* @Description 网络编程概述
* @Version 1.0
*/
public class InternetTest {
/**
* OSI参考模型 TCP/IP参考模型 TCP/IP参考模型各层对应协议
* 应用层 应用层 HTTP,FTP,Telnet,DNS...
* 表示层 传输层 TCP,UDP...
* 会话层 网络层 IP,ICMP,ARP...
* 传输层 物理+数据链路层 link
* 网络层
* 数据链路层
* 物理层
*
* 网络通信的要素一: IP 和端口号
* 在java中,使用InetAddress类代表IP
* 端口号:计算机上正在运行的进程
* ip地址和端口号组合得出一个网络套接字: Socket
*
* 网络通信的要素二:协议
* TCP协议:
* 1.使用TCP协议之前,需建立TCP连接,形成数据传输通道
* 2.传输数据之前,采用“三次握手”方式,点对点通信,是可靠的
* 3.TCP协议进行通信的两个应用进程:客户端,服务端
* 4.在连接中可进行大数据传输
* 5.传输完毕,需释放已建立的连接,效率低
*
* UDP协议:
* 1.将数据,源,目的封装成数据包,不需要建立连接
* 2.每个数据报的大小限制在64kb内
* 3.发送不管对方是否准备好,接受方收到也不会确认,故是不可靠的
* 4.可以广播发送
* 5.发送数据结束时无需释放资源,开销小,速度快
*/
@Test
public void test1() {
try {
InetAddress inte1 = InetAddress.getByName("127.0.0.1");
//通过域名获取ip
InetAddress inet2 = InetAddress.getByName("www.baidu.com");
//获取本地ip
InetAddress inet3 = InetAddress.getLocalHost();
//获取域名
String hostName = inet2.getHostName();
//获取ip
String hostAddress = inet2.getHostAddress();
System.out.println(inte1);// /127.0.0.1
System.out.println(inet2);// www.baidu.com/14.215.177.38
System.out.println(inet3);// LAPTOP-IJGVLVU7/192.168.80.28
System.out.println(hostName); // www.baidu.com
System.out.println(hostAddress); // 14.215.177.38
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
2.TCP三次握手
3.TCP网络编程
1)
package com.yl.pdfdemo.day08.p5;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @Author wfj
* @Date 2021/7/1
* @Description TCP例子1:客户端发送信息给服务端,服务端将数据显示在控制台上
* @Version 1.0
*/
public class TcpTest1 {
//客户端
@Test
public void test1() {
Socket socket = null;
OutputStream os = null;
try {
InetAddress inet = InetAddress.getByName("127.0.0.1");
//1.创建socket对象,指明服务端的ip和端口号
socket = new Socket(inet,8899);
//2.获取输出流
os = socket.getOutputStream();
//3.写出数据
os.write("客户端:hello java vue".getBytes());
} catch (IOException 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();
}
}
}
}
//服务端
@Test
public void test2() {
Socket socket = null;
InputStream is = null;
ServerSocket ss = null;
ByteArrayOutputStream baos = null;
try {
//1.创建ServerSocket
ss = new ServerSocket(8899);
//2.调用accept()方法,获取客户端的socket
socket = ss.accept();
//3.获取输入流
is = socket.getInputStream();
//为了防止乱码,可以用ByteArrayOutputStream,相当于将所有读到得数据都存到一个数组里
baos = new ByteArrayOutputStream();
byte[] bytes = new byte[5];
int len;
//4.读取数据
while((len = is.read(bytes)) != -1) {
baos.write(bytes,0,len);
}
System.out.println(baos.toString());
System.out.println("接收到来自客户端:"+socket.getInetAddress().getHostAddress()+"的消息");
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2)
package com.yl.pdfdemo.day08.p5;
import org.junit.Test;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @Author wfj
* @Date 2021/7/1
* @Description Tcp练习2:客户端发送文件给服务端,服务端将文件保存到本地
* @Version 1.0
*/
public class TcpTest2 {
//客户端
@Test
public void test1() {
Socket socket = null;
OutputStream os = null;
BufferedInputStream bis = null;
try {
//创建客户端socket
socket = new Socket("127.0.0.1",9977);
//获取输出流
os = socket.getOutputStream();
//创建图片的输入流
bis = new BufferedInputStream(new FileInputStream(new File("D:\\test\\test1\\22.jpg")));
//读取数据
byte[] bytes = new byte[1024];
int len;
while((len = bis.read(bytes)) != -1) {
//写出数据
os.write(bytes,0,len);
}
} catch (IOException 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();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//服务端
@Test
public void test2() {
ServerSocket ss = null;
Socket socket = null;
InputStream is = null;
BufferedOutputStream bos = null;
try {
//创建服务端Socket
ss = new ServerSocket(9977);
//获取客户端Socket
socket = ss.accept();
//获取输入流
is = socket.getInputStream();
//创建输出流
bos = new BufferedOutputStream(new FileOutputStream(new File("D:\\test\\test1\\qq22.jpg")));
//读取数据
byte[] bytes = new byte[1024];
int len;
while ((len = is.read(bytes)) != -1) {
//写出数据
bos.write(bytes,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
3)
package com.yl.pdfdemo.day08.p5;
import org.junit.Test;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @Author wfj
* @Date 2021/7/1
* @Description Tcp练习3 从客户端发送文件给服务端,服务端保存到本地,并返回“发送成功”给客户端,并且关闭相关连接
* @Version 1.0
*/
public class TcpTest3 {
//客户端
@Test
public void test1() {
Socket socket = null;
OutputStream os = null;
BufferedInputStream bis = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//创建客户端socket
socket = new Socket("127.0.0.1",9977);
//获取输出流
os = socket.getOutputStream();
//创建图片的输入流
bis = new BufferedInputStream(new FileInputStream(new File("D:\\test\\test1\\22.jpg")));
//读取数据
byte[] bytes = new byte[1024];
int len;
while((len = bis.read(bytes)) != -1) {
//写出数据
os.write(bytes,0,len);
}
//注意,写出数据之后,服务端一直循环读数据,这里没有明确标识数据已写出完毕,故那边会一直在读,程序不会继续往下走
//关闭数据写出
socket.shutdownOutput();
//接收来自服务端的消息,并在控制台显示
is = socket.getInputStream();
//为了防止出现乱码,使用ByteArrayOuputStream
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len1;
while ((len1 = is.read(buffer)) != -1) {
baos.write(buffer,0,len1);
}
System.out.println(baos.toString());
} catch (IOException 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();
}
}
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (baos != null) {
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//服务端
@Test
public void test2() {
ServerSocket ss = null;
Socket socket = null;
InputStream is = null;
BufferedOutputStream bos = null;
OutputStream os = null;
try {
//创建服务端Socket
ss = new ServerSocket(9977);
//获取客户端Socket
socket = ss.accept();
//获取输入流
is = socket.getInputStream();
//创建输出流
bos = new BufferedOutputStream(new FileOutputStream(new File("D:\\test\\test1\\qqq22.jpg")));
//读取数据
byte[] bytes = new byte[1024];
int len;
while ((len = is.read(bytes)) != -1) {
//写出数据
bos.write(bytes,0,len);
}
System.out.println("文件发送成功");
//服务给客户端反馈信息
os = socket.getOutputStream();
os.write("发送文件成功-来自于服务端的消息".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
4.UDP网络编程
package com.yl.pdfdemo.day08.p5;
import org.junit.Test;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* @Author wfj
* @Date 2021/7/2
* @Description UDP协议网络编程例子
* @Version 1.0
*/
public class UdpTest {
//发送端
@Test
public void test1() {
DatagramSocket datagramSocket = null;
try {
//创建Socket
datagramSocket = new DatagramSocket();
String str = "hello vue";
byte[] data = str.getBytes();
InetAddress inet = InetAddress.getLocalHost();
DatagramPacket datagramPacket = new DatagramPacket(data,0,data.length,inet,9097);
//发送数据包
datagramSocket.send(datagramPacket);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (datagramSocket != null) {
try {
datagramSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
//接收端
@Test
public void test2() {
DatagramSocket datagramSocket = null;
try {
datagramSocket = new DatagramSocket(9097);
byte [] bufer = new byte[1024];
DatagramPacket datagramPacket = new DatagramPacket(bufer,0,bufer.length);
datagramSocket.receive(datagramPacket);
System.out.println(new String(datagramPacket.getData(),0,datagramPacket.getLength()));
} catch (IOException e) {
e.printStackTrace();
} finally {
if (datagramSocket != null) {
try {
datagramSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
5.URL网络编程
package com.yl.pdfdemo.day08.p5;
import org.junit.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @Author wfj
* @Date 2021/7/2
* @Description URL网络编程
* @Version 1.0
*/
public class UrlTest {
/**
* 1.URL:统一资源定位符,对应着互联网的某一资源地址
* 2.格式:http://localhost:8080/user/selectById?id = 1
* 协议 主机名 端口号 资源地址 参数列表
*/
//URL一些常用方法
@Test
public void test1() throws Exception{
URL url = new URL("http://localhost:8080/user/selectById?id = 1");
//获取协议名
System.out.println(url.getProtocol());// http
//获取该url的主机名
System.out.println(url.getHost());// localhost
//获取该url的端口号
System.out.println(url.getPort());// 8080
//获取该url的文件路径
System.out.println(url.getPath());// /user/selectById
//获取该url的文件名
System.out.println(url.getFile());// /user/selectById?id = 1
//获取该url的查询名
System.out.println(url.getQuery());// id = 1
}
//把一张图片放到tomcat目录下,启动tomcat,下载那张图片
@Test
public void test2() {
HttpURLConnection urlConnection = null;
InputStream is = null;
FileOutputStream fos = null;
try {
URL url = new URL("http://localhost:8080/img/11.jpg");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
is = urlConnection.getInputStream();
fos = new FileOutputStream(new File("D:\\test\\test1\\pp.jpg"));
byte [] bytes = new byte[1024];
int len;
while ((len = is.read(bytes)) != -1) {
fos.write(bytes,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (urlConnection != null) {
try {
urlConnection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}