网络
让多台电脑之间能够通信:广域网(外网)局域网(内网
TCP通信 javaweb基于Http通信
概念比较重要
七层模型
1.应用层2.表示层3.会话层4.运输层5.网络层6数据链路层7物理层(背好辣!)
TCP/IP参考模型
1.应用层2.运输层3.网络层4物理层
IP:ip地址是电脑唯一地址,方便通信
端口:端口号,0-65535个 确认与哪个程序通信
TCP:面向连接的可靠的链接,基于字节流
TCP通信的时候:发送数据之前与对方建立连接
TCP通信
socket编程:Socket编程
客户端和服务器现在可以通过字节流通信
1.服务器端
- 创建1个serversocket
- 调用accept(),等到客户端链接
- 得到客户端的socket对象
- 输入流接收数据输出流发送数据
- 关闭
2.客户端
- 创建socket,请求服务器地址等
- 调用getInputStream
- 接收信息 输出信息
- 关闭
一个例子:
在线小说系统
思路:
1.用户有两种选择 上传小说和阅读小说
-
上传小说
- 用户选择上传小说
- 向服务端发送小说名字
- 向服务端发送上传文件地址
- 收到上传成功or失败消息
-
用户阅读小说
-
用户选择阅读小说
-
收到服务端小说列表
-
发送想要阅读的小说名
-
收到服务端发送的小说
package Note; import java.io.*; import java.net.Socket; import java.util.Scanner; /** * @ClassName Client * @Description 添加描述 * @Author Dodo * @LastChangeDate 2022/8/25 11:55 * @Version v1.0 */ public class Client { private Socket socket; private InputStream in = null; private OutputStream out = null; private BufferedReader br = null; private BufferedWriter bw = null; public Client() throws IOException { Socket socket = new Socket("127.0.0.1", 6666); in = socket.getInputStream(); out = socket.getOutputStream(); br = new BufferedReader(new InputStreamReader(in)); bw = new BufferedWriter(new OutputStreamWriter(out)); } public void showChoice() throws IOException { System.out.println("请选择1.上传小说2.阅读小说3.退出"); Scanner input = new Scanner(System.in); int choice = input.nextInt(); if (choice == 1) { upNote(); showChoice(); } else if (choice == 2) { readNote(); showChoice(); } else { System.out.println("感谢使用"); sent("exit", bw); //关闭资源 } } public void sent(String str, BufferedWriter bw) throws IOException { bw.write(str); //input.next() 阻塞 bw.newLine(); //写了换行符 bw.flush(); } public void upNote() throws IOException { sent("上传小说", bw); Scanner input = new Scanner(System.in); System.out.println("请输入小说名字"); sent(input.next(), bw); System.out.println("请输入上传地址"); sent(input.next(), bw); String line = br.readLine(); //阻塞方法, 读到行结尾 换行符 System.out.println(line); } public void readNote() throws IOException { sent("阅读小说", bw); String lineTitles = null; lineTitles = br.readLine(); System.out.println("小说菜单"); while (!lineTitles.equals("end")) { System.out.print(lineTitles + " "); lineTitles = br.readLine(); } System.out.println(); System.out.println("请输入想看的小说"); Scanner input = new Scanner(System.in); String lineTitle = input.next(); sent(lineTitle, bw); //小说 String line = br.readLine(); while (!line.equals("结束")) { System.out.println(line); line = br.readLine(); } } public void closeRe() throws IOException { try { if (bw != null) { bw.close(); } if (br != null) br.close(); if (socket != null) socket.close(); } catch (IOException e2) { e2.printStackTrace(); } } public static void main(String[] args) throws IOException { Client client = new Client(); client.showChoice(); client.closeRe(); } }
-
服务端
-
用户上传小说
- 接收小说名
- 接收路径复制小说
- 将小说复制到指定文件夹并添加小说名到集合和名字文件
-
用户阅读小说
-
发送小说名给用户
-
接收小说名
-
发送小说
package Note; import java.io.*; import java.net.Socket; import java.util.ArrayList; import java.util.List; /** * @ClassName ServerThread * @Description 添加描述 * @Author Dodo * @LastChangeDate 2022/8/25 13:24 * @Version v1.0 */ public class ServerThread extends Thread{ File file = new File("E:\\练习\\title.txt"); private List<String> listTitle = null; private Socket client; private InputStream in = null; private OutputStream out = null; private BufferedReader br = null; private BufferedWriter bw = null; public ServerThread(String name, Socket client) throws IOException, ClassNotFoundException { super(name); this.client = client; if (!file.exists()) { file.createNewFile(); } listTitle = new ArrayList<>(); if (file.length() != 0) { InputStream inputStream = new FileInputStream(file); ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); FileInputStream fileInputStream = new FileInputStream(file); listTitle = (ArrayList<String>) objectInputStream.readObject(); } InputStream in = client.getInputStream(); OutputStream out = client.getOutputStream(); br = new BufferedReader(new InputStreamReader(in)); bw = new BufferedWriter(new OutputStreamWriter(out)); } @Override public void run() { while(true){ try { String postLine = br.readLine(); if (postLine.equals("exit")){ clientStop(); break; } if(postLine.equals("上传小说")){ upNote(); }else { readNote(); } } catch (IOException e) { e.printStackTrace(); } } } public void clientStop(){ System.out.println(Thread.currentThread().getName()+"退出访问"); try { if (bw != null) { bw.close(); } if (br != null) br.close(); if (client != null) client.close(); System.out.println(Thread.currentThread().getName() + "处理完毕"); } catch (IOException e2) { e2.printStackTrace(); } } public void readNote() throws IOException { String line = ""; for (String title : listTitle ) { bw.write(title); bw.newLine(); //写了换行符 bw.flush(); //刷新缓存区 } bw.write("end"); bw.newLine(); //写了换行符 bw.flush(); String postTitle =br.readLine(); System.out.println("用户搜索" + postTitle); StringBuilder stringBuilder = new StringBuilder(postTitle); postTitle = stringBuilder.append(".txt").toString(); File fileRead = new File("E:\\练习\\" + postTitle); if (!fileRead.exists()) { line = "没有收录该文章"; } else { FileInputStream fileInputStream = new FileInputStream(fileRead); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String readline = null; while ((readline = bufferedReader.readLine()) != null) { bw.write(readline); bw.newLine(); //写了换行符 bw.flush(); //刷新缓存区 } line = "结束"; } //发送数据 bw.write(line); bw.newLine(); //写了换行符 bw.flush(); //刷新缓存区 } public void upNote() throws IOException { System.out.println(Thread.currentThread().getName() + "上传小说"); String title = br.readLine(); System.out.println("小说名字:" + title); String filePath = "E:\\练习\\" + title + ".txt"; System.out.println(filePath); String oldFilePath = br.readLine(); System.out.println(oldFilePath); File source = new File(oldFilePath); File target = new File(filePath); if (!source.exists()) { bw.write("上传路径错误!"); bw.newLine(); //写了换行符 bw.flush(); //刷新缓存区 } else { copyFileByStream(source, target); listTitle.add(title); OutputStream outputStream = new FileOutputStream(file); ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); objectOutputStream.writeObject(listTitle); objectOutputStream.close(); bw.write("上传成功"); bw.newLine(); //写了换行符 bw.flush(); //刷新缓存区 } } public void copyFileByStream(File source, File target) { // 判断源文件是否存在,如果不存在,就退出程序 // target.getParentFile()表示用来获取目标对象的父目录对象 // 如果目标文件路径不存在,就创建 InputStream is = null; OutputStream os = null; try { // 准备输入流 is = new FileInputStream(source); // 准备输出流 os = new FileOutputStream(target); // 准备一个数组,用来存放读写的数据 byte[] b = new byte[1024]; int len = 0; // read(b)实现读取操作,数据存入b数组,返回读取长度给len,当所有内容都读取完毕,len=-1 while ((len = is.read(b)) != -1) { // 实现写的操作 os.write(b, 0, len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (is != null) { is.close(); } if (os != null) { os.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
-
UDP通信
UDP是一个面向无连接,不可靠的传输层协议.
(仅了解)