Java基础——网络编程
文章目录
一、网络编程概述
实现网络通信需要解决的两个问题
1.如何准确地定位网络上一台或多台主机;定位主机上的特定的应用。
2.找到主机后如何可靠高效地进行数据传输
二、网络通信要素概述
网络通信的两个要素
1.对应问题一:IP和端口号
2.对应问题二:提供网络通信协议:TCP/IP参考模型(应用层、传输层、网络层、物理+数据链路层)
三、通信要素1:IP和端口号
1、IP
1.IP:唯一的标识Internet上的计算机(通信实体)
2.在Java中使用InetAddress类代表IP
3.域名:www.baidu.com、www.jd.com
4.本地回路地址:127.0.0.1,对应着:localhost
2、IP的分类
1.分类一:IPV4和IPV6
1)IPV4:4个字节组成,4个0-255。
2)IPV6:16个字节(128位),写成8个无符号整数,每个整数用四个十六进制位表示。
2.分类二:公网地址(万维网使用)和私有地址(局域网使用)
192.168开头的就是私有地址,范围即为192.168.0.0-192.168.255.255
3、InetAddress类
InetAddress类的一个对象代表一个具体的IP地址
1)实例化
1、在给定主机名的情况下确定主机的IP地址,如果参数为null,获得的是本机的IP地址
static InetAddress getByName(String host)
2、获取本机的地址
static InetAddress getLocalHost()
InetAddress inet1 = InetAddress.getByName("192.168.1.1");
InetAddress inet2 = InetAddress.getByName("www.baidu.com");
InetAddress inet3 = InetAddress.getByName("");
InetAddress inet4 = InetAddress.getLocalHost();
System.out.println(inet1);
System.out.println(inet2);
System.out.println(inet3);
System.out.println(inet4);
/*
运行结果:/192.168.1.1
www.baidu.com/36.152.44.96
localhost/127.0.0.1
DESKTOP-AP8TS05/192.168.10.104
*/
2)常用方法
1、获取此 IP 地址的主机名,即域名
String getHostName()
2、获取IP地址,返回 IP 地址字符串(以文本表现形式)
String getHostAddress()
InetAddress inet5 = InetAddress.getByName("www.jd.com");
System.out.println(inet5);
//获取域名
System.out.println(inet5.getHostName());
//获取IP地址
System.out.println(inet5.getHostAddress());
/*
运行结果:www.jd.com/183.216.190.3
www.jd.com
183.216.190.3
*/
4、端口号
1.端口号:正在计算机上运行的进程(程序)。
2.要求:不同的进程有不同的端口号
3.范围:被规定为一个16位的整数:0-65535
4.端口号与IP地址的组合得出一个网络套接字:Socket
5、端口分类
1.公认端口:0-1023。被预先定义的服务通信占用(如:HTTP占用端口80,FTP占用端口21、Telnet占用端口23)
2.注册端口:1024-49151。分配给用户进程或应用程序。(如:Tomcat占用端口8080,MySQL占用端口3306,Oracle占用端口1521等)
3.动态/私有端口:49152~65535
四、通信要素2:网络协议
1、模型
TCP/IP参考模型 | TCP/IP参考模型各层对应协议 |
---|---|
应用层 | HTTP、FTP、Telent、DNS等 |
传输层 | TCP、UDP |
网络层 | IP、ICMP、ARP |
物理+数据链路层 | LInk |
2、TCP和UDP的区别
TCP协议 | UDP协议 |
---|---|
使用TCP协议前,须先建立TCP链接,形成传输数据通道 | 将数据、源、目的封装成数据包,不需要建立链接 |
传输前,采用“三次握手”方式,传输后,采用“四次握手”方式,点对点通信,是可靠的 | 发送不管对方是否准备好,接收方收到也不确认,因此是不可靠的 |
在连接过程中可进行大数据量的传输 | 每个数据的大小限制在64k内 |
传输完毕,需是释放已建立的连接,效率低 | 发送数据结束时无需释放资源,开销小,速度快 |
TCP协议进行通信的两个应用进程:客户端、服务端 | 可以广播发送 |
生活实例:打电话 | 生活实例:发送短信、发电报 |
五、TCP网络编程
1、示例一:
例子一:客户端发送信息给服务端,服务端将数据显示在控制台上
1)客户端
1、创建Socket对象,指定服务端的ip地址和端口
public Socket(InetAddress address, int port)
2、打开连接到 Socket 的输出流
public OutputStream getOutputStream()
3、Socket 进行写操作
public void write(byte b[])
4、关闭Socket
public synchronized void close()
Socket socket = null;
OutputStream os = null;
try {
//1、创建Socket对象,指定服务端的ip地址和端口
InetAddress inet = InetAddress.getByName("127.0.0.1");
socket = new Socket(inet, 8899);
//2、获取一个输出流,用于输出数据
os = socket.getOutputStream();
//3、写出数据的操作
os.write("你好,我是客户端!".getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
if(os != null){
try {
//4、资源的关闭
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2)服务端
1、创建服务器端的ServerSocket,指明自己的端口号
public ServerSocket(int port)
2、调用accept()表示接受来自于客户端的socket
public Socket accept()
3、获取输入流
public InputStream getInputStream()
4、读取输入流中的数据
public int read(byte b[])
5、关闭ServerSocket和Socket对象
public void close()
public synchronized void close()
ServerSocket ss = null;
Socket socket = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//1、创建服务器端的ServerSocket,指明自己的端口号
ss = new ServerSocket(8899);
//2、调用accept()表示接受来自于客户端的socket
socket = ss.accept();
//3、获取输入流
is = socket.getInputStream();
//4、读取输入流中的数据
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 (Exception e){
e.printStackTrace();
}finally {
if(baos!=null){
try {
//5、关闭资源
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(ss!=null){
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
运行结果:你好,我是客户端!
收到了来自于:127.0.0.1
*/
2、示例二:
例题2:客户端发送文件给服务端,服务端将文件保存在本地。
1)客户端
Socket socket = null;
OutputStream os = null;
FileInputStream fis = null;
try {
//1、创建Socket对象,指明服务器的ip和端口号
InetAddress inet = InetAddress.getByName("127.0.0.1");
socket = new Socket(inet, 9090);
//2、获取一个输出流,用于输出数据
os = socket.getOutputStream();
//3、写出数据的操作
fis = new FileInputStream(new File("01.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read())!=-1){
os.write(buffer,0,len);
}
} catch (Exception 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(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2)服务端
ServerSocket ss = null;
Socket socket = null;
InputStream is = null;
FileOutputStream fos = null;
try {
//1、创建服务器端的ServerSocket,指明自己的端口号
ss = new ServerSocket(9090);
//2、调用accept()表示接受来自于客户端的socket
socket = ss.accept();
//3、获取输入流
is = socket.getInputStream();
//4、读取输入流中的数据
fos = new FileOutputStream(new File("dog.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = is.read())!=-1){
fos.write(buffer,0,len);
}
System.out.println("文件已经写出,请及时查收");
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fos!=null){
try {
//5、关闭资源
fos.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(ss!=null){
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
运行结果:文件已经写出,请及时查收
*/
3、示例三:
.示例3:从客户端发送文件给服务端,服务端保存到本地。并返回“你好,照片已经传输完毕,请您查收”给客户端。并关闭相应的连接
1)客户端
1、关闭客户端的输出流。相当于给流中加入一个结束标记-1
public void shutdownOutput()
Socket socket = null;
OutputStream os = null;
FileInputStream fis = null;
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
//1、创建Socket对象,指明服务器的ip和端口号
InetAddress inet = InetAddress.getByName("127.0.0.1");
socket = new Socket(inet, 9090);
//2、获取一个输出流,用于输出数据
os = socket.getOutputStream();
//3、写出数据的操作
fis = new FileInputStream(new File("01.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read())!=-1){
os.write(buffer,0,len);
}
socket.shutdownOutput();
//4、接受来自于服务器端的数据,并显示到控制台上
is = socket.getInputStream();
baos = new ByteArrayOutputStream();
byte[] buffer1 = new byte[1024];
int len1;
while((len1 = is.read(buffer1))!=-1){
baos.write(buffer1,0,len1);
}
System.out.println(baos.toString());
} catch (Exception e) {
e.printStackTrace();
}finally {
if(baos!=null){
try {
//5、关闭资源
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
运行结果:你好,照片已经传输完毕,请您查收
*/
2)服务端
ServerSocket ss = null;
Socket socket = null;
InputStream is = null;
FileOutputStream fos = null;
OutputStream os = null;
try {
//1、创建服务器端的ServerSocket,指明自己的端口号
ss = new ServerSocket(9090);
//2、调用accept()表示接受来自于客户端的socket
socket = ss.accept();
//3、获取输入流
is = socket.getInputStream();
//4、读取输入流中的数据
fos = new FileOutputStream(new File("dog0001.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = is.read())!=-1){
fos.write(buffer,0,len);
}
//5、服务器端给客户端反馈
os = socket.getOutputStream();
os.write("你好,照片已经传输完毕,请您查收".getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally {
if(os!=null){
try {
//6、关闭资源
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos!=null){
try {
fos.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(ss!=null){
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
六、UDP网络编程
示例:
示例:发送方发送“你好,我是UDP的发送方”给接收方
1)发送方
1、构造数据报包,用来将长度为 length 的包发送到指定主机上的指定端口号。length参数必须小于等于 buf.length
public DatagramPacket(byte buf[], int offset, int length,InetAddress address, int port)
2、从此套接字发送数据报包
public void send(DatagramPacket p)
DatagramSocket socket = null;
try {
//1、创建发送端
socket = new DatagramSocket();
//2、建立数据包
String str = "你好,我是UDP方法的发送方";
byte[] data = str.getBytes();
InetAddress inet = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9091);
//3、调用DatagramSocket的发送方法
socket.send(packet);
} catch (Exception e) {
e.printStackTrace();
}finally {
//4、关闭socket
if(socket!=null){
socket.close();
}
}
2)接收方
1、创建数据报套接字并将其绑定到本地主机上的指定端口。
public DatagramSocket(int port)
2、构造 DatagramPacket,用来接收长度为 length 的数据包
public DatagramPacket(byte buf[], int offset, int length)
3、从此套接字接收数据报包
public synchronized void receive(DatagramPacket p)
4、返回数据缓冲区
public synchronized byte[] getData()
5、返回将要发送或接收到的数据的长度
public synchronized int getLength()
DatagramSocket socket = null;
try {
//1、创建接受端,指明端口
socket = new DatagramSocket(9091);
//2、建立数据包
byte[] buffer = new byte[100];
DatagramPacket packet = new DatagramPacket(buffer,buffer.length);
//3、调用DatagramSocket的接收方法
socket.receive(packet);
System.out.println(new String(packet.getData(),0,packet.getLength()));
} catch (Exception e) {
e.printStackTrace();
}finally {
//4、关闭socket
if(socket!=null){
socket.close();
}
}
/*
运行结果:你好,我是UDP方法的发送方
*/
七、URL编程
1.URL(Uniform Resource Locator):统一资源定位符,它表示Internet上某一资源的地址
2.URL的基本结构由5部分组成:
<传输协议>://<主机名>:<端口号>/<文件名>#片段名?参数列表
例如:
http://192.168.1.100:8080/helloworld/index.jsp#a?username=shkstart&password=123
1、常用方法
1、获取该URL的协议名
public String getProtocol()
2、获取该URL的主机名
public String getHost()
3、获取该URL的端口号
public String getPort()
4、获取该URL的文件路径
public String getPath()
5、获取该URL的文件名
public String getFile()
6、获取该URL的查询名
public String getQuery()
try {
URL url = new URL("http://192.168.1.100:8080/helloworld/index.jsp#a?username=shkstart&password=123");
//获取该URL的协议名
System.out.println(url.getProtocol());
//获取该URL的主机名
System.out.println(url.getHost());
//获取该URL的端口名
System.out.println(url.getPort());
//获取该URL的文件路径
System.out.println(url.getPath());
//获取该URL的文件名
System.out.println(url.getFile());
//获取该URL的查询名
System.out.println(url.getQuery());
} catch (MalformedURLException e) {
e.printStackTrace();
}
/*
运行结果:http
192.168.1.100
8080
/helloworld/index.jsp
/helloworld/index.jsp
null
*/
示例
读取、下载对应的url资源
1、返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接
public URLConnection openConnection()
2、获取输入流
public InputStream getInputStream()
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("beauty01.jpg");
byte[] buffer = new byte[1024];
int len;
while((len = is.read())!=-1){
fos.write(buffer,0,len);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(urlConnection!=null){
urlConnection.disconnect();
}
}