1. 网络编程的常识
(1) 七层网络模型
(2) 相关的协议(笔试题)
1. 协议的概念
- 协议让计算机之间进行通讯的一套规则
2. TCP协议
3. UDP协议
(3) IP地址相关(重点)
(4) (重点)端口号
2. 基于TCP协议的编程模型(重点)
(1) C/S架构的简介
(2) 编程模型
(3)相关类和方法的解析
1. ServerSocket类
2. Socket类
package com.lagou.task19;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;
/**
* @author 西风月
* @date 2020/8/29
* @description
*/
public class ClientStringTest {
public static void main(String[] args) {
Socket s = null;
PrintStream ps = null;
Scanner sc = null;
BufferedReader br = null;
try {
//1. 创建Socket类型的对象并提供服务器的通信地址
s = new Socket("127.0.0.1", 6666);
System.out.println("连接服务器成功!");
//2. 使用输入输出流进行通信
ps = new PrintStream(s.getOutputStream());
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
while(true) {
System.out.println("请输入要发送的数据:");
sc = new Scanner(System.in);
String s1 = sc.next();
//实现客户端向服务器发送字符串"hello"
ps.println(s1);
//实现接受服务器发来的字符串内容并打印
String s2 = br.readLine();
System.out.println("服务器回发的消息是:" + s2);
if("bye".equalsIgnoreCase(s1)) {
System.out.println("聊天结束!");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//3. 关闭Socket并释放资源
if(br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(sc != null) {
sc.close();
}
if(null != s) {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(null != ps) {
ps.close();
}
}
}
}
package com.lagou.task19;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* @author 西风月
* @date 2020/8/29
* @description
*/
public class ServerStringTest {
public static void main(String[] args) {
ServerSocket ss = null;
Socket s = null;
try {
//1. 创建ServerSocket类型的对象并提供端口号
ss = new ServerSocket(6666);
//2. 等待客户端的连接请求,调用accept方法
while(true) {
System.out.println("等待客户端的连接请求!");
s = ss.accept(); //当没有客户端连接时,则服务器阻塞在accept方法的调用这里
System.out.println("客户端" +s.getInetAddress() + "连接成功~");
//每当有一个客户端连接成功,则需要启动一个新的线程为之提供服务
new ServerThread(s).start();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4. 关闭Socket并释放有关的资源
if(null != ss) {
try {
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
package com.lagou.task19;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.util.SortedMap;
/**
* @author 西风月
* @date 2020/8/30
* @description
*/
public class ServerThread extends Thread {
private Socket s;
public ServerThread(Socket s) {
this.s = s;
}
@Override
public void run() {
BufferedReader br = null;
PrintStream ps = null;
try {
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
ps = new PrintStream(s.getOutputStream());
while(true) {
//3. 使用输入输出流进行通信
//实现对客户端发来字符内容的接受并且打印
String s1 = br.readLine();
System.out.println("客户端" +s.getInetAddress() + "发来的字符串是:" + s1);
if("bye".equalsIgnoreCase(s1)) {
System.out.println("客户端" +s.getInetAddress() + "下线了!");
break;
}
ps.println("I Recevice!");
System.out.println("服务器发送数据成功!");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(null != s) {
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(ps != null) {
ps.close();
}
if(null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
3. 基于udp协议的编程模型(熟悉)
(1)编程模型
(2) 相关类和方法的解析
1. DatagramSocket类
2. DatagramPacket类
3. InetAddress类
package com.lagou.task19;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
/**
* @author 西风月
* @date 2020/8/30
* @description
*/
public class ReceiveTest {
public static void main(String[] args) {
DatagramSocket ds = null;
try {
//1. 创建DatagramSocket类型的对象并提供端口号
ds = new DatagramSocket(8888);
//2. 创建DatagramPackage类型的对象并提供缓冲区接数据
byte[] bArr = new byte[20];
DatagramPacket dp = new DatagramPacket(bArr, bArr.length);
//3. 通过Socket接收数据内容并放到Packet里面,调用receive方法
System.out.println("等待数据中.....");
ds.receive(dp);
System.out.println("接收的数据是:" + new String(bArr) + "!");
//回发消息
byte[] bArr2 = "I Receive!".getBytes();
DatagramPacket dp2 = new DatagramPacket(bArr2, bArr2.length, dp.getAddress(), dp.getPort());
ds.send(dp2);
System.out.println("回发消息成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
//4. 关闭Socket并释放有关资源
if(ds != null) {
ds.close();
}
}
}
}
package com.lagou.task19;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
/**
* @author 西风月
* @date 2020/8/30
* @description
*/
public class SendTest {
public static void main(String[] args) {
DatagramSocket ds = null;
try {
//1. 创建DatagramSocket类型的对象
ds = new DatagramSocket();
//2. 创建DatagramSocket类型对象并提供接收方的通信地址和端口
byte[] bArr = "hello".getBytes();
DatagramPacket dp = new DatagramPacket(bArr, bArr.length, InetAddress.getLocalHost(), 8888);
//3. 通过 Socket发送Packet,调用send方法
ds.send(dp);
System.out.println("发送数据成功!");
//4. 接受回发消息
byte[] bArr2 = new byte[20];
DatagramPacket dp2 = new DatagramPacket(bArr2, bArr2.length);
ds.receive(dp2);
System.out.println("接收到的回发消息内容是:" + new String(bArr2, 0, dp2.getLength()));
} catch (IOException e) {
e.printStackTrace();
} finally {
//4. 关闭Socket并释放有关的资源
if(ds != null) {
ds.close();
}
}
}
}
4. URL类(熟悉)
(1) 基本概念
(2)常用的方法
(3)URLConnection类
package com.lagou.task19;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
/**
* @author 西风月
* @date 2020/8/30
* @description
*/
public class URLTest {
public static void main(String[] args) {
URL url = null;
try {
url = new URL("https://www.lagou.com");
System.out.println("获取到的协议名称是:"+ url.getProtocol());
System.out.println("获取到的主机名称是:" + url.getHost());
System.out.println("获取到的端口号是:" + url.getPort());
//获取URLConnection类的实例
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream i = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(i));
String str = null;
while((str = br.readLine()) != null) System.out.println(str);
br.close();
//断开连接
urlConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
} finally {
//省略不写....
}
}
}
5. 总结
- 网络编程
- 网络编程的常识
- tcp协议的编程模型
- udp协议的编程模型
- URL类的概念