在线乱码卡一卡二卡新HD,最近韩国免费观看视频,国产色无码精品视频国产,亚洲男人的天堂久久香蕉

聊聊網(wǎng)絡(luò)編程與通信原理-天天熱點(diǎn)評(píng)
來(lái)源:知了一笑    時(shí)間:2022-12-11 19:51:28
一、基礎(chǔ)概念

不同設(shè)備之間通過(guò)網(wǎng)絡(luò)進(jìn)行數(shù)據(jù)傳輸,并且基于通用的網(wǎng)絡(luò)協(xié)議作為多種設(shè)備的兼容標(biāo)準(zhǔn),稱為網(wǎng)絡(luò)通信;

以C/S架構(gòu)來(lái)看,在一次請(qǐng)求當(dāng)中,客戶端和服務(wù)端進(jìn)行數(shù)據(jù)傳輸?shù)慕换r(shí),在不同階段和層次中需要遵守的網(wǎng)絡(luò)通信協(xié)議也不一樣;

應(yīng)用層:HTTP超文本傳輸協(xié)議,基于TCP/IP通信協(xié)議來(lái)傳遞數(shù)據(jù);


(相關(guān)資料圖)

傳輸層:TCP傳輸控制協(xié)議,采用三次握手的方式建立連接,形成數(shù)據(jù)傳輸通道;

網(wǎng)絡(luò)層:IP協(xié)議,作用是把各種傳輸?shù)臄?shù)據(jù)包發(fā)送給請(qǐng)求的接收方;

通信雙方進(jìn)行交互時(shí),發(fā)送方數(shù)據(jù)在各層傳輸時(shí),每通過(guò)一層就會(huì)添加該層的首部信息;接收方與之相反,每通過(guò)一次就會(huì)刪除該層的首部信息;

二、JDK源碼

在java.net?源碼包中,提供了與網(wǎng)絡(luò)編程相關(guān)的基礎(chǔ)API;

1、InetAddress

封裝了對(duì)IP地址的相關(guān)操作,在使用該API之前可以先查看本機(jī)的??hosts??的映射,Linux系統(tǒng)中在??/etc/hosts??路徑下;

import java.net.InetAddress;public class TestInet { public static void main(String[] args) throws Exception { // 獲取本機(jī) InetAddress 對(duì)象 InetAddress localHost = InetAddress.getLocalHost(); printInetAddress(localHost); // 獲取指定域名 InetAddress 對(duì)象 InetAddress inetAddress = InetAddress.getByName("www.baidu.com"); printInetAddress(inetAddress); // 獲取本機(jī)配置 InetAddress 對(duì)象 InetAddress confAddress = InetAddress.getByName("nacos-service"); printInetAddress(confAddress); } public static void printInetAddress (InetAddress inetAddress){ System.out.println("InetAddress:"+inetAddress); System.out.println("主機(jī)名:"+inetAddress.getHostName()); System.out.println("IP地址:"+inetAddress.getHostAddress()); }}2、URL

統(tǒng)一資源定位符,URL一般包括:協(xié)議、主機(jī)名、端口、路徑、查詢參數(shù)、錨點(diǎn)等,路徑+查詢參數(shù),也被稱為文件;

import java.net.URL;public class TestURL { public static void main(String[] args) throws Exception { URL url = new URL("https://www.baidu.com:80/s?wd=Java#bd") ; printURL(url); } private static void printURL (URL url){ System.out.println("協(xié)議:" + url.getProtocol()); System.out.println("域名:" + url.getHost()); System.out.println("端口:" + url.getPort()); System.out.println("路徑:" + url.getPath()); System.out.println("參數(shù):" + url.getQuery()); System.out.println("文件:" + url.getFile()); System.out.println("錨點(diǎn):" + url.getRef()); }}3、HttpURLConnection

作為URLConnection的抽象子類,用來(lái)處理針對(duì)Http協(xié)議的請(qǐng)求,可以設(shè)置連接超時(shí)、讀取超時(shí)、以及請(qǐng)求的其他屬性,是服務(wù)間通信的常用方式;

public class TestHttp { public static void main(String[] args) throws Exception { // 訪問(wèn) 網(wǎng)址 內(nèi)容 URL url = new URL("https://www.jd.com"); HttpURLConnection httpUrlConnection = (HttpURLConnection) url.openConnection(); printHttp(httpUrlConnection); // 請(qǐng)求 服務(wù) 接口 URL api = new URL("http://localhost:8082/info/99"); HttpURLConnection apiConnection = (HttpURLConnection) api.openConnection(); apiConnection.setRequestMethod("GET"); apiConnection.setConnectTimeout(3000); printHttp(apiConnection); } private static void printHttp (HttpURLConnection httpUrlConnection) throws Exception{ try (InputStream inputStream = httpUrlConnection.getInputStream()) { BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); String line ; while ((line = bufferedReader.readLine()) != null) { System.out.println(line); } } }}三、通信編程1、Socket

Socket也被稱為套接字,是兩臺(tái)設(shè)備之間通信的端點(diǎn),會(huì)把網(wǎng)絡(luò)連接當(dāng)成流處理,則數(shù)據(jù)以IO形式傳輸,這種方式在當(dāng)前被普遍采用;

從網(wǎng)絡(luò)編程直接跳到Socket套接字,概念上確實(shí)有較大跨度,概念過(guò)度抽象時(shí),可以看看源碼的核心結(jié)構(gòu),在理解時(shí)會(huì)輕松很多,在JDK中重點(diǎn)看SocketImpl抽象類;

public abstract class SocketImpl implements SocketOptions { // Socket對(duì)象,客戶端和服務(wù)端 Socket socket = null; ServerSocket serverSocket = null; // 套接字的文件描述對(duì)象 protected FileDescriptor fd; // 套接字的路由IP地址 protected InetAddress address; // 套接字連接到的遠(yuǎn)程主機(jī)上的端口號(hào) protected int port; // 套接字連接到的本地端口號(hào) protected int localport;}

套接字的抽象實(shí)現(xiàn)類,是實(shí)現(xiàn)套接字的所有類的公共超類,可以用于創(chuàng)建客戶端和服務(wù)器套接字;

所以到底如何理解Socket概念?從抽象類中來(lái)看,套接字就是指代網(wǎng)絡(luò)通訊中系統(tǒng)資源的核心標(biāo)識(shí),比如通訊方IP地址、端口、狀態(tài)等;

2、SocketServer

創(chuàng)建Socket服務(wù)端,并且在8989端口監(jiān)聽(tīng),接收客戶端的連接請(qǐng)求和相關(guān)信息,并且響應(yīng)客戶端,發(fā)送指定的數(shù)據(jù);

public class SocketServer { public static void main(String[] args) throws Exception { // 1、創(chuàng)建Socket服務(wù)端 ServerSocket serverSocket = new ServerSocket(8989); System.out.println("socket-server:8989,waiting connect..."); // 2、方法阻塞等待,直到有客戶端連接 Socket socket = serverSocket.accept(); System.out.println("socket-server:8989,get connect:"+socket.getPort()); // 3、輸入流,輸出流 InputStream inStream = socket.getInputStream(); OutputStream outStream = socket.getOutputStream(); // 4、數(shù)據(jù)接收和響應(yīng) byte[] buf = new byte[1024]; int readLen = 0; while ((readLen=inStream.read(buf)) != -1){ // 接收數(shù)據(jù) String readVar = new String(buf, 0, readLen) ; if ("exit".equals(readVar)){ break ; } System.out.println("recv:"+readVar+";time:"+DateTime.now().toString(DatePattern.NORM_DATETIME_PATTERN)); // 響應(yīng)數(shù)據(jù) outStream.write(("resp-time:"+DateTime.now().toString(DatePattern.NORM_DATETIME_PATTERN)).getBytes()); } // 5、資源關(guān)閉 outStream.close(); inStream.close(); socket.close(); serverSocket.close(); System.out.println("socket-server:8989,exit..."); }}

需要注意的是步驟2輸出的端口號(hào)是隨機(jī)不確定的,結(jié)合jps和lsof -i tcp:port?命令查看進(jìn)程和端口號(hào)的占用情況;

3、SocketClient

創(chuàng)建Socket客戶端,并且連接到服務(wù)端,讀取命令行輸入的內(nèi)容并發(fā)送到服務(wù)端,并且輸出服務(wù)端的響應(yīng)數(shù)據(jù);

public class SocketClient { public static void main(String[] args) throws Exception { // 1、創(chuàng)建Socket客戶端 Socket socket = new Socket(InetAddress.getLocalHost(), 8989); System.out.println("server-client,connect to:8989"); // 2、輸入流,輸出流 OutputStream outStream = socket.getOutputStream(); InputStream inStream = socket.getInputStream(); // 3、數(shù)據(jù)發(fā)送和響應(yīng)接收 int readLen = 0; byte[] buf = new byte[1024]; while (true){ // 讀取命令行輸入 BufferedReader bufReader = new BufferedReader(new InputStreamReader(System.in)); String iptLine = bufReader.readLine(); if ("exit".equals(iptLine)){ break; } // 發(fā)送數(shù)據(jù) outStream.write(iptLine.getBytes()); // 接收數(shù)據(jù) if ((readLen = inStream.read(buf)) != -1) { System.out.println(new String(buf, 0, readLen)); } } // 4、資源關(guān)閉 inStream.close(); outStream.close(); socket.close(); System.out.println("socket-client,get exit command"); }}

測(cè)試結(jié)果:整個(gè)流程在沒(méi)有收到客戶端的??exit??退出指令前,會(huì)保持連接的狀態(tài),并且可以基于字節(jié)流模式,進(jìn)行持續(xù)的數(shù)據(jù)傳輸;

4、字符流使用

基于上述的基礎(chǔ)案例,采用字符流的方式進(jìn)行數(shù)據(jù)傳輸,客戶端和服務(wù)端只進(jìn)行一次簡(jiǎn)單的交互;

-- 1、客戶端BufferedReader bufReader = new BufferedReader(new InputStreamReader(inStream));BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter(outStream));// 客戶端發(fā)送數(shù)據(jù)bufWriter.write("hello,server");bufWriter.newLine();bufWriter.flush();// 客戶端接收數(shù)據(jù)System.out.println("client-read:"+bufReader.readLine());-- 2、服務(wù)端BufferedReader bufReader = new BufferedReader(new InputStreamReader(inStream));BufferedWriter bufWriter = new BufferedWriter(new OutputStreamWriter(outStream));// 服務(wù)端接收數(shù)據(jù)System.out.println("server-read:"+bufReader.readLine());// 服務(wù)端響應(yīng)數(shù)據(jù)bufWriter.write("hello,client");bufWriter.newLine();bufWriter.flush();5、文件傳輸

基于上述的基礎(chǔ)案例,客戶端向服務(wù)端發(fā)送圖片文件,服務(wù)端完成文件的讀取和保存,在處理完成后給客戶端發(fā)送結(jié)果描述;

-- 1、客戶端// 客戶端發(fā)送圖片F(xiàn)ileInputStream fileStream = new FileInputStream("Local_File_Path/jvm.png");byte[] bytes = new byte[1024];int i = 0;while ((i = fileStream.read(bytes)) != -1) { outStream.write(bytes);}// 寫(xiě)入結(jié)束標(biāo)記,禁用此套接字的輸出流,之后再使用輸出流會(huì)拋異常socket.shutdownOutput();// 接收服務(wù)端響應(yīng)結(jié)果System.out.println("server-resp:"+new String(bytes,0,readLen));-- 2、服務(wù)端// 接收客戶端圖片F(xiàn)ileOutputStream fileOutputStream = new FileOutputStream("Local_File_Path/new_jvm.png");byte[] bytes = new byte[1024];int i = 0;while ((i = inStream.read(bytes)) != -1) { fileOutputStream.write(bytes, 0, i);}// 響應(yīng)客戶端文件處理結(jié)果outStream.write("file-save-success".getBytes());6、TCP協(xié)議

Socket網(wǎng)絡(luò)編程是基于TCP協(xié)議的,TCP傳輸控制協(xié)議是一種面向連接的、可靠的、基于字節(jié)流的傳輸層通信協(xié)議,在上述案例中側(cè)重基于流的數(shù)據(jù)傳輸,其中關(guān)于連接還涉及兩個(gè)核心概念:

三次握手:建立連接的過(guò)程,在這個(gè)過(guò)程中進(jìn)行了三次網(wǎng)絡(luò)通信,當(dāng)連接處于建立的狀態(tài),就可以進(jìn)行正常的通信,即數(shù)據(jù)傳輸;四次揮手:關(guān)閉連接的過(guò)程,調(diào)用??close??方法,即連接使用結(jié)束,在這個(gè)過(guò)程中進(jìn)行了四次網(wǎng)絡(luò)通信;

四、Http組件

在服務(wù)通信時(shí)依賴網(wǎng)絡(luò),而對(duì)于編程來(lái)說(shuō),更常見(jiàn)的是的Http的組件,在微服務(wù)架構(gòu)中,涉及到Http組件工具有很多,例如Spring框架中的RestTemplate,F(xiàn)eign框架支持ApacheHttp和OkHttp;下面圍繞幾個(gè)常用的組件編寫(xiě)測(cè)試案例;

1、基礎(chǔ)接口

@RestControllerpublic class BizWeb { @GetMapping("/getApi/{id}") public Rep getApi(@PathVariable Integer id){ log.info("id={}",id); return Rep.ok(id) ; } @GetMapping("/getApi_v2/{id}") public Rep getApiV2(HttpServletRequest request, @PathVariable Integer id, @RequestParam("name"){ String token = request.getHeader("Token"); log.info("token={},id={},name={}",token,id,name); return Rep.ok(id) ; } @PostMapping("/postApi") public Rep postApi(HttpServletRequest request,@RequestBody IdKey idKey){ String token = request.getHeader("Token"); log.info("token={},idKey={}", token,JSONUtil.toJsonStr(idKey)); return Rep.ok(idKey) ; } @PutMapping("/putApi") public Rep putApi(@RequestBody IdKey idKey){ log.info("idKey={}", JSONUtil.toJsonStr(idKey)); return Rep.ok(idKey) ; } @DeleteMapping("/delApi/{id}") public Rep delApi(@PathVariable Integer id){ log.info("id={}",id); return Rep.ok(id) ; }}2、ApacheHttp

public class TestApacheHttp { private static final String BASE_URL = "http://localhost:8083" ; public static void main(String[] args){ BasicHeader header = new BasicHeader("Token","ApacheSup") ; // 1、發(fā)送Get請(qǐng)求 Map param = new HashMap<>() ; param.put("name","cicada") ; Rep getRep = doGet(BASE_URL+"/getApi_v2/3",header,param, Rep.class); System.out.println("get:"+getRep); // 2、發(fā)送Post請(qǐng)求 IdKey postBody = new IdKey(1,"id-key-我") ; Rep postRep = doPost (BASE_URL+"/postApi", header, postBody, Rep.class); System.out.println("post:"+postRep); } /** * 構(gòu)建HttpClient對(duì)象 */ private static CloseableHttpClient buildHttpClient (){ // 請(qǐng)求配置 RequestConfig reqConfig = RequestConfig.custom().setConnectTimeout(6000).build(); return HttpClients.custom() .setDefaultRequestConfig(reqConfig).build(); } /** * 執(zhí)行Get請(qǐng)求 */ public static T doGet (String url, Header header, Map param, Class repClass){ // 創(chuàng)建Get請(qǐng)求 CloseableHttpClient httpClient = buildHttpClient(); HttpGet httpGet = new HttpGet(); httpGet.addHeader(header); try { URIBuilder builder = new URIBuilder(url); if (param != null) { for (String key : param.keySet()) { builder.addParameter(key, param.get(key)); } } httpGet.setURI(builder.build()); // 請(qǐng)求執(zhí)行 HttpResponse httpResponse = httpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode() == 200) { // 結(jié)果轉(zhuǎn)換 String resp = EntityUtils.toString(httpResponse.getEntity()); return JSONUtil.toBean(resp, repClass); } } catch (Exception e) { e.printStackTrace(); } finally { IoUtil.close(httpClient); } return null; } /** * 執(zhí)行Post請(qǐng)求 */ public static T doPost (String url, Header header, Object body,Class repClass){ // 創(chuàng)建Post請(qǐng)求 CloseableHttpClient httpClient = buildHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.addHeader(header); StringEntity conBody = new StringEntity(JSONUtil.toJsonStr(body),ContentType.APPLICATION_JSON); httpPost.setEntity(conBody); try { // 請(qǐng)求執(zhí)行 HttpResponse httpResponse = httpClient.execute(httpPost); if (httpResponse.getStatusLine().getStatusCode() == 200) { // 結(jié)果轉(zhuǎn)換 String resp = EntityUtils.toString(httpResponse.getEntity()); return JSONUtil.toBean(resp, repClass); } } catch (Exception e) { e.printStackTrace(); }finally { IoUtil.close(httpClient); } return null; }}3、OkHttp

public class TestOkHttp { private static final String BASE_URL = "http://localhost:8083" ; public static void main(String[] args){ Headers headers = new Headers.Builder().add("Token","OkHttpSup").build() ; // 1、發(fā)送Get請(qǐng)求 Rep getRep = execute(BASE_URL+"/getApi/1", Method.GET.name(), headers, null, Rep.class); System.out.println("get:"+getRep); // 2、發(fā)送Post請(qǐng)求 IdKey postBody = new IdKey(1,"id-key") ; Rep postRep = execute(BASE_URL+"/postApi", Method.POST.name(), headers, buildBody(postBody), Rep.class); System.out.println("post:"+postRep); // 3、發(fā)送Put請(qǐng)求 IdKey putBody = new IdKey(2,"key-id") ; Rep putRep = execute(BASE_URL+"/putApi", Method.PUT.name(), headers, buildBody(putBody), Rep.class); System.out.println("put:"+putRep); // 4、發(fā)送Delete請(qǐng)求 Rep delRep = execute(BASE_URL+"/delApi/2", Method.DELETE.name(), headers, null, Rep.class); System.out.println("del:"+delRep); } /** * 構(gòu)建JSON請(qǐng)求體 */ public static RequestBody buildBody (Object body){ MediaType mediaType = MediaType.parse("application/json; charset=utf-8"); return RequestBody.create(mediaType, JSONUtil.toJsonStr(body)) ; } /** * 構(gòu)建OkHttpClient對(duì)象 */ public static OkHttpClient buildOkHttp (){ return new OkHttpClient.Builder() .readTimeout(10, TimeUnit.SECONDS).connectTimeout(6, TimeUnit.SECONDS) .connectionPool(new ConnectionPool(15, 5, TimeUnit.SECONDS)) .build(); } /** * 執(zhí)行請(qǐng)求 */ public static T execute (String url, String method, Headers headers, RequestBody body, Class repClass){ // 請(qǐng)求創(chuàng)建 OkHttpClient httpClient = buildOkHttp() ; Request.Builder requestBuild = new Request.Builder() .url(url).method(method, body); if (headers != null) { requestBuild.headers(headers); } try { // 請(qǐng)求執(zhí)行 Response response = httpClient.newCall(requestBuild.build()).execute(); // 結(jié)果轉(zhuǎn)換 InputStream inStream = null; if (response.isSuccessful()) { ResponseBody responseBody = response.body(); if (responseBody != null) { inStream = responseBody.byteStream(); } } if (inStream != null) { try { byte[] respByte = IoUtil.readBytes(inStream); if (respByte != null) { return JSONUtil.toBean(new String(respByte, Charset.defaultCharset()), repClass); } } catch (Exception e) { e.printStackTrace(); } finally { IoUtil.close(inStream); } } } catch (Exception e) { e.printStackTrace(); } return null; }}4、RestTemplate

public class TestRestTemplate { private static final String BASE_URL = "http://localhost:8083" ; public static void main(String[] args){ RestTemplate restTemplate = buildRestTemplate() ; // 1、發(fā)送Get請(qǐng)求 Map paramMap = new HashMap<>() ; Rep getRep = restTemplate.getForObject(BASE_URL+"/getApi/1",Rep.class,paramMap); System.out.println("get:"+getRep); // 2、發(fā)送Post請(qǐng)求 IdKey idKey = new IdKey(1,"id-key") ; Rep postRep = restTemplate.postForObject(BASE_URL+"/postApi",idKey,Rep.class); System.out.println("post:"+postRep); // 3、發(fā)送Put請(qǐng)求 IdKey idKey2 = new IdKey(2,"key-id") ; restTemplate.put(BASE_URL+"/putApi",idKey2,paramMap); // 4、發(fā)送Delete請(qǐng)求 restTemplate.delete(BASE_URL+"/delApi/2",paramMap); // 5、自定義Header請(qǐng)求 HttpHeaders headers = new HttpHeaders(); headers.add("Token","AdminSup"); HttpEntity requestEntity = new HttpEntity<>(idKey, headers); ResponseEntity respEntity = restTemplate.exchange(BASE_URL+"/postApi", HttpMethod.POST, requestEntity, Rep.class); System.out.println("post-header:"+respEntity.getBody()); } private static RestTemplate buildRestTemplate (){ // 1、參數(shù)配置 SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setReadTimeout(3000); factory.setConnectTimeout(6000); // 2、創(chuàng)建對(duì)象 return new RestTemplate(factory) ; }}五、參考源碼

編程文檔:https://gitee.com/cicadasmile/butte-java-note

應(yīng)用倉(cāng)庫(kù):https://gitee.com/cicadasmile/butte-flyer-parent

關(guān)鍵詞: 數(shù)據(jù)傳輸 網(wǎng)絡(luò)通信 傳輸控制協(xié)議 通信協(xié)議 發(fā)送數(shù)據(jù)

上一篇:

下一篇:

X 關(guān)閉

X 關(guān)閉

<蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <文本链> <文本链> <文本链> <文本链> <文本链> <文本链>