什么是计算机网络
1.1概述
地球村:也译为世界村(global village),对地球的一种比喻说法。现代科技的迅速发展,缩小了地球上的时空距离,国际交往日益频繁便利,因而整个地球就如同是茫茫宇宙中的一个小村落。
计算机网络:
计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统
网络编程的目的:
无线电台……..传播交流信息,数据交换。通信
想要达到这个效果需要什么:
1、如何准确的定位网络上的一台主机 : 端口,定位到这个计算机上的某个资源
2、找到了这个主机,如何传输数据呢?
javaweb:网页编程 B/S
网络编程:TPC/IP C/S
1.2网络通信的要素
人工智能:智能汽车:用在工厂等人少的地方
如何实现网络的通信?
通信双方地址:
ip
端口号
就能定位到一台具体电脑上的具体的应用
规则:网络通信的协议
http,fip,smtp等
TCP/IP参考模型

小结:
1、网络编程中有两个主要的问题
如何准确的定位到网络上的一台或者多台主机
39.156.66.14就是百度的ip
www.baidu.com是百度的域名
找到主机之后如何进行通信
2、网络编程中的要素
IP和端口号 IP
网络通信协议 udp , tcp
3、Java是万物皆对象
1.3 IP
ip地址:InetAddress
唯一定位一台网络上的计算机
127.0.0.1:本机 localhost
自己给自己呼叫
ip地址的分类
ipv4/ipv6
ipv4就是如 127.0.0.1 这样,4个字节组成 每个字节的长度为 0-255,总共有42亿个 , ;30亿在北美,亚洲四亿个,2011年就用尽了
ipv6:128位。 8个无符号整数组成!
如
2001:0bb2:aaaa:0015:0000:0000:1aaa:1312!
公网(互联网)-私网(局域网)
ABCD类地址
192.168.xx.xx,专门给组织内部使用的
域名:记忆IP问题
*IP:www.vip.com
package com.li.lesson01;import java.net.InetAddress;import java.net.UnknownHostException;//测试IPpublic class TestInetAddress {public static void main(String[] args) {//查询本机地址try {InetAddress inetAddress1=InetAddress.getByName("127.0.0.1");System.out.println(inetAddress1);InetAddress inetAddress3=InetAddress.getByName("localhost");System.out.println(inetAddress3);InetAddress inetAddress4=InetAddress.getLocalHost();System.out.println(inetAddress4);//查询网站IP地址InetAddress inetAddress2=InetAddress.getByName("www.baidu.com");System.out.println(inetAddress2);//常用方法//System.out.println(inetAddress2.getAddress());//可以不记住System.out.println(inetAddress2.getCanonicalHostName());//规范的名字System.out.println(inetAddress2.getHostAddress());//ipSystem.out.println(inetAddress2.getHostName());//域名,或者自己电脑的名字} catch (UnknownHostException e) {e.printStackTrace();}}}
1.4端口Port
端口表示计算机上一个程序的进程:
- 不同的进程有不同的端口号!用来区分软件!
- 端口被规定0-65535
- TCP,UDP:每一个都有65535个,单个协议下,端口号不能冲突
- 端口分类:
- 公有端口:0~1023
- HTTP:80
- HTTPS:443
- FTP:21
- Telent:23
- 程序注册端口:1024-49151,拿来分配给用户或者程序
- Tomcat:8030
- MySQL:3306
- Oracle:1521
- 动态、私有端口:49152~65535尽量不要用这里面的端口
netstat -ano//查看所有的端口netstat -ano|findstr "5200" //查看指定的端口tasklist|findstr "14692" //查看指定端口的进程ctrl+shift+ESC//打开任务管理器


```java
package com.li.lesson01;
- 公有端口:0~1023
import java.net.InetSocketAddress;
public class TestInetSockAddress { public static void main(String[] args) { InetSocketAddress socketAddress=new InetSocketAddress(“127.0.0.1”,8080); System.out.println(socketAddress); InetSocketAddress socketAddress2=new InetSocketAddress(“localhost”,8080); System.out.println(socketAddress2);
System.out.println(socketAddress.getAddress());System.out.println(socketAddress.getHostName());//地址System.out.println(socketAddress.getPort());//端口}
}
<br /><a name="JeIAd"></a>## 1.5通信协议协议:相当于约定,就好比我们现在说的是普通话<br />**网络通信协议: **速率,传输代码,代码结构,传输控制........<br />问题:非常的复杂<br />大事化小:分层!<br />**TCP/IP协议簇(实际上是一组协议):**- 重要:- TCP:用户传输协议- UDP:用户数据报协议- 出名的协议:- TCP:用户传输协议- IP:网络互联协议- - **TCP UDP对比:**- TCP想成打电话- 好处:连接,稳定- “三次握手”、“四次挥手”```java最少需要三次,保证稳定连接A:你瞅啥?B:瞅你咋地?A:干一场!A:我要走了B:你真的要走了吗?B:你真的真的要走了吗?A:我真的要走了!
所谓的三次握手即TCP连接的建立。这个连接必须是一方主动打开,另一方被动打开的。

三次握手通俗解释:
所谓的四次挥手即TCP连接的释放(解除)。连接的释放必须是一方主动释放,另一方被动释放。
四次挥手通俗理解
举个栗子:把客户端比作男孩,服务器比作女孩。通过他们的分手来说明“四次挥手”过程。
“第一次挥手”:日久见人心,男孩发现女孩变成了自己讨厌的样子,忍无可忍,于是决定分手,随即写了一封信告诉女孩。
“第二次挥手”:女孩收到信之后,知道了男孩要和自己分手,怒火中烧,心中暗骂:你算什么东西,当初你可不是这个样子的!于是立马给男孩写了一封回信:分手就分手,给我点时间,我要把你的东西整理好,全部还给你!男孩收到女孩的第一封信之后,明白了女孩知道自己要和她分手。随后等待女孩把自己的东西收拾好。
“第三次挥手”:过了几天,女孩把男孩送的东西都整理好了,于是再次写信给男孩:你的东西我整理好了,快把它们拿走,从此你我恩断义绝!
“第四次挥手”:男孩收到女孩第二封信之后,知道了女孩收拾好东西了,可以正式分手了,于是再次写信告诉女孩:我知道了,这就去拿回来!
这里双方都有各自的坚持。女孩自发出第二封信开始,限定一天内收不到男孩回信,就会再发一封信催促男孩来取东西!
男孩自发出第二封信开始,限定两天内没有再次收到女孩的信就认为,女孩收到了自己的第二封信;若两天内再次收到女孩的来信,就认为自己的第二封信女孩没收到,需要再写一封信,再等两天…..
倘若双方信都能正常收到,最少只用四封信就能彻底分手!这就是“四次挥手”。
- 分为客户端、服务端- 传输完成,释放连接,效率低
- UDP:发短信
import java.io.IOException; import java.io.OutputStream; import java.io.*; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import java.nio.charset.StandardCharsets;
//客户端 public class TcpClientDemo01 { public static void main(String[] args) {
Socket socket=null;OutputStream os=null;try {//1、要知道服务器的地址、端号InetAddress serverIP=InetAddress.getByName("127.0.0.1");int port=6666;//2、创建一个socket连接socket=new Socket(serverIP,port);//3、发送消息IO流os=socket.getOutputStream();os.write("欢迎学习java".getBytes());} catch (UnknownHostException e) {e.printStackTrace();} 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();}}}}
}
服务器:<br />1、建立服务的端口 ServerSocket<br />2、等待用户的连接 accept<br />3、接收用的消息```javapackage com.li.lesson02;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.ServerSocket;import java.net.Socket;//服务器public class TcpServerDemo02 {public static void main(String[] args) {ServerSocket serverSocket=null;Socket socket=null;InputStream is=null;ByteArrayOutputStream baos=null;try {//1、我得有一个地址serverSocket = new ServerSocket(6666);while (true) {//可以循环的去接收//2、等待客户端连接过来socket = serverSocket.accept();//3、读取客户端的消息is = socket.getInputStream();/*** 不建议使用,可能会出现乱码现象* byte[] buffer =new byte[1024];int len;while ((len=is.read(buffer))!=-1){String msg=new String(buffer,0,len);System.out.println(msg);}*///管道流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();}}}}}}}
1.7文件上传
服务端
package com.li.lesson02;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.ServerSocket;import java.net.Socket;import java.io.*;import java.nio.charset.StandardCharsets;public class TcpServerDemo02 {public static void main(String[] args) throws IOException {//1、创建服务ServerSocket serverSocket=new ServerSocket(8888);//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());//关闭资源fos.close();is.close();socket.close();serverSocket.close();}}
客户端
package com.li.lesson02;import java.io.IOException;import java.io.OutputStream;import java.net.InetAddress;import java.net.Socket;import java.io.*;import java.net.UnknownHostException;public class TcpClientDemo02 {public static void main(String[] args) throws IOException {//1、创建一个Socket连接Socket socket=new Socket(InetAddress.getByName("127.0.0.1"),8888);//2、创建一个输出流OutputStream os=socket.getOutputStream();//3、读取文件FileInputStream fis=new FileInputStream(new File("bingdundun.png"));//4、写出文件byte[] buffer =new byte[1024];int len;while((len=fis.read(buffer))!=-1){os.write(buffer,0,len);}//通知服务器,我已经结束了socket.shutdownOutput();//我已经传输完了//确定服务器接收完毕才能断开连接InputStream is=socket.getInputStream();//String byte[]ByteArrayOutputStream baos=new ByteArrayOutputStream();byte[] buffer2=new byte[1024];int len2;while((len2=is.read(buffer2))!=-1){baos.write(buffer2,0,len2);}System.out.println(baos.toString());//5、关闭资源baos.close();is.close();fis.close();os.close();socket.close();}}
1.8初识Tomcat
- 服务端
- 自定义S
- Tomcat服务器S:java后台开发
- 客户端
import java.io.IOException; import java.net.*; import java.nio.charset.StandardCharsets;
//不需要连接服务器 public class UdpClientDemo01 { public static void main(String[] args) throws IOException { //1、建立一个Socket DatagramSocket socket=new DatagramSocket();
//2、建个包String msg="你好啊,服务器!";InetAddress localhost=InetAddress.getByName("localhost");int port=9090;//数据:数据的长度起始,要发送给谁DatagramPacket packet=new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);//3、发送包socket.send(packet);//4、关闭流socket.close();}
}
接收端```javapackage com.li.lesson03;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.SocketException;//还是要等待客户端的连接public class UdpServerDemo01 {public static void main(String[] args) throws IOException {//开放端口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();}}
UDP聊天实现
package com.li.chat;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetSocketAddress;import java.net.SocketException;public class UdpSenderDemo01 {public static void main(String[] args) throws IOException {DatagramSocket socket=new DatagramSocket(8888);System.out.println(socket);//准备数据:控制台读取 System.inBufferedReader reader=new BufferedReader(new InputStreamReader(System.in));while (true) {String data = reader.readLine();System.out.println(data);byte[] datas = data.getBytes();DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 6666));System.out.println(packet);socket.send(packet);if(data.trim().equals("bye")){break;}}socket.close();}}
package com.li.chat;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.SocketException;public class UdpReceiveDemo01 {public static void main(String[] args) throws IOException {DatagramSocket socket=new DatagramSocket(6666);System.out.println(socket);while (true){//准备接收包裹byte[] container= new byte[1024];DatagramPacket packet=new DatagramPacket(container,0,container.length);System.out.println(packet);//阻塞式接收包裹socket.receive(packet);byte[] data=packet.getData();String receiveDate=(new String(data,0,packet.getLength()));System.out.println(receiveDate.trim());//断开连接 byeif(receiveDate.trim().equals("bye")){break;}}socket.close();}}
