Java網(wǎng)絡(luò)通信基礎(chǔ)編程(必看篇)
方式一:同步阻塞方式(BIO):
服務(wù)器端(Server):
package com.ietree.basicskill.socket.mode1;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 服務(wù)端
*/
public class Server {
// 端口號
final static int PORT = 8765;
public static void main(String[] args) {
ServerSocket server = null;
try {
server = new ServerSocket(PORT);
System.out.println("Server start......");
// 進(jìn)行阻塞
Socket socket = server.accept();
// 創(chuàng)建一個程序執(zhí)行客戶端的任務(wù)
new Thread(new ServerHandler(socket)).start();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(server != null){
try {
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
server = null;
}
}
}
采用多線程來處理接收到的請求(ServerHandler):
package com.ietree.basicskill.socket.mode1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class ServerHandler implements Runnable {
private Socket socket;
public ServerHandler(Socket socket) {
this.socket = socket;
}
@Override
public void run() {
BufferedReader in = null;
PrintWriter out = null;
try {
in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
out = new PrintWriter(this.socket.getOutputStream(), true);
String body = null;
while (true) {
body = in.readLine();
if(body == null){
break;
}
System.out.println("Server: " + body);
out.println("服務(wù)器端回送響應(yīng)的數(shù)據(jù)。");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out != null){
try {
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
socket = null;
}
}
}
客戶端(Client):
package com.ietree.basicskill.socket.mode1;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
/**
* 客戶端
*/
public class Client {
final static String ADDRESS = "127.0.0.1";
final static int PORT = 8765;
public static void main(String[] args) {
Socket socket = null;
BufferedReader in = null;
PrintWriter out = null;
try {
socket = new Socket(ADDRESS, PORT);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// 向服務(wù)器端發(fā)送數(shù)據(jù)
out.println("接收到客戶端的請求數(shù)據(jù)......");
String response = in.readLine();
System.out.println("Client: " + response);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(out != null){
try {
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if(socket != null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
socket = null;
}
}
}
程序輸出:
Server:
Server start...... Server: 接收到客戶端的請求數(shù)據(jù)......
Client:
Client: 服務(wù)器端回送響應(yīng)的數(shù)據(jù)。
同步非阻塞(NIO)
異步非阻塞(AIO)
以上這篇Java網(wǎng)絡(luò)通信基礎(chǔ)編程(必看篇)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
聊聊Controller中RequestMapping的作用
這篇文章主要介紹了Controller中RequestMapping的作用,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
SpringBoot帶你實(shí)現(xiàn)一個點(diǎn)餐小程序
有個小伙伴臨時找到我,要開發(fā)一個點(diǎn)餐的系統(tǒng),時間比較著急,給了2天的時間。馬馬虎虎的搞出來了,頭發(fā)掉了一撮!下面介紹下本系統(tǒng),感興趣的小伙伴,可以參考開發(fā)下2022-07-07
一文簡介Java中BlockingQueue阻塞隊(duì)列
本文主要介紹了一文簡介Java中BlockingQueue阻塞隊(duì)列,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
Java8通過CompletableFuture實(shí)現(xiàn)異步回調(diào)
這篇文章主要介紹了Java8通過CompletableFuture實(shí)現(xiàn)異步回調(diào),CompletableFuture是Java?8?中新增的一個類,它是對Future接口的擴(kuò)展,下文關(guān)于其更多相關(guān)詳細(xì)介紹需要的小伙伴可以參考一下2022-04-04
Java BufferWriter寫文件寫不進(jìn)去或缺失數(shù)據(jù)的解決
這篇文章主要介紹了Java BufferWriter寫文件寫不進(jìn)去或缺失數(shù)據(jù)的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
PowerJob的GridFsManager工作流程源碼解讀
這篇文章主要為大家介紹了PowerJob的GridFsManager工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
在java中main函數(shù)如何調(diào)用外部非static方法
這篇文章主要介紹了在java中main函數(shù)如何調(diào)用外部非static方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12

