网络编程的概述
计算机网络:将各个网络连接起来
网络编程的目的:直接或间接地通过网络协议与其他计算机实现数据交换,进行通讯
通信双方地址:
1.IP 2.端口号
一定的规则:
1.OSI模型
2.TCP/IP模型
Ip地址:唯一表示一台主机
Java中使用InetAddress类代表IP
IPV4 和 IPV6
IPV4 : 四个字节组成
IPV6: 十六个字节组成
域名:www.baidu.com
本地回路地址:127.0.0.1 localhost
端口号的理解:不同的进程有不同的端口号
被规定为一个16位的整数 0 - 65535
端口分类:不同的进程拥有不同的端口号
公认端口:0 - 1023
注册端口:1024 - 49151
动态/私有端口:49152 - 65535
端口号 和 Ip 组合在一起得出了 网络套接字:Socket
网络协议
传输层中:
Tcp:传输控制协议
可靠的
进行大数据量的传输
需释放建立的连接,效率低
UDP: 用户数据报协议
不需要建立连接
不可靠的
不需释放资源,开销小,速度快
一些方法的使用
InetAddress inet1 = InetAddress.getByName("192.168.10.14");///192.168.10.14
InetAddress inet2 = InetAddress.getByName("www.baidu.com");//www.baidu.com/110.242.68.3
System.out.println(inet2.getHostAddress());//110.242.68.3
System.out.println(inet2.getHostName());//www.baidu.com
InetAddress inet3 = InetAddress.getByName("127.0.0.1");///127.0.0.1
System.out.println(InetAddress.getLocalHost());//WIN-1TS86EU...
实现TCP的网络编程:客户机和服务器
要先运行服务器,在运行客户机
//客户机
@Test
public void test019_client() throws IOException {
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
Socket socket = new Socket(inetAddress,8899);
OutputStream os = socket.getOutputStream();
os.write("你好".getBytes());
os.close();
socket.close();
}
//服务器:
@Test
public void test019_server() throws IOException {
ServerSocket ss = new ServerSocket(8899);
Socket socket = ss.accept();
InputStream is = socket.getInputStream();
//不建议这样写
// byte[] buffer = new byte[1024];
// int len;
// while((len = is.read(buffer))!= -1){
// String str = new String(buffer,0,len);
// System.out.println(str);
// }
ByteArrayOutputStream 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());
baos.close();
is.close();
ss.close();
socket.close();
}
TCP传输图片
@Test
public void test021_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("微信图片_20210525115618.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();
}
@Test
public void test21_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 sender23() 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 receiver23() 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网络编程
@Test
public void test24(){
/*
URL网络编程
1.URL:统一资源定位符,对应着互联网的某一资源地址
2.格式:
http://localhost:8080/examples/beauty.jpg?username=Tom
协议 主机名 端口号 资源地址 参数列表
*/
try {
URL url = new URL("http://localhost:8080/examples/beauty.jpg?username=Tom");
// public String getProtocol( ) 获取该URL的协议名
System.out.println(url.getProtocol());
// public String getHost( ) 获取该URL的主机名
System.out.println(url.getHost());
// public String getPort( ) 获取该URL的端口号
System.out.println(url.getPort());
// public String getPath( ) 获取该URL的文件路径
System.out.println(url.getPath());
// public String getFile( ) 获取该URL的文件名
System.out.println(url.getFile());
// public String getQuery( ) 获取该URL的查询名
System.out.println(url.getQuery());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
使用Tomcat在网站上下载文件
@Test
public void test25(){
//Tomcat从网址上下载文件
HttpURLConnection urlConnection = null;
InputStream is = null;
FileOutputStream fos = null;
try {
//1
URL url = new URL("http://localhost:8080/examples/beauty.jpg");
//2
urlConnection = (HttpURLConnection) url.openConnection();
//3
urlConnection.connect();
//4
is = urlConnection.getInputStream();
fos = new FileOutputStream("day10\\beauty3.jpg");
//5
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();
}
}
}
}