计算机网络:
把分布在不同地理区域的计算机与专门的外部设备用通信线路互连成-一个规
模大、功能强的网络系统,从而使众多的计算机可以方便地互相传递信息、
共享硬件、软件、数据信息等资源。
网络编程的目的:
直接或间接地通过网络协议与其它计算机实现数据交换,进行通讯。
网络编程中有两个主要的问题:
➢如何准确地定位网络上一台或多台主机;定位主机上的特定的应用
➢找到主机后如何可靠高效地进行数据传输
网络通信要素 概述:
ip地址和端口号
网络通信协议
IP地址: InetAddress类的对象
➢唯一标识Internet.的计算机(通信实体)
➢本地回环地址(hostAddress): 127.0.0.1 主机名(hostName): localhost
➢IP地址分类方式: IPV4和IPV6
(IPV4: 4个字节组成,4个0-255。 大概42亿,30亿都在北美,亚洲4亿。2011年初已
经用尽。以点分十进制表示,如192.168.0.1
(IPV6: 128位 (16个字节),写成8个无符号整数,每个整数用四个十六进制位表示,
数之间用冒号(: )分开,如: 3ffe:3201:1401:1280:c8ff:fe4d:db39:1984
➢IP地址分类方式2:公网地址(万维网使用)和私有地址(局域网使用)。192.168.
开头的就是私有址址,范围即为192.168.0.0–192.168.255.255,专门为组织机构内部使用
➢特点:不易记忆
本机回路地址:127.0.0.1 对应着: localhost
实例化ip地址
InetAddress inet1 = InetAddress . getByName("192.168.10.14");
InetAddress inet1 = InetAddress . getByName("www.atguigu.com");//域名
实例化本机ip地址还可以:
InetAddress inet4
InetAddress .getLocalHost( );//获取本机ip地址
用户也可以通过域名的方式来实例化访问具体的ip地址
域名解析服务器DNS会帮我们解析出对应的ip地址。在通过该ip地址访问对应的服务器。
//getHostName()
System. out . println( inet2. getHostName());//获取域名
//getHostAddress()
System. out . println(inet2. getHostAddress());//获取ip地址
端口号:
标识正在计算机上运行的进程。
不同的进程有不同的端口号
端口号标识正在计算机上运行的进程(程序)
➢不同的进程有不同的端口号
➢被规定为一个16位的整数0~65535。
➢端口分类:
公认端口: 0~1023。被预先定义的服务通信占用(如: HTTP占用端口80,FTP 占用端口21,Telnet占用端 口23)
注册端口: 1024~49151。分配给用户进程或应用程序。( 如: Tomcat占用端口8080, MySQL占用端口3306, Oracle 占用端口1521等)
➢动态/私有端口: 49152~65535
端口号与IP地址的组合得出一个网络套接字: Socket。
网络通信协议
TCP协议:
- 使用TCP协议前,须先建立TCP连接,形成传输数据通道
- 传输前,采用“三次握手”方式,点对点通信,是可靠的
- TCP协议进行通信的两个应用进程:客户端、服务端。 在连接中可进行大数据量的传输
- 传输完毕,需释放已建立的连接,效率低
UDP协议
- 将数据源封装成数据包,不需要建立连接
- 每个数据报的大小限制在64K内 发送不管对方是否准备好,接收方收到也不确认,故是不可靠的
- 可以广播发送
- 发送数据结束时无需释放资源,开销小,速度快
TCP协议类似于生活中的打电话
UDP协议类似于生活中的发送短信、发电报
网络编程小例子:
客户端::
InetAddress inet = InetAddress . getByName("127.0.0.1");
Socket socket = new Socket(inet, port: 8899); .
OutputStream os = socket . getOutputStream( );
os .write("你好,我是客户端mm" . getBytes());
os . close();|
socket. close() ;
服务器端:
ServerSocket ss = new ServerSocket( port: 8899);
Socket socket = ss.accept( );
InputStream is = socket . getInputStream( );
//解决中文乱码的问题
ByteArray0utputStream baos = new ByteArray0utputStream();
byte[] buffer = new byte[5];
int 1en;
while((1en = is.read(buffer)) != -1){
baos . write(buffer, off: 0,1en);
}
System. out . println(baos . toString());
//关闭资源
baos. close();
is.close();
socket.close();
ss. close();
先启动服务器端,后启动客户端
例题二:
客户端:
//1.
Socket socket = new Socket( InetAddress . getByName("127.0.0.1"). port: 9090)
//2.
OutputStream oS = socket . get0utputStream( );
//3.
FileInputStream fis = new FileInputStream(new File( pathname: "beauty. jpg
//4.
byte[] buffer = new byte[1024];
int len;
while((len = fis. read(buffer)) != -1){
os .write(buffer, off: 0,1en);
}
//5.
fis.close();
os. close( );
socket.close();
服务器端
ServerSocket ss = new ServerSocket( port: 9090);
Socket socket = ss.accept();
InputStream is = socket . getInputStream();
FileOutputStream fos = new F ileOutputStream(new File( pathname: "beauty1.jI
byte[] buffer = new byte[1024];
int len;
while((1en = is.read(buffer)) != -1){
fos . write(buffer, off: 0,1en);
}
fos.close();
is. close();
socket.close( );
ss.close();
URL: 同一资源定位符
URL类:
●URL(Uniform Resource Locator):统一资 源定位符,它表示Internet.上某一资源的地址。
●它是一种具体的URI,即URL可以用来标识-一个资源,而且还指明了如何locate这个资源。
●通过URL我们可以访问Internet 上的各种网络资源,比如最常见的www, ftp .站点。浏览器通过解析给定的URL可以在网络.上查找相应的文件或其他资源。
●URL的基本结构由5部分组成:
<传输协议>://<主机名>:<端口号>/<文件名>#片段名?参数列表
➢例如:
http://192. 168. 1.100:8080/helloworld/index.jsp#a?username=shkstart&password=123
➢#片段名:即锚点,例如看小说,直接定位到章节
➢参数列表格式:参数名=参数值&参数名=参数值…
2.格式:
http://localhost: 8080/exampLes/beauty. jpg ?username=Tom
协议 主机名 端口号 资源地址 参数列表
方法:
URL url = new URL("http://localhost : 8080/examples/beauty. jpg?username=Tom")
System. out . print1n(ur1. getProtocol());// 获取该URL的协议名
// 获取该URL的主机名
System. out . println(ur1. getHost());
// 获取该URL的端口号
System. out . println(url. getPort());
// 获取该URL的文件路径
System. out . println(url. getPath());
// 获取该URL的文件名
System. out . print1n(url.getFile());
// 获取该URL的查询名
System. out .print1n(ur1. getQuery() ;
HttpURLConnection urlConnection = null;
InputStream is = nu1l;
File0utputStream fos = nu1l;
try {
URL ur1 = new URL( "http://localhost :8080/ examples/beauty.jpg" );
urlConnection = (HttpURLConnection) url . openConnection();
urlConnection. connect();//进行服务器连接
is = urlConnection. getInputStream();//获取输入流
fos = new FileOutputStream( name: "day10\ \beauty3.jpg");//输出流,将下载的资源保存到本机电脑上
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1){
fos . write(buffer, off: 0,1en);
}