socket编程
1.网络基础知识
TCP/IP是目前世界上应用最为广泛的协议是以TCP和IP为基础的不同层次上多个协议的集合也称:TCP/IP协议族 或TCP/IP协议栈
TCP:Transmission Control Protocol 传输控制协议
IP:Internet Protocol 互联网协议
HTTP:超文件传输协议
FTP:文件传输协议
SMTP:简单邮件传送协议
IP地址:
为实现网络中不同计算机之间的通信,每台机器都必须有一个唯一的标识—IP地址
IP地址格式:数字型,如:192.164.0.2
端口:
1.用于区分不同应用程序
比如:两台电脑之间使用聊天,聊天工具有很多,如:QQ、米聊、飞信、微信,在t1上使用微信访问t2的主机上的应用,就是端口号来区分不同应用程序只能是同一个端口号的应用可以互相通信,这样微信发送过来的消息就不可能到QQ上
2.端口号范围为:065535,其中01023为系统所保留(这样在我们自定义端口号的时候建议是使用1023以后的端口)
3.IP地址和端口号组成了所谓的Socket,Socket是网络上运行的程序之间双向通信链路的终结点,是TCP和UDP的基础
比如:两个人聊天,A向B拨打电话,首先发出去的电话是会被总机所拦截的,提示输入B的分机号码,这样如果A向拨通B的电话不仅要知道总机号码,还需要知道B的分机号码,这样才算是完成了一条通信通道,那么两台电脑之间的通信也是如此,你不仅需要知道对方的IP地址,还需要知道对方的应用程序的端口号,这样才能建立起通信连接。
4.常用协议的端口号:HTTP:80 ftp:21 telnet:23
java中的网络支持
针对网络通信的不同层次,JAVA提供的网络功能有四大类
1.InetAddress:用于标识网络上的硬件资源
2.URL:统一资源定位符 通过URL可以直接读取或写入网络上的数据
3.Sockets:使用TCP协议实现网络通信的Socket相关的类
4.Datagram:使用UDP协议,将数据保存在数据报中,通过网络进行通信
2.InetAddress类
1.InetAddress类用于标识网络上的硬件资源,表示互联网协议(IP)地址。
测试类
package com.example.demo.test;
import javax.xml.ws.soap.Addressing;
import java.net.InetAddress;
import java.util.Arrays;
/**
* 用于测试 InetAddress类
*/
public class TestInetAddress {
public static void main(String[] args) {
try {
//获取本机的InetAddress实列
InetAddress inetAddress=InetAddress.getLocalHost();
//获取计算机名称
System.out.println("计算机名称:"+inetAddress.getHostName());
System.out.println("计算机IP:"+inetAddress.getHostAddress());
System.out.println(inetAddress);
byte[] bytes=inetAddress.getAddress();//获取字节数组形式的IP
System.out.println(Arrays.toString(bytes));
//根据机器名获取InetAddress实例
InetAddress inetAddress1=InetAddress.getByName("DESKTOP-E75FT3D");
System.out.println("计算机名称1:"+inetAddress1.getHostName());
System.out.println("计算机IP1:"+inetAddress1.getHostAddress());
//根据IP地址获取InetAddress实例
InetAddress inetAddress2=InetAddress.getByName("169.254.254.109");
System.out.println("计算机名称2:"+inetAddress2.getHostName());
System.out.println("计算机IP2:"+inetAddress2.getHostAddress());
}catch (Exception e){
e.printStackTrace();
}
}
}
3.URL
1.URL(Uniform Resource Locator) 统一资源定位符,表示Internet上某一资源的地址
2.URL有两个部分组成:协议名称 : 资源名称 中间用冒号隔开
3.在java.net包中,提供了URL类来表示URL
测试类
package com.example.demo.test;
import java.net.StandardSocketOptions;
import java.net.URL;
/**
* URL类常用方法
*/
public class TestUrl {
public static void main(String[] args) {
try {
//创建一个URL实例
URL baidu=new URL("http://www.baidu.com");
URL url=new URL(baidu,"/index.html");
System.out.println("协议:"+url.getProtocol());
System.out.println("主机:"+url.getHost());
//如果未指定端口号,则根据协议的不同使用默认的端口号,现在使用的80端口,这样他的getPort()方法返回值为:-1
System.out.println("端口:"+url.getPort());
System.out.println("文件路径:"+url.getPath());
System.out.println("文件名称:"+url.getFile());
System.out.println("相对路径:"+url.getRef());
System.out.println("查询字符串:"+url.getQuery());
}catch (Exception e){
e.printStackTrace();
}
}
}
使用URL读取网页内容
测试类
package com.example.demo.test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
/**
* 使用URL来读取网页内容
* 通过URL对象的openStream()方法可以得到指定资源的输入流
*/
public class TestUrl2 {
public static void main(String[] args) {
try {
//创建URL实例
URL url=new URL("http://www.baidu.com");
//通过URL的openStream方法获取URL对象所表示的资源的字节输入流
InputStream is=url.openStream();
//将字节输入流转换为字符输入流
InputStreamReader isr=new InputStreamReader(is);
//添加缓冲流增加读取速度
BufferedReader br=new BufferedReader(isr);
String data=br.readLine();//读取数据
while (data!=null){
System.out.println(data);
data=br.readLine();
}
//关闭资源
br.close();
isr.close();
is.close();
}catch (Exception e){
e.printStackTrace();
}
}
}