经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 其他 » 计算机原理 » 查看文章
网络编程与通信原理
来源:cnblogs  作者:知了一笑  时间:2022/12/12 9:00:16  对本文有异议

总感觉这个概念,和研发有点脱节;

一、基础概念

不同设备之间通过网络进行数据传输,并且基于通用的网络协议作为多种设备的兼容标准,称为网络通信;

以C/S架构来看,在一次请求当中,客户端和服务端进行数据传输的交互时,在不同阶段和层次中需要遵守的网络通信协议也不一样;

应用层:HTTP超文本传输协议,基于TCP/IP通信协议来传递数据;

传输层:TCP传输控制协议,采用三次握手的方式建立连接,形成数据传输通道;

网络层:IP协议,作用是把各种传输的数据包发送给请求的接收方;

通信双方进行交互时,发送方数据在各层传输时,每通过一层就会添加该层的首部信息;接收方与之相反,每通过一次就会删除该层的首部信息;

二、JDK源码

java.net源码包中,提供了与网络编程相关的基础API;

1、InetAddress

封装了对IP地址的相关操作,在使用该API之前可以先查看本机的hosts的映射,Linux系统中在/etc/hosts路径下;

  1. import java.net.InetAddress;
  2. public class TestInet {
  3. public static void main(String[] args) throws Exception {
  4. // 获取本机 InetAddress 对象
  5. InetAddress localHost = InetAddress.getLocalHost();
  6. printInetAddress(localHost);
  7. // 获取指定域名 InetAddress 对象
  8. InetAddress inetAddress = InetAddress.getByName("www.baidu.com");
  9. printInetAddress(inetAddress);
  10. // 获取本机配置 InetAddress 对象
  11. InetAddress confAddress = InetAddress.getByName("nacos-service");
  12. printInetAddress(confAddress);
  13. }
  14. public static void printInetAddress (InetAddress inetAddress){
  15. System.out.println("InetAddress:"+inetAddress);
  16. System.out.println("主机名:"+inetAddress.getHostName());
  17. System.out.println("IP地址:"+inetAddress.getHostAddress());
  18. }
  19. }

2、URL

统一资源定位符,URL一般包括:协议、主机名、端口、路径、查询参数、锚点等,路径+查询参数,也被称为文件;

  1. import java.net.URL;
  2. public class TestURL {
  3. public static void main(String[] args) throws Exception {
  4. URL url = new URL("https://www.baidu.com:80/s?wd=Java#bd") ;
  5. printURL(url);
  6. }
  7. private static void printURL (URL url){
  8. System.out.println("协议:" + url.getProtocol());
  9. System.out.println("域名:" + url.getHost());
  10. System.out.println("端口:" + url.getPort());
  11. System.out.println("路径:" + url.getPath());
  12. System.out.println("参数:" + url.getQuery());
  13. System.out.println("文件:" + url.getFile());
  14. System.out.println("锚点:" + url.getRef());
  15. }
  16. }

3、HttpURLConnection

作为URLConnection的抽象子类,用来处理针对Http协议的请求,可以设置连接超时、读取超时、以及请求的其他属性,是服务间通信的常用方式;

  1. public class TestHttp {
  2. public static void main(String[] args) throws Exception {
  3. // 访问 网址 内容
  4. URL url = new URL("https://www.jd.com");
  5. HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection();
  6. printHttp(httpUrlConnection);
  7. // 请求 服务 接口
  8. URL api = new URL("http://localhost:8082/info/99");
  9. HttpURLConnection apiConnection = (HttpURLConnection) api.openConnection();
  10. apiConnection.setRequestMethod("GET");
  11. apiConnection.setConnectTimeout(3000);
  12. printHttp(apiConnection);
  13. }
  14. private static void printHttp (HttpURLConnection httpUrlConnection) throws Exception{
  15. try (InputStream inputStream = httpUrlConnection.getInputStream()) {
  16. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
  17. String line ;
  18. while ((line = bufferedReader.readLine()) != null) {
  19. System.out.println(line);
  20. }
  21. }
  22. }
  23. }

三、通信编程

1、Socket

Socket也被称为套接字,是两台设备之间通信的端点,会把网络连接当成流处理,则数据以IO形式传输,这种方式在当前被普遍采用;

从网络编程直接跳到Socket套接字,概念上确实有较大跨度,概念过度抽象时,可以看看源码的核心结构,在理解时会轻松很多,在JDK中重点看SocketImpl抽象类;

  1. public abstract class SocketImpl implements SocketOptions {
  2. // Socket对象,客户端和服务端
  3. Socket socket = null;
  4. ServerSocket serverSocket = null;
  5. // 套接字的文件描述对象
  6. protected FileDescriptor fd;
  7. // 套接字的路由IP地址
  8. protected InetAddress address;
  9. // 套接字连接到的远程主机上的端口号
  10. protected int port;
  11. // 套接字连接到的本地端口号
  12. protected int localport;
  13. }

套接字的抽象实现类,是实现套接字的所有类的公共超类,可以用于创建客户端和服务器套接字;

所以到底如何理解Socket概念?从抽象类中来看,套接字就是指代网络通讯中系统资源的核心标识,比如通讯方IP地址、端口、状态等;

2、SocketServer

创建Socket服务端,并且在8989端口监听,接收客户端的连接请求和相关信息,并且响应客户端,发送指定的数据;

  1. public class SocketServer {
  2. public static void main(String[] args) throws Exception {
  3. // 1、创建Socket服务端
  4. ServerSocket serverSocket = new ServerSocket(8989);
  5. System.out.println("socket-server:8989,waiting connect...");
  6. // 2、方法阻塞等待,直到有客户端连接
  7. Socket socket = serverSocket.accept();
  8. System.out.println("socket-server:8989,get connect:"+socket.getPort());
  9. // 3、输入流,输出流
  10. InputStream inStream = socket.getInputStream();
  11. OutputStream outStream = socket.getOutputStream();
  12. // 4、数据接收和响应
  13. byte[] buf = new byte[1024];
  14. int readLen = 0;
  15. while ((readLen=inStream.read(buf)) != -1){
  16. // 接收数据
  17. String readVar = new String(buf, 0, readLen) ;
  18. if ("exit".equals(readVar)){
  19. break ;
  20. }
  21. System.out.println("recv:"+readVar+";time:"+DateTime.now().toString(DatePattern.NORM_DATETIME_PATTERN));
  22. // 响应数据
  23. outStream.write(("resp-time:"+DateTime.now().toString(DatePattern.NORM_DATETIME_PATTERN)).getBytes());
  24. }
  25. // 5、资源关闭
  26. outStream.close();
  27. inStream.close();
  28. socket.close();
  29. serverSocket.close();
  30. System.out.println("socket-server:8989,exit...");
  31. }
  32. }

需要注意的是步骤2输出的端口号是随机不确定的,结合jpslsof -i tcp:port命令查看进程和端口号的占用情况;

3、SocketClient

创建Socket客户端,并且连接到服务端,读取命令行输入的内容并发送到服务端,并且输出服务端的响应数据;

  1. public class SocketClient {
  2. public static void main(String[] args) throws Exception {
  3. // 1、创建Socket客户端
  4. Socket socket = new Socket(InetAddress.getLocalHost(), 8989);
  5. System.out.println("server-client,connect to:8989");
  6. // 2、输入流,输出流
  7. OutputStream outStream = socket.getOutputStream();
  8. InputStream inStream = socket.getInputStream();
  9. // 3、数据发送和响应接收
  10. int readLen = 0;
  11. byte[] buf = new byte[1024];
  12. while (true){
  13. // 读取命令行输入
  14. BufferedReader bufReader = new BufferedReader(new InputStreamReader(System.in));
  15. String iptLine = bufReader.readLine();
  16. if ("exit".equals(iptLine)){
  17. break;
  18. }
  19. // 发送数据
  20. outStream.write(iptLine.getBytes());
  21. // 接收数据
  22. if ((readLen = inStream.read(buf)) != -1) {
  23. System.out.println(new String(buf, 0, readLen));
  24. }
  25. }
  26. // 4、资源关闭
  27. inStream.close();
  28. outStream.close();
  29. socket.close();
  30. System.out.println("socket-client,get exit command");
  31. }
  32. }

测试结果:整个流程在没有收到客户端的exit退出指令前,会保持连接的状态,并且可以基于字节流模式,进行持续的数据传输;

4、字符流使用

基于上述的基础案例,采用字符流的方式进行数据传输,客户端和服务端只进行一次简单的交互;

  1. -- 1、客户端
  2. BufferedReader bufReader = new BufferedReader(new InputStreamReader(inStream));
  3. BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter(outStream));
  4. // 客户端发送数据
  5. bufWriter.write("hello,server");
  6. bufWriter.newLine();
  7. bufWriter.flush();
  8. // 客户端接收数据
  9. System.out.println("client-read:"+bufReader.readLine());
  10. -- 2、服务端
  11. BufferedReader bufReader = new BufferedReader(new InputStreamReader(inStream));
  12. BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter(outStream));
  13. // 服务端接收数据
  14. System.out.println("server-read:"+bufReader.readLine());
  15. // 服务端响应数据
  16. bufWriter.write("hello,client");
  17. bufWriter.newLine();
  18. bufWriter.flush();

5、文件传输

基于上述的基础案例,客户端向服务端发送图片文件,服务端完成文件的读取和保存,在处理完成后给客户端发送结果描述;

  1. -- 1、客户端
  2. // 客户端发送图片
  3. FileInputStream fileStream = new FileInputStream("Local_File_Path/jvm.png");
  4. byte[] bytes = new byte[1024];
  5. int i = 0;
  6. while ((i = fileStream.read(bytes)) != -1) {
  7. outStream.write(bytes);
  8. }
  9. // 写入结束标记,禁用此套接字的输出流,之后再使用输出流会抛异常
  10. socket.shutdownOutput();
  11. // 接收服务端响应结果
  12. System.out.println("server-resp:"+new String(bytes,0,readLen));
  13. -- 2、服务端
  14. // 接收客户端图片
  15. FileOutputStream fileOutputStream = new FileOutputStream("Local_File_Path/new_jvm.png");
  16. byte[] bytes = new byte[1024];
  17. int i = 0;
  18. while ((i = inStream.read(bytes)) != -1) {
  19. fileOutputStream.write(bytes, 0, i);
  20. }
  21. // 响应客户端文件处理结果
  22. outStream.write("file-save-success".getBytes());

6、TCP协议

Socket网络编程是基于TCP协议的,TCP传输控制协议是一种面向连接的、可靠的、基于字节流的传输层通信协议,在上述案例中侧重基于流的数据传输,其中关于连接还涉及两个核心概念:

三次握手:建立连接的过程,在这个过程中进行了三次网络通信,当连接处于建立的状态,就可以进行正常的通信,即数据传输;四次挥手:关闭连接的过程,调用close方法,即连接使用结束,在这个过程中进行了四次网络通信;

四、Http组件

在服务通信时依赖网络,而对于编程来说,更常见的是的Http的组件,在微服务架构中,涉及到Http组件工具有很多,例如Spring框架中的RestTemplate,Feign框架支持ApacheHttp和OkHttp;下面围绕几个常用的组件编写测试案例;

1、基础接口

  1. @RestController
  2. public class BizWeb {
  3. @GetMapping("/getApi/{id}")
  4. public Rep<Integer> getApi(@PathVariable Integer id){
  5. log.info("id={}",id);
  6. return Rep.ok(id) ;
  7. }
  8. @GetMapping("/getApi_v2/{id}")
  9. public Rep<Integer> getApiV2(HttpServletRequest request,
  10. @PathVariable Integer id,
  11. @RequestParam("name") String name){
  12. String token = request.getHeader("Token");
  13. log.info("token={},id={},name={}",token,id,name);
  14. return Rep.ok(id) ;
  15. }
  16. @PostMapping("/postApi")
  17. public Rep<IdKey> postApi(HttpServletRequest request,@RequestBody IdKey idKey){
  18. String token = request.getHeader("Token");
  19. log.info("token={},idKey={}", token,JSONUtil.toJsonStr(idKey));
  20. return Rep.ok(idKey) ;
  21. }
  22. @PutMapping("/putApi")
  23. public Rep<IdKey> putApi(@RequestBody IdKey idKey){
  24. log.info("idKey={}", JSONUtil.toJsonStr(idKey));
  25. return Rep.ok(idKey) ;
  26. }
  27. @DeleteMapping("/delApi/{id}")
  28. public Rep<Integer> delApi(@PathVariable Integer id){
  29. log.info("id={}",id);
  30. return Rep.ok(id) ;
  31. }
  32. }

2、ApacheHttp

  1. public class TestApacheHttp {
  2. private static final String BASE_URL = "http://localhost:8083" ;
  3. public static void main(String[] args) {
  4. BasicHeader header = new BasicHeader("Token","ApacheSup") ;
  5. // 1、发送Get请求
  6. Map<String,String> param = new HashMap<>() ;
  7. param.put("name","cicada") ;
  8. Rep getRep = doGet(BASE_URL+"/getApi_v2/3",header,param, Rep.class);
  9. System.out.println("get:"+getRep);
  10. // 2、发送Post请求
  11. IdKey postBody = new IdKey(1,"id-key-我") ;
  12. Rep postRep = doPost (BASE_URL+"/postApi", header, postBody, Rep.class);
  13. System.out.println("post:"+postRep);
  14. }
  15. /**
  16. * 构建HttpClient对象
  17. */
  18. private static CloseableHttpClient buildHttpClient (){
  19. // 请求配置
  20. RequestConfig reqConfig = RequestConfig.custom().setConnectTimeout(6000).build();
  21. return HttpClients.custom()
  22. .setDefaultRequestConfig(reqConfig).build();
  23. }
  24. /**
  25. * 执行Get请求
  26. */
  27. public static <T> T doGet (String url, Header header, Map<String,String> param,
  28. Class<T> repClass) {
  29. // 创建Get请求
  30. CloseableHttpClient httpClient = buildHttpClient();
  31. HttpGet httpGet = new HttpGet();
  32. httpGet.addHeader(header);
  33. try {
  34. URIBuilder builder = new URIBuilder(url);
  35. if (param != null) {
  36. for (String key : param.keySet()) {
  37. builder.addParameter(key, param.get(key));
  38. }
  39. }
  40. httpGet.setURI(builder.build());
  41. // 请求执行
  42. HttpResponse httpResponse = httpClient.execute(httpGet);
  43. if (httpResponse.getStatusLine().getStatusCode() == 200) {
  44. // 结果转换
  45. String resp = EntityUtils.toString(httpResponse.getEntity());
  46. return JSONUtil.toBean(resp, repClass);
  47. }
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. } finally {
  51. IoUtil.close(httpClient);
  52. }
  53. return null;
  54. }
  55. /**
  56. * 执行Post请求
  57. */
  58. public static <T> T doPost (String url, Header header, Object body,Class<T> repClass) {
  59. // 创建Post请求
  60. CloseableHttpClient httpClient = buildHttpClient();
  61. HttpPost httpPost = new HttpPost(url);
  62. httpPost.addHeader(header);
  63. StringEntity conBody = new StringEntity(JSONUtil.toJsonStr(body),ContentType.APPLICATION_JSON);
  64. httpPost.setEntity(conBody);
  65. try {
  66. // 请求执行
  67. HttpResponse httpResponse = httpClient.execute(httpPost);
  68. if (httpResponse.getStatusLine().getStatusCode() == 200) {
  69. // 结果转换
  70. String resp = EntityUtils.toString(httpResponse.getEntity());
  71. return JSONUtil.toBean(resp, repClass);
  72. }
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. }finally {
  76. IoUtil.close(httpClient);
  77. }
  78. return null;
  79. }
  80. }

3、OkHttp

  1. public class TestOkHttp {
  2. private static final String BASE_URL = "http://localhost:8083" ;
  3. public static void main(String[] args) {
  4. Headers headers = new Headers.Builder().add("Token","OkHttpSup").build() ;
  5. // 1、发送Get请求
  6. Rep getRep = execute(BASE_URL+"/getApi/1", Method.GET.name(), headers, null, Rep.class);
  7. System.out.println("get:"+getRep);
  8. // 2、发送Post请求
  9. IdKey postBody = new IdKey(1,"id-key") ;
  10. Rep postRep = execute(BASE_URL+"/postApi", Method.POST.name(), headers, buildBody(postBody), Rep.class);
  11. System.out.println("post:"+postRep);
  12. // 3、发送Put请求
  13. IdKey putBody = new IdKey(2,"key-id") ;
  14. Rep putRep = execute(BASE_URL+"/putApi", Method.PUT.name(), headers, buildBody(putBody), Rep.class);
  15. System.out.println("put:"+putRep);
  16. // 4、发送Delete请求
  17. Rep delRep = execute(BASE_URL+"/delApi/2", Method.DELETE.name(), headers, null, Rep.class);
  18. System.out.println("del:"+delRep);
  19. }
  20. /**
  21. * 构建JSON请求体
  22. */
  23. public static RequestBody buildBody (Object body){
  24. MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
  25. return RequestBody.create(mediaType, JSONUtil.toJsonStr(body)) ;
  26. }
  27. /**
  28. * 构建OkHttpClient对象
  29. */
  30. public static OkHttpClient buildOkHttp () {
  31. return new OkHttpClient.Builder()
  32. .readTimeout(10, TimeUnit.SECONDS).connectTimeout(6, TimeUnit.SECONDS)
  33. .connectionPool(new ConnectionPool(15, 5, TimeUnit.SECONDS))
  34. .build();
  35. }
  36. /**
  37. * 执行请求
  38. */
  39. public static <T> T execute (String url, String method,
  40. Headers headers, RequestBody body,
  41. Class<T> repClass) {
  42. // 请求创建
  43. OkHttpClient httpClient = buildOkHttp() ;
  44. Request.Builder requestBuild = new Request.Builder()
  45. .url(url).method(method, body);
  46. if (headers != null) {
  47. requestBuild.headers(headers);
  48. }
  49. try {
  50. // 请求执行
  51. Response response = httpClient.newCall(requestBuild.build()).execute();
  52. // 结果转换
  53. InputStream inStream = null;
  54. if (response.isSuccessful()) {
  55. ResponseBody responseBody = response.body();
  56. if (responseBody != null) {
  57. inStream = responseBody.byteStream();
  58. }
  59. }
  60. if (inStream != null) {
  61. try {
  62. byte[] respByte = IoUtil.readBytes(inStream);
  63. if (respByte != null) {
  64. return JSONUtil.toBean(new String(respByte, Charset.defaultCharset()), repClass);
  65. }
  66. } catch (Exception e) {
  67. e.printStackTrace();
  68. } finally {
  69. IoUtil.close(inStream);
  70. }
  71. }
  72. } catch (Exception e) {
  73. e.printStackTrace();
  74. }
  75. return null;
  76. }
  77. }

4、RestTemplate

  1. public class TestRestTemplate {
  2. private static final String BASE_URL = "http://localhost:8083" ;
  3. public static void main(String[] args) {
  4. RestTemplate restTemplate = buildRestTemplate() ;
  5. // 1、发送Get请求
  6. Map<String,String> paramMap = new HashMap<>() ;
  7. Rep getRep = restTemplate.getForObject(BASE_URL+"/getApi/1",Rep.class,paramMap);
  8. System.out.println("get:"+getRep);
  9. // 2、发送Post请求
  10. IdKey idKey = new IdKey(1,"id-key") ;
  11. Rep postRep = restTemplate.postForObject(BASE_URL+"/postApi",idKey,Rep.class);
  12. System.out.println("post:"+postRep);
  13. // 3、发送Put请求
  14. IdKey idKey2 = new IdKey(2,"key-id") ;
  15. restTemplate.put(BASE_URL+"/putApi",idKey2,paramMap);
  16. // 4、发送Delete请求
  17. restTemplate.delete(BASE_URL+"/delApi/2",paramMap);
  18. // 5、自定义Header请求
  19. HttpHeaders headers = new HttpHeaders();
  20. headers.add("Token","AdminSup");
  21. HttpEntity<IdKey> requestEntity = new HttpEntity<>(idKey, headers);
  22. ResponseEntity<Rep> respEntity = restTemplate.exchange(BASE_URL+"/postApi",
  23. HttpMethod.POST, requestEntity, Rep.class);
  24. System.out.println("post-header:"+respEntity.getBody());
  25. }
  26. private static RestTemplate buildRestTemplate (){
  27. // 1、参数配置
  28. SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
  29. factory.setReadTimeout(3000);
  30. factory.setConnectTimeout(6000);
  31. // 2、创建对象
  32. return new RestTemplate(factory) ;
  33. }
  34. }

五、参考源码

  1. 编程文档:
  2. https://gitee.com/cicadasmile/butte-java-note
  3. 应用仓库:
  4. https://gitee.com/cicadasmile/butte-flyer-parent

原文链接:https://www.cnblogs.com/cicada-smile/p/16974205.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号