提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
前言
实现网络通信要解决两个问题:
1、如何准确的定位网络上的一台或者多台主机;
2、定位主机上的特定应用
一、网络通信的两个要素
网络通信的两个要素分别对应上述两个问题。
1、 IP和端口号
IP:是唯一标识Internet上的计算机(实体);
在Java中使用 InterAddress 类代表IP
IP分类:IPV4、 IPv6;万维网、局域网。
域名:类似一种让人容易记住的网址,通过域名解析(DNS)转化对应的IP。
端口号与IP地址的组合得出一个网络套接字:Socket
2、TCP/IP网络通信协议
指能够在多个不同网络间实现信息传输的协议簇。TCP/IP协议不仅仅指的是TCP 和IP两个协议,而是指一个由FTP、SMTP、TCP、UDP、IP等协议构成的协议簇, 只是因为在TCP/IP协议中TCP协议和IP协议最具代表性,所以被称为TCP/IP协议。
应用层:是协议的第一层,直接为应用进程提供服务。
(1)对不同种类的应用程序它们会根据自己的需要来使用应用层的不同协议,邮件传输应用使用了SMTP协议、万维网应用使用了HTTP协议、远程登录服务应用使用了有TELNET协议。
(2)应用层还能加密、解密、格式化数据。
(3)应用层可以建立或解除与其他节点的联系,这样可以充分节省网络资源
传输层:使用者使用平台和计算机信息网内部数据结合的通道,可以实现数据传输与数据共享
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
网络层:主要协议有ICMP、IP、IGMP,主要负责网络中数据包的传送等;而网络访问层,也叫网路接口层或数据链路层,主要协议有ARP、RARP,主要功能是提供链路管理错误检测、对不同通信媒介有关信息细节问题进行有效处理等
网络接口层:在TCP/IP协议中,网络接口层位于第四层。由于网络接口层兼并了物理层和数据链路层所以,网络接口层既是传输数据的物理媒介,也可以为网络层提供一条准确无误的线路
TCP与UDP的区别
TCP协议
- 使用TCP协议前,先建立TCP连接,形成传输数据通道。
- 传输前,采用三次握手的方式,建立连接,点对点通信,是可靠的。
- TCP进行通信的两个应用进程:客户端、服务端。
- 传输完毕,需要释放已建立连接,效率低。(四次挥手)
UDP协议
5. 将数据、源封装成数据包,不需要建立连接。
6. . 每个数据报的大小限制在64k
7. 发送不管对方受否准备好,接收方收到也不确认,故不可靠。
8. 可以广播发送。
9. 发送数据时,无需释放资源,开销小,速度快。
TCP三次握手与四次挥手
二、TCP网络编程
1 客户端-服务端传输
客户端发送信息给服务端,服务端将数据显示在控制台上
客户建立套接字—通过输出流写入信息–发送
服务器建立服务器套接字(指定端口)—接受信息–通过输入流读入–通过输出流输出到终端
@Test
public void client(){
Socket socket = null;
OutputStream os = null;
try{
// 1 建立套接字 Socket对象
InetAddress inet = InetAddress.getByName("127.0.0.1");
socket = new Socket(inet, 8899);
// 获取输出流
os = socket.getOutputStream();
// 输出流发送的数据
os.write("我是客户端mm".getBytes(StandardCharsets.UTF_8));
}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();
}
}
}
}
@Test
public void server(){
ServerSocket ss = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try{
// 建立服务器套接字,指定端口
ss = new ServerSocket(8899);
// 接受套接字数据
socket = ss.accept();
// 获取输入流
is =socket.getInputStream();
// 读取输入流的数据
baos = new ByteArrayOutputStream();
byte[] buffer = new byte[5];
int len;
while ((len =is.read(buffer)) != -1){
baos.write(buffer, 0, len);
}
System.out.println(baos.toString());
System.out.println("收到了来自于:" + socket.getInetAddress().getHostAddress() + "的数据");
}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();
}
}
}
}
2 写入文件
@Test
public void client(){
FileInputStream fis =null;
OutputStream os = null;
Socket socket = null;
try{
socket = new Socket(InetAddress.getByName("127.0.0.1"),9000);
os = socket.getOutputStream();
fis = new FileInputStream(new File("beauty.jpg"));
byte[] buffer = new byte[1024];
int len;
while((len = fis.read(buffer)) != -1 ){
os.write(buffer, 0, len);
}
}catch (IOException e){
e.printStackTrace();
}finally {
if(fis!=null){
try {
fis.close();
}catch(IOException e){
e.printStackTrace();
}
}
if(os !=null){
try {
os.close();
}catch(IOException e){
e.printStackTrace();
}
}
if(fis !=null){
try {
fis.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
@Test
public void server() throws IOException{
// 这里的异常需要上述try-catch-finally
ServerSocket ss = new ServerSocket(9000);
Socket socket = ss.accept();
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(new File("b1.jpg"));
byte[] buffer = new byte[1024];
int len;
while((len= is.read(buffer))!=-1){
fos.write(buffer, 0, len);
}
fos.close();
is.close();
socket.close();
}
发送成功客户端
- 例题3:从客户端发送文件给服务端,服务端保存到本地。并返回“发送成功”给客户端。
@Test
public void client() throws IOException {
//1.
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
//2.
OutputStream os = socket.getOutputStream();
//3.
FileInputStream fis = new FileInputStream(new File("beauty.jpg"));
//4.
byte[] buffer = new byte[1024];
int len;
while((len = fis.read(buffer)) != -1){
os.write(buffer,0,len);
}
//关闭数据的输出
socket.shutdownOutput();
//5.接收来自于服务器端的数据,并显示到控制台上
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bufferr = new byte[20];
int len1;
while((len1 = is.read(buffer)) != -1){
baos.write(buffer,0,len1);
}
System.out.println(baos.toString());
//6.
fis.close();
os.close();
socket.close();
baos.close();
}
/*
这里涉及到的异常,应该使用try-catch-finally处理
*/
@Test
public void server() throws IOException {
//1.
ServerSocket ss = new ServerSocket(9090);
//2.
Socket socket = ss.accept();
//3.
InputStream is = socket.getInputStream();
//4.
FileOutputStream fos = new FileOutputStream(new File("beauty2.jpg"));
//5.
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
System.out.println("图片传输完成");
//6.服务器端给予客户端反馈
OutputStream os = socket.getOutputStream();
os.write("你好,美女,照片我已收到,非常漂亮!".getBytes());
//7.
fos.close();
is.close();
socket.close();
ss.close();
os.close();
}
三、UDP网络通信
//发送端
@Test
public void sender() throws IOException {
DatagramSocket socket = new DatagramSocket();
String str = "我是UDP方式发送的导弹";
byte[] data = str.getBytes();
InetAddress inet = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9090);
socket.send(packet);
socket.close();
}
//接收端
@Test
public void receiver() throws IOException {
DatagramSocket socket = new DatagramSocket(9090);
byte[] buffer = new byte[100];
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
socket.receive(packet);
System.out.println(new String(packet.getData(),0,packet.getLength()));
socket.close();
}
四、 URL
- URL(Uniform Resource Locator)的理解:
统一资源定位符,对应着互联网的某一资源地址 - URL的5个基本结构:
http://localhost:8080/examples/beauty.jpg?username=Tom
协议 主机名 端口号 资源地址 参数列表 - 如何实例化
URL url = new URL(“http://localhost:8080/examples/beauty.jpg?username=Tom”);
public static void main(String[] args) {
HttpURLConnection urlConnection = null;
InputStream is = null;
FileOutputStream fos = null;
try {
URL url = new URL("http://localhost:8080/examples/beauty.jpg");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
is = urlConnection.getInputStream();
fos = new FileOutputStream("day10\\beauty3.jpg");
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
System.out.println("下载完成");
} 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){
urlConnection.disconnect();
}
}
}